After I posted my Raspberry Pi server room rack temperature monitor project, I received a request to make a tutorial on how to measure humidity in addition to temperature with a Raspberry Pi. The following video demonstrates 2 methods for controlling a DHT22 sensor:
The 4 pin DHT22 sensor has 2.54mm spacing so it works with a standard breadboard. It can operate at 3.3V which makes it well suited for the Raspberry Pi.
The wiring is very simple. Please make sure you add the 10KΩ pull-up resistor between the 3.3V and the data line.
The Adafruit Python DHT Sensor library makes interfacing the DHT22 easy. Use git clone to install it from your home directory in a terminal window as follows
A reboot of the Pi is required for the Adafruit drivers to function properly.
Access to the GPIO requires superuser privileges. (Update: super-user privileges are no longer required for GPIO access with the latest version of Raspbian Jessie.) On older systems use gksu to open Idle with the necessary permissions:
To poll the sensor in Python:
An alternative DHT22 library is provided by pigpio which can be installed using the following commands from your home directory:
The pigpio DHT22 module can be downloaded from the examples page or just use wget:
Before using pigpio, you must start the daemon:
One advantage of this library is that once the daemon is started, you do not need special permissions in Python to poll the sensor. Make sure you unzip the pigpio DHT22 module to the same folder as your program. I noticed that on newer and faster Pi’s it is necessary to add a sleep delay of 10ms to allow the trigger command to complete.
The pigpio DHT22 module also supports an LED indicator which can be wired to any GPIO pin in series with the appropriate resistor:
One optional parameter is added to the sensor method to specify the GPIO pin of the LED:
The DHT22 also comes in a wired module branded as AM2302.
Please note that the DHT22 sensor requires a minimum of 2 seconds between calls. If you try to poll it more frequently, you may experience errors.