The Pi Becomes a Data Collector

The Raspberry Pi can connect directly to sensors.  From temperature and humidity to light and motion to sound and well beyond, these add-ons can be sold individually or in large kits.  This ability to read and process data from a sensor turns your Raspberry Pi into an instant IoT device – for under $50.

Get a Sensor

In this project, a 3-pin DHT22 sensor with capacitive humidity sensor and a thermistordht22 (for temperature  and humidity) will be used.  If ordering from Amazon you can either get sensors cheap or fast – bur rarely both.  I purchased the $9.99 Gowoops version (2 sensors at $5 a sensor) of the DHT22 and received it in about two days.  I also purchased the $3.68 Sodial version (1 sensor) and received it in about one month. Make sure the sensor you order comes with cables/wires to connect the sensor to the Pi.   If you’re lucky enough to have a MicroCenter or similar store in your city, you might also look there.  There doesn’t appear to be much difference in sensor quality (in my two sensors).  What I paid for was speed of delivery.

Pi Compatibility

The easiest way to connect a sensor to a Raspberry Pi is by using the built-in header, available on current Pi models (except the Pi Zero).  On the Pi 3 B+, the basic GPIOPi3BPlus header is a 2×20 male pin connector panel that is built-in to the Pi’s board (visible on the top edge of the board pictured).  Each pin has an address; some pins are designated for data input or output, others for power, and some are ground.  Connecting the sensor is a matter of getting the three DH22 wires connected to the correct pins.

Generally for this type of project, I would recommend the Pi 3 B+, due to its processor speed and built-in hardware.  A Pi Zero for example does not have a built-in header and one must be soldered in place first.

Connect the Sensor

  1. Turn the Pi off and unplug the power.
  2. Remove the case. Position the Pi’s board so the header sits at the top edge (away from you).  Look at the GPIO header diagram below. Locate pin 1, which is on the left side of your board in the row closest to you.GPIOB-Plus
  3. Connect the 3 colored jumper cables from the sensor to the GPIO.  Cable colors will vary.  Technically, on the header, it doesn’t matter which power, ground, or GPIO pins you use, as long as you know the GPIO number of the data cable.  It will need to be used in your test program.  I recommend:
    1. Power cable: sensor (+) to header pin 1
    2. Data cable: sensor (out) to header GPIO4 (same as pin 7)
    3. Ground cable: sensor (-) to header pin 6
  4. Put the case back on the Pi.
  5. Connect the cables to the DHT22, as noted above.
  6. Plug the power back into the Pi and turn on.

Install the Software

Let’s make sure the sensor is working.  In order to use this or any sensor, you’ll need to install software (a python library) that can read and interpret the data coming from the sensor. In this case, we’ll install the Adafruit_DHT library and then write some short Python code to output the sensor data.

From here, I assume you have any version of Python 3 installed.  It’s installed by default, so unless you removed it, it should be a safe assumption.

Start Terminal. By default, this launches a black window with the bash prompt.  At that prompt, tell Python to add the code to read the sensor data. Text is case-sensitive.

  • pip3 install Adafruit_DHT

The script will download and install software and hopefully give you a confirmation everything went well. Techie note for conda users: The library doesn’t appear to be loaded into the Anaconda cloud.  Download and manually install it (like we did with arcgis, if you need the sensor exposed to your conda environment.

Test the Sensor

You need a Python IDE (integrated development environment), a place to write and run code. While most Pis already have at least one Python IDE, you may find it necessary to download one from: Start menu > Preferences > Recommended Software.

Thonny and Geeny were both pre-installed on my Pi and work fine for this task.  Python 3 IDLE is not advisable for this project. With Thonny open, create a new file and copy/paste the Python code below (or download the .py file from here). github

  • import Adafruit_DHT
  • sensor = Adafruit_DHT.DHT22
  • #This is the GPIO Pin number, not just the Pin number.
  • #This pin in pin 7 but sits in GPIO Pin 4. Use 4 below.
  • pin = 4
  • humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
  • temperature = temperature * 9/5.0 + 32
  • if humidity is not None and temperature is not None:
    • print(‘Temp={0:0.1f}*F  Humidity={1:0.1f}%’.format(temperature, humidity))
  • else:
    • print(‘Failed to get reading. Try again!’)


A few notes on the code:

  • Lines starting with # (hashtags) are comments.
  • The indentation on the two print lines is important. Just tab both lines in one time. This is how Python articulates logic and code “flow” (with tabs).
  • Note the import statement at the top.  This is where the Adafruit library is loaded by Python and starts the magic.
  • Most importantly, note the pin= line. This is the GPIO# not the actual pin #.

This sample script is derived from Adafruit’s script.  I’ve just simplified it a bit but you can find the original here.

If the script runs correctly, the output should look something like:

  • Temp=67.5*F  Humidity=40.1%

Extension Ideas

  • Put a for loop around your code and make it run repeatedly.
  • Put a time.sleep() function in the for loop to space out sampling events.
  • Log your data.
  • Calculate the heat index, based on the temp and humidity data.