Raspberry Pi Tutorial: How to Use the DHT-22

Introduction: Raspberry Pi Tutorial: How to Use the DHT-22

The DHT-22 (also named as AM2302) is a digital-output relative humidity and temperature sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin. In this tutorial you will learn how to use the DHT-22 sensor with Raspberry Pi.

Tutorial updates and more Raspberry Pi tutorials can be found here:

Let's get started!

Step 1: About the DHT-22 Sensor

The DHT22 is a basic, low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin (no analog input pins needed). Connections are simple, the first pin on the left to 3.3V power, the second pin to your data input pin and the right most pin to ground.

Technical details:

  • Power: 3-5V
  • Max Current: 2.5mA
  • Humidity: 0-100%, 2-5% accuracy
  • Temperature: -40 to 80°C, ±0.5°C accuracy

Step 2: What You Will Need - Hardware

Step 3: The Circuit

The connections are pretty easy, see the image above with breadboard circuit schematic.

Step 4: Install Adafruit DHT Library

Before python code you need to download and install the DHT library in your Raspberry Pi. Open the terminal window and type:

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

Now you will have to reboot your Pi system to get the Adafruit driver.

Step 5: Python Code

2 3 4 5 6 7 8 9 10 11
import Adafruit_DHT as dht
from time import sleep
#Set DATA pin
DHT = 4
while True:
    #Read Temp and Hum from DHT22
    h,t = dht.read_retry(dht.DHT22, DHT)
    #Print Temperature and Humidity on Shell window
    print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(t,h))
    sleep(5) #Wait 5 seconds and read again

Download the code from here and open it with Thonny Python IDE or run it from terminal.

Step 6: Well Done

You have successfully completed one more Raspberry Pi "How to" tutorial and you learned how to use the DHT-22 sensor.

I hope you liked this, let me know in the comments.

Video in Greek language

2 People Made This Project!


  • Game Design: Student Design Challenge

    Game Design: Student Design Challenge
  • Make It Bridge

    Make It Bridge
  • Big and Small Contest

    Big and Small Contest



Question 17 days ago on Introduction

1. What is the purpose of the 10K resistor?
2. Pin 3? is left unconnected. Does it have a purpose?


11 months ago

I've made it, but it doesn't work. And I'm sure its not the
instructable that's incorrect. I've tried multiple versions of code to
read this sensor yet I can never get a reading off of it. I've tried
two different sensors, both brand new, I've tried with and without the
10k resistor, all the same results. So when I find my error it will be a
glorious day! (-: Thanks for the instructable.

Traceback (most recent call last):
File "dht22tutorial.py", line 10, in <module>
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(t,h))
TypeError: unsupported format string passed to NoneType.__format__
pi@pi-dev4:~/source-code/python $



Question 1 year ago on Step 6

Can the same code pattern be used with the DHT11?
Thans for the post.


Question 2 years ago on Step 5

How do I fix the following error:

from . import Beaglebone_Black_Driver as driver
ImportError: cannot import name 'Beaglebone_Black_Driver' from 'Adafruit_DHT' (/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/__init__.py)

Used your code above exactly.

Above code works on RPi 3b but not on new build of Rpi 4 "BUSTER"

I've attempted 2 rebuilds from scratch and end up in the same place.

I have tried to edit the platform_detect.py file but I dont have write credentials to edit the file. I dont know how to cure that either.

(In "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/platform_detect.py", you can add the followings at line #112 in the elif ladder, so it should workaround the issue.
elif match.group(1) == 'BCM2711':
return 3}


Question 2 years ago

Looking for code to display the readings to a 16 x 2 lcd screen that is using an i2c setup with the current adafruit library. (2020) The one listed on git above is depreciated.