Connecting Temperature and Humidity Sensor - DHT22

This article is about setting up your raspberry pi and connecting a temperature / humidity sensor to it. I have used this DHT22 to read the temperature and humididty of my greenhouse to monitor when I should open and close windows and even kick on a fan if necessary to help my plants grow in the best environment they are suitable for. Regardless of your application, I will show you how to wire up your DHT22 sensor and give you some sample code to get started. If your pi is new and fresh out of the box, follow my Quick setup guide

What you will need

Step one: Wire the connections

  • Following the wiring diagram above, make all the connections between your pi and the DHT22 temperature and humidity sensor:
  • 1) Pin 1 connects to a 3.3V source
  • 2) Pin 2 connects to the desired GPIO pin (I am using gpio 24)
  • 3) Pin 3 is not used
  • 4) Pin 4 connects to the ground
  • 5) Make sure to add the 10K Ohm pull-up resistor between the 3.3V and the data line.

Step two: Download and install the Adafruit DHT Library

The Adafruit Python DHT Sensor library comes with exmples and makes working with the DHT22 very easy.

  • cd ~
  • git clone https://github.com/adafruit/Adafruit_Python_DHT.git
  • cd Adafruit_Python_DHT
  • sudo apt-get update
  • sudo apt-get install build-essential python-dev
  • sudo python setup.py install

Step three: Test the sensor out!

  • cd /home/pi/Adafruit_Python_DHT/examples
  • sudo ./AdafruitDHT.py 22 24

You should see something like below (note that 24 is the gpio we used):