Skip to content

uPesy ships directly only in France !

uPesy products available on Amazon for EU

Contents Menu Expand Light mode Dark mode Auto light/dark mode

How to use the DHT22 sensor with the Raspberry Pi Pico board in Python?

(Updated at 01/05/2023)

The DHT22 is an advanced version of the famous and inexpensive temperature and humidity sensor DHT11, used in many DIY projects. It offers greater accuracy than its predecessor but has the identical drawback: few measurements per second: only 1 measurement every 2 seconds. Despite this, it is perfectly suitable for creating a DIY IoT weather station.

DHT22 sensor overview

The DHT22 sensor with its white case

Note

The DHT22 is sometimes referred to as AM2302

Getting started with the DHT22 sensor

The DHT22 is part of the DHTxx family. A comparison between the DHT11 and the DHT22 is available to see their differences better. The DTH22 has a white plastic housing, while the DHT11 has a blue one. If your sensor is blue, I recommend reading the DHT11 tutorial .

Some characteristics of the DHT22 sensor

The DHT22 can be directly powered by 3.3V, which is very convenient for the Pi Pico, which operates on 3.3V.

Technical characteristics of DHT22

Features

DHT22

Temperature accuracy

± 0.5°C

Humidity accuracy

± 2%

Temperature range

-40-80 °C

Humidity range

0-100%

Sampling

0.5/s

Supply voltage

3-6V

Current

~1.5mA

Note

The DHT22 is a better choice for monitoring temperatures outdoors than the DHT11 since it can measure temperatures down to -40°C.

The DHT22’s performance isn’t as good as that of Bosh’s BMExxx sensors, which makes it suitable for specific applications but not others. It all depends on what you need the sensor to do.😉

Connections of the DHT22 sensor

The DHT22 sensor typically has 4 pins, although some modules may expose only 3. It is essential to be aware that the pin-out of the DHT22 module will vary depending on the manufacturer.

When it comes to numbering the pins of a sensor, you should begin counting from the left while holding the sensor so that the grid is facing you. The first pin is always the power supply, while the others may vary.

Pin Wiring

DHT22 Module

RPi Pi Pico

1 (VCC )

3V3

2 (GND )

GND

3 (OUT )

GPIO13

Pin Wiring

DHT22 Module

RPi Pi Pico

1 (+ )

3V3

2 (OUT )

GPIO13

3 (- )

GND

Pin Wiring

DHT22 Module

RPi Pi Pico

1

3V3

2

GPIO13

3

Do not connect

4

GND

The DHT22 is a temperature and humidity sensor with only one pin to transmit data in addition to the power supply.

Wiring diagram for using the DHT22 module with an RPi Pi Pico

If your module does not include a pull-up resistor, adding one between the pins 3V3 and the signal (GP13 ) is necessary. The recommended range for this resistor is from 4.7kΩ to 10kΩ. Any output pin can be used, but in this example, we are using the GP13 . Make sure to adjust the circuit to your model if it differs slightly. 🙂

dht22 Raspberry Pi Pico circuit Fritzing

Not easy to fix the pins 😕

dht22 upesy rp2040 board circuit Fritzing

It’s easier when the number is indicated 😉

Here is an example with the uPesy RP2040 DevKit board, an upgraded Pico and a DHT22 module with the signal pin on the far right:

Wiring on DHT22 and RP2040 breadboard

Wiring the DHT22 with a uPesy RP2040 DevKit board

Measuring the temperature and humidity on the DHT22 module with MicroPython

Since DHT22 uses a proprietary protocol, it is necessary to use a library to communicate with the sensor. Fortunately, such a library is included in the latest versions of MicroPython, so you don’t need to download an additional one. You can directly import the module with import dht .

from machine import Pin
from time import sleep
import dht

sensor = dht.DHT22(Pin(13))

while True:
  try:
    sleep(1)     # the DHT22 returns at most one measurement every 2s
    sensor.measure()     # Recovers measurements from the sensor
    print(f"Temperature : {sensor.temperature():.1f}°C")
    print(f"Humidite    : {sensor.humidity():.1f}%")
  except OSError as e:
    print("Failed reception")

In the Thonny IDE terminal, you will see the temperature expressed in °C and the humidity in percentage.

upython  DHT22 with Pico on thonny IDE

In Python, you can use f-string to display variables with a text, for example, f"Humidite: {sensor.humidity():.1f}" . The string must start with an f , and the variable must be placed between square brackets. It is possible to specify the number of digits after the decimal point, which will be displayed with :.1f . Here, we put a figure after the decimal point because the sensor is accurate to 0.5°C.

Calculate the heat index | Real feel temperature

The heat index can calculate the temperature felt. It is a value in °C based on the temperature and the relative humidity in the air. The higher the humidity level in the air at a given temperature, the higher the temperature felt

The formula is a bit long 😨 :

temperature felt formula

Formula taken from Wikipedia

Fortunately, I have made a ready-made function that takes care of it: 😉

from machine import 
   
   
 

  
  
  
  
  
  
     
     
     


  


  
      
      

     
        
           
           
             
             
             
               
               
                 
    


 
    
          
          

          
          
           

        
        
        
       

This section is available to premium members only. You still have 67% to discover.

Subscribe for only 5$/month

Already subscribed? Sign in