DHT22 Tutorial for Raspberry Pi

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.

DHT22 Temperature & Humidity Sensor

The wiring is very simple.  Please make sure you add the 10KΩ pull-up resistor between the 3.3V and the data line.

DHT22 Wiring


The Adafruit software library makes interfacing the DHT22 easy.  Use git clone to install it from your home directory in a terminal window as follows

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


Taizo Hori A reboot of the Pi is required for the Adafruit drivers to function properly.

sudo reboot


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:

/usr/bin/gksu -u root idle


To poll the sensor in Python:

import Adafruit_DHT as dht
h,t = dht.read_retry(dht.DHT22, 4)
print 'Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(t, h)


An alternative DHT22 library is provided by pigpio which can be installed using the following commands from your home directory:

wget abyz.co.uk/rpi/pigpio/pigpio.zip
unzip pigpio.zip
sudo make install


The pigpio DHT22 module can be downloaded from the examples page or just use wget:

wget abyz.co.uk/rpi/pigpio/code/DHT22_py.zip


Before using pigpio, you must start the daemon:

sudo pigpiod


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.

import pigpio
from time import sleep
# this connects to the pigpio daemon which must be started first
pi = pigpio.pi()
# Pigpio DHT22 module should be in same folder as your program
import DHT22 
s = DHT22.sensor(pi, 4)
sleep(.01) # Necessary on faster Raspberry Pi's
print('{:3.2f}'.format(s.humidity() / 1.))
print('{:3.2f}'.format(s.temperature() / 1.))


The pigpio DHT22 module also supports an LED indicator which can be wired to any GPIO pin in series with the appropriate resistor:

DHT22 Wiring with LED


One optional parameter is added to the sensor method to specify the GPIO pin of the LED: 

# last parameter is LED GPIO pin
s = DHT22.sensor(pi, 4, 17)


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.

Mario Troubleshooting Tips.