Raspberry Pi - DHT22

This tutorial instructs you how to use Raspberry Pi to read the temperature and humidity from DHT22 sensor. In detail, we will learn:

We suggest:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×Temperature and Humidity Sensor DHT22
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of DHT22 Temperature and Humidity Sensor

The Temperature and Humidity Sensor Pinout

The DHT22 on the market comes with two forms: sensor and module.

The DHT22 sensors in their original form have four pins:

  • GND: This pin must be connected to ground (0V)
  • VCC: This pin must be connected to the voltage supply (5V)
  • DATA: This pin is used for communication between the sensor and Raspberry Pi
  • NC: This pin should not be connected and can be ignored
DHT22 temperature and humidity sensor pinout

The DHT22 module has three pins:

  • GND pin (-): must be connected to GND (0V)
  • VCC pin (+): must be connected to VCC (5V)
  • OUT pin: used for communication between the sensor and Raspberry Pi

※ NOTE THAT:

The arrangement of pins on a module can differ between manufacturers. It is essential to always use the labels printed on the module when working with it. Be sure to take a close look!

Wiring Diagram

Wiring to Raspberry Pi is the same for both sensors. In its original form, a resistor with a value between 5K and 10K Ohms is necessary to keep the data line high, thus enabling communication between the sensor and the Raspberry Pi.

Raspberry Pi - DHT22 Sensor Wiring

The wiring diagram between Raspberry Pi and DHT22 Temperature and humidity Sensor

This image is created using Fritzing. Click to enlarge image

Connect the DHT22 sensor to the Raspberry Pi as follows:

  • DHT22 VCC pin to Raspberry Pi 5V (pin 2 or any 5V GPIO pin)
  • DHT22 GND pin to Raspberry Pi GND (pin 30 or any GND GPIO pin)
  • DHT22 DATA pin to a GPIO pin on the Raspberry Pi (e.g., GPIO 12 (pin 32).

For better stability, you need to add a resistor (a value between 5K and 10K Ohms) between the VCC and DATA pins of the DHT22.

Raspberry Pi - DHT22 Module Wiring

Most DHT22 sensor modules come with an integrated resistor, thus eliminating the need for additional wiring or soldering.

The wiring diagram between Raspberry Pi and DHT22 Temperature and humidity Module

This image is created using Fritzing. Click to enlarge image

Connect the DHT22 module to the Raspberry Pi as follows:

  • DHT22 VCC pin to Raspberry Pi 5V (pin 2 or any 5V GPIO pin)
  • DHT22 GND pin to Raspberry Pi GND (pin 30 or any GND GPIO pin)
  • DHT22 DATA pin to a GPIO pin on the Raspberry Pi (e.g., GPIO 12 (pin 32).

How To Program for Raspberry Pi to read Temperature and Humidity from DHT22 Sensor

  • Open the terminal on your Raspberry Pi or connect via SSH.
  • Update the package list and install the required libraries by running the following commands:
sudo apt-get update sudo apt-get install python3-dev python3-pip sudo pip3 install Adafruit_DHT
  • Now, you can write a Python script for Raspberry Pi to read data from the DHT22 sensor.
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-dht22 import Adafruit_DHT import time # Set the sensor type and GPIO pin sensor = Adafruit_DHT.DHT11 pin = 17 # Change this to the GPIO pin you used for DATA try: while True: # Try to read the temperature and humidity from the sensor humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) # If the reading was successful, display the values on the same line if humidity is not None and temperature is not None: print(f"Temperature: {temperature:.1f} °C, Humidity: {humidity:.1f} %") else: print("Failed to retrieve data from the DHT22 sensor.") # Wait some time before taking the next reading time.sleep(2) except KeyboardInterrupt: print("\nExiting...")

Save the Python code in a file (e.g., dht22.py) on your Raspberry Pi. Then, execute the script in the terminal:

python3 dht22.py
  • Check the results on the terminal.
PuTTY - Raspberry Pi
Temperature: 25.4 °C, Humidity: 52.7 % Temperature: 25.7 °C, Humidity: 52.1 % Temperature: 26.1 °C, Humidity: 52.3 % Temperature: 25.9 °C, Humidity: 52.3 %

The script continuously reads the sensor data every 2 seconds (as specified in the time.sleep(2) line), so you will see updated readings like this every 2 seconds until you stop the script by pressing Ctrl + C keys in the terminal.

If, for any reason, the script fails to read data from the sensor, it will display:

PuTTY - Raspberry Pi
Failed to retrieve data from the DHT22 sensor.

Additional Knowledge

Let's compare DHT11 and DHT22 sensors.

The commons between DHT11 and DHT22

  • Providing the temperature and humidity information.
  • Pinouts remain the same.
  • The wiring to Raspberry Pi is unchanged.
  • Programming, utilizing a library, is comparable with only one line of code being different.

The differences between DHT11 and DHT22

DHT11 DHT22
Price ultra low cost low cost
Temperature Range 0°C to 50°C -40°C to 80°C
Temperature Accuracy ± 2°C ± 0.5°C
Humidity Range 20% to 80% 0% to 100%
Humidity Accuracy 5% ± 2 to 5%
Reading Rate 1Hz (once every second) 0.5Hz (once every 2 seconds)
Body size 15.5mm x 12mm x 5.5mm 15.1mm x 25mm x 7.7mm

Evidently, the DHT22 is more precise than the DHT11 and has a broader range, though it is pricier.

Video Tutorial