Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (MicroPython) | Random Nerd Tutorials (2024)

This guide shows how to read temperature and humidity from the DHT11 or DHT22 sensors using the Raspberry Pi Pico board with MicroPython firmware.

Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (MicroPython) | Random Nerd Tutorials (1)

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.

Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (MicroPython) | Random Nerd Tutorials (2)

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.

DHT11
DHT22
Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (MicroPython) | Random Nerd Tutorials (3)
Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (MicroPython) | Random Nerd Tutorials (4)
Temperature range0 to 50 ºC +/-2 ºC-40 to 80 ºC +/-0.5ºC
Humidity range20 to 90% +/-5%0 to 100% +/-2%
ResolutionHumidity: 1%
Temperature: 1ºC
Humidity: 0.1%
Temperature: 0.1ºC
Operating voltage3 – 5.5 V DC3 – 6 V DC
Current supply0.5 – 2.5 mA1 – 1.5 mA
Sampling period1 second2 seconds
Price$1 to $5$4 to $10
Where to buyCheck pricesCheck 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.

Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (MicroPython) | Random Nerd Tutorials (5)

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 pinConnect to
13.3V
2Any digital GPIO; also connect a 10k Ohm pull-up resistor
3Don’t connect
4GND

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.

Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (MicroPython) | Random Nerd Tutorials (6)

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 pinConnect to
GNDGND
VCC3V3(OUT)
DATGPIO 22 (or any other digital pin)

Parts Required

Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (MicroPython) | Random Nerd Tutorials (7)

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):

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!

Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (MicroPython) | Random Nerd Tutorials (8)

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.

Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (MicroPython) | Random Nerd Tutorials (9)

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.')

View raw code

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.

Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (MicroPython) | Random Nerd Tutorials (10)

With the code copied to the file, click on theSaveicon. Then, selectRaspberry Pi Pico.

Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (MicroPython) | Random Nerd Tutorials (11)

Save the file with the following name:main.py.

Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (MicroPython) | Random Nerd Tutorials (12)

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.

Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (MicroPython) | Random Nerd Tutorials (13)

New temperature and humidity readings will be published on the shell every two seconds.

Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (MicroPython) | Random Nerd Tutorials (14)

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.

Raspberry Pi Pico: DHT11/DHT22 Temperature and Humidity Sensor (MicroPython) | Random Nerd Tutorials (2024)

FAQs

Which is better DHT22 or DHT11? ›

Humidity Accuracy: DHT11 has a humidity accuracy of ±5%, in contrast to DHT22, which is ±2%. Cost: The cost of DHT11 is $5.90 compared to that of DHT22, which is $9.90. In conclusion, in all aspects, the DHT22 beats the DHT11. This includes humidity accuracy, humidity range temperature accuracy, and temperature range.

How accurate is DHT22 temperature and humidity sensor? ›

The DHT22 is the more expensive version which obviously has better specifications. Its temperature measuring range is from -40 to +125 degrees Celsius with +-0.5 degrees accuracy, while the DHT11 temperature range is from 0 to 50 degrees Celsius with +-2 degrees accuracy.

Why is my DHT22 sensor not working? ›

DHT sensor is fried or broken

Unfortunately, these cheap sensors sometimes look totally fine, but they are fried/broken. So, even though you assembled the right circuit and code, it will still fail to get the readings. Try to use a different sensor to see if it fixes your problem.

How accurate is the DHT11 temperature and humidity sensor? ›

The sensor is an economical peripheral [1]. It is capable of measuring relative humidity between 20 and 80% RH within the operating temperature range of 0 to 50°C with an accuracy of ± 5% RH. Temperature is also measured in the range of 0 to 50°C with an accuracy of ± 2°C.

How do I know if DHT11 is working? ›

Best way that I followed was to blow warm air from mouth on to DHT11, trust me you'll see the readings of relative humidity change for a few seconds and then getting back to normal showing the room's relative humidity. Change in the readings indicate that the sensor is flawless.

What are the disadvantages of the DHT22 sensor? ›

DHT22 Sensor

Advantages: Higher accuracy and wider range, making it suitable for more demanding applications. Disadvantages: Slightly higher cost compared to DHT11.

Does DHT22 need calibration? ›

Though the DHT22 sensor has been widely used for several studies [7,9,10,11]. For this reason, calibration is needed for this sensor in local conditions by utilizing ASTM E337-84 as the basic principle used as a reference for calibration.

What are the disadvantages of humidity sensors? ›

Disadvantages of Resistive Humidity Sensors
  • Resistive Humidity Sensors are sensitive to chemical vapors and other contaminants.
  • The output readings may shift if used with water soluble products.

Does DHT22 need a resistor? ›

sensors with ESPHome. The DHT22 and DHT11 require external pull up resistors on the data line. To do this, solder a resistor with about 4.7kΩ (anything in the range from 1kΩ to 10kΩ probably works fine, but if you're having issues try the 4.7kΩ recommended by the manufacturer) between DATA and 3.3V .

Is DHT22 3V or 5V? ›

A DHT22 can normally be powered by anything from 3.3 to 6V (5.5V on some spec sheets). Where did you read that is it has a range of 2-3V? Also, your D1 Mini has a regulator on board that will take up to 5.5V and supply 3.3V to the ESP.

Why do humidity sensors fail? ›

If a sensor gets exposed to chemicals, like floor wax, a decrease in sensitivity usually happens. The substances inside occupy space that water would otherwise be able to get to. That results in measurements that are lower at high humidity. In short, the readings are inaccurate and cannot be relied on.

How do you use a DHT11 temperature and humidity sensor? ›

  1. Step 1: Connect DHT11 to Arduino. Connect DHT11 to Arduino using jumper cables. ...
  2. Step 2: Add the DHT Library. Library DHT can be download here: ...
  3. Step 3: Choose Arduino Board. Goto Tools and adjust the arduino board to the picture above. ...
  4. Step 4: Program. Include this code to read the value from the DHT11 sensor. ...
  5. Step 5: Result.

How do I connect my Raspberry Pi to a temperature sensor? ›

Connect pin 1 to the ground GPIO pin (labelled GND on the AdaFruit connector). Connect pin 2 to the GPIO pin 4 (labelled #4 on the AdaFruit connector). Put the 4.7kΩ resistor between pin 2 and pin 3 of the temperature sensor. Turn the Pi on, then put your finger against the sensor.

Which library is used to integrate DHT11 with Raspberry Pi? ›

The Adafruit Python DHT Sensor library is created to read the Humidity and Temperature on Raspberry Pi or Beaglebone Black. It is developed for DHT series sensors like DHT11, DHT22, or AM2302.

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Barbera Armstrong

Last Updated:

Views: 5662

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.