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.54 mm spacing so it works with a standard breadboard. It can operate at 3.3 V which makes it well suited for the Raspberry Pi.
The wiring is very simple. Please make sure you add the 10 KΩ pull-up resistor between the 3.3 V and the data line.
The Adafruit Python DHT Sensor library makes interfacing the DHT22 easy. Please note that you should not use pip for installation because there are platform issues. Use git clone to install it from your home directory in a terminal window as follows. You can target Python 3 instead of Python 2 by changing python in the last line to python3.
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.) The Idle IDE no longer comes with the latest version of Raspbian. It has been replaced by the Thonny Python IDE. 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.
Please note that it is no longer necessary to install pigpio because it now comes included with the latest version of Raspbian.
But if you need to manually install it, you can use the following:
The DHT22.py library will need to be placed in the same folder as your program. Please make sure you unzip the file. It can be downloaded from the examples page or you can 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 10 ms 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.
LCD Display Example:
I received a question about maximum wire length between the Pi and the DHT22 sensor. The datasheet states the maximum wire length is 100 meters. I think this is very optimistic. Distances greater than 100 cm requires 5 V, so it is necessary to use a level shifter on the data line to lower the 5 V down to a Pi friendly 3.3 V. I recommend using a level shifter with a FET design such as the Adafruit 4 channel BSS138. I found the TXB104 shifters unreliable with the DHT22. I performed a test at 20 meters distance and got reliable results using CAT6E 550 MHz 23 AWG network cable (1 pair for 5 V and ground and another pair for data and ground.) I also lowered the resistor value down to 4.7 KΩ. Here’s a schematic:
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.
Please note this is an old tutorial and there are many more sensors available. I really like the Adafruit Si7021 which is reliable and easy to connect because of it I2C protocol.