This guide shows how to read temperature and humidity from the DHT11 or DHT22 sensors using the Raspberry Pi Pico board with MicroPython firmware.
Do you prefer using Arduino IDE? Check the following tutorial: Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (Arduino IDE).
Table of Contents
- Prerequisites – MicroPython Firmware
- Introducing the DHT11 and DHT22 Sensors
- Wiring the DHT Sensor to the Raspberry Pi Pico
- Raspberry Pi Pico Getting Temperature and Humidity from DHT Sensor
DHT Module Usage
There is a dht module that comes with the MicroPython firmware by default, which makes it pretty straightforward to get temperature and humidity from the DHT11 or DHT22 temperature sensors.
1. Start by importing the dht and machine modules:
import dhtfrom machine import Pin
2. Create a dht object that refers to the sensor’s data pin, in this case, it’s GPIO 22:
#sensor = dht.DHT11(Pin(22))sensor = dht.DHT22(Pin(22))
3. To measure and read the sensor values, use:
sensor.measure() sensor.temperature()sensor.humidity()
Continue reading for more information and the detailed tutorial.
Prerequisites – MicroPython Firmware
To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board.
The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.
- Programming Raspberry Pi Pico using MicroPython
Introducing the DHT11 and DHT22 Sensors
The DHT11 and DHT22 sensors are used to measure temperature and relative humidity. These are very popular among makers and electronics hobbyists.
These sensors contain a chip that does analog to digital conversion and spit out a digital signal with the temperature and humidity. This makes them very easy to use with any microcontroller.
DHT11 vs DHT22
The DHT11 and DHT22 are very similar, but differ in their specifications. The following table compares some of the most important specifications of the DHT11 and DHT22 temperature and humidity sensors. For a more in-depth analysis of these sensors, please check the sensors’ datasheet.
Temperature range | 0 to 50 ºC +/-2 ºC | -40 to 80 ºC +/-0.5ºC |
Humidity range | 20 to 90% +/-5% | 0 to 100% +/-2% |
Resolution | Humidity: 1% Temperature: 1ºC | Humidity: 0.1% Temperature: 0.1ºC |
Operating voltage | 3 – 5.5 V DC | 3 – 6 V DC |
Current supply | 0.5 – 2.5 mA | 1 – 1.5 mA |
Sampling period | 1 second | 2 seconds |
Price | $1 to $5 | $4 to $10 |
Where to buy | Check prices | Check prices |
The DHT22 sensor has a better resolution and a wider temperature and humidity measurement range. However, it is a bit more expensive, and you can only request readings with 2 seconds interval.
The DHT11 has a smaller range and it’s less accurate. However, you can request sensor readings every second. It’s also a bit cheaper.
Despite their differences, they work in a similar way, and you can use the same code to read temperature and humidity. You just need to select in the code the sensor type you’re using.
DHT Pinout
DHT sensors have four pins as shown in the following figure. However, if you get your DHT sensor in a breakout board, it comes with only three pins and with an internal pull-up resistor on pin 2.
The following table shows the DHT22 and DHT11 pinout. When the sensor is facing you, pin numbering starts at 1 from left to right
DHT pin | Connect to |
1 | 3.3V |
2 | Any digital GPIO; also connect a 10k Ohm pull-up resistor |
3 | Don’t connect |
4 | GND |
DHT Sensor Breakout Board
If you got a DHT11 or DHT22 sensor on a breakout board, they only come with three pins and have an internal pull-up resistor on the data pin.
In this case, wiring is even simpler and you don’t need to wire an external resistor. The DHT breakout boards usually have labels on the pins: GND, VCC, and DATA.
DHT pin | Connect to |
GND | GND |
VCC | 3V3(OUT) |
DAT | GPIO 22 (or any other digital pin) |
Parts Required
Here’s a list of parts you need to build the circuit (if you don’t have a DHT breakout board, you need a 4.7kOhm resistor):
- Raspberry Pi Pico
- DHT11 or DHT22 temperature and humidity sensor
- 4.7k Ohm resistor or similar value (not needed if you have a DHT breakout board)
- Breadboard
- Jumper wires
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Wiring the DHT11/DHT22 to the Raspberry Pi Pico
Wire the DHT22 or DHT11 sensor to the Raspberry Pi Pico as shown in the following schematic diagram. If you have a DHT breakout board, ignore the resistor.
In this example, we’re connecting the DHT data pin to GPIO 22. However, you can use any other suitable digital pin.
You might also like reading: Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained.
Raspberry Pi Pico with DHT Sensor – Code
Open a new file in Thonny IDE or another MicroPython IDE of your choice and copy the following code.
# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-dht11-dht22-micropython/from machine import Pinfrom time import sleepimport dht sensor = dht.DHT22(Pin(22))#sensor = dht.DHT11(Pin(22))while True: try: sleep(2) sensor.measure() temp = sensor.temperature() hum = sensor.humidity() temp_f = temp * (9/5) + 32.0 print('Temperature: %3.1f C' %temp) print('Temperature: %3.1f F' %temp_f) print('Humidity: %3.1f %%' %hum) except OSError as e: print('Failed to read sensor.')
How the Code Works
Import the Pin class from the machine module to define pins, import the sleep method from the time module to add delays to your code, and finally import the dht module to import the functions to read from the DHT sensors.
from machine import Pinfrom time import sleepimport dht
Define a dht object called sensor on the specified data pin. In this case, we’re connecting the data pin to GPIO 22. Use the following command if you’re using a DHT22 sensor:
sensor = dht.DHT22(Pin(22))
Comment the previous line and uncomment the next one if you’re using a DHT11 sensor.
#sensor = dht.DHT11(Pin(22))
In the while loop, we use try and except statements. In the try statement, we try to get temperature and humidity values.
Note: try and except allow us to continue the execution of the program when an exception happens. For example, when an error occurs, the try block code execution is stopped and transferred to the except block. In our example, the exception is especially useful to prevent the board from crashing when we are not able to read from the sensor.
In the try statement, first, add a delay of two seconds because the DHT22 maximum sampling rate is two seconds. In the case of the DHT11, it is one second.
sleep(2)
Before requesting temperature and humidity, you need to use the measure() method on the sensor object.
sensor.measure()
Then, read the temperature with sensor.temperature() and the humidity with sensor.humidity(). Save those readings on the temp and hum variables.
temp = sensor.temperature()hum = sensor.humidity()
The following command converts the temperature to Fahrenheit degrees.
temp_f = temp * (9/5) + 32.0
Finally, print all the readings on the MicroPython shell using the print() function:
print('Temperature: %3.1f C' %temp)print('Temperature: %3.1f F' %temp_f)print('Humidity: %3.1f %%' %hum)
In case there is an error getting the readings, the except statement runs and an error message is printed:
except OSError as e: print('Failed to read sensor.')
Demonstration
Save the code to your Raspberry Pi Pico board using Thonny IDE or any other MicroPython IDE of your choice.
Copy the code provided to a new file on Thonny IDE.
With the code copied to the file, click on theSaveicon. Then, selectRaspberry Pi Pico.
Save the file with the following name:main.py.
Note: When you name a file main.py, the Raspberry Pi Pico will run that file automatically on boot. If you call it a different name, it will still be saved on the board filesystem, but it will not run automatically on boot.
Reset your board (unplug and plug it into your computer). Click the little green button “Run Current Script” or press F5.
New temperature and humidity readings will be published on the shell every two seconds.
Troubleshooting
If your DHT sensor fails to get the readings, read our DHT Troubleshooting Guide to help you fix the issue (even though it’s a troubleshooting guide for Arduino IDE, most issues are related to hardware, so most tips are useful regardless of the programming language you’re using).
Wrapping Up
In this tutorial, you learned how to interface the DHT11 and DHT22 temperature sensors with the Raspberry Pi Pico and how to get temperature and humidity readings using MicroPython firmware.
There’s a specific built-in MicroPython module to interface with DHT sensors, the dht module. After defining a dht object, you just need to use the temperature() and humidity() methods.
Learn more about the Raspberry Pi Pico with our eBook:
- Learn Raspberry Pi Pico with MicroPython (eBook)
We have other Raspberry Pi Pico basic tutorials that you might like reading:
- Getting Started with Raspberry Pi Pico (and Pico W)
- Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained
- Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (MicroPython)
- Raspberry Pi Pico: PWM Fading an LED (MicroPython)
>> Check out all our Raspberry Pi Pico Guides.
Thanks for reading.