Raspberry Pi Pico W with DHT11 (MicroPython Code)

In this tutorial, we will interface Raspberry Pi Pico W with the DHT11 temperature and humidity sensor using Micropython. We will discuss a simple script to view the DHT11 sensor data.

Raspberry Pi Pico also has an onboard temperature sensor. If you wish to use that sensor, you can refer to our article to read the onboard temperature sensor on Raspberry Pi Pico using MicroPython.

Overview of DHT11 Temperature & Humidity Sensor

DHT11 is an inexpensive temperature and humidity sensor that has gained popularity in the Arduino community. At the heart of the sensor is an NTC thermistor to sense the temperature and a resistive-type humidity sensor. The resistance of a Negative Temperature Coefficient(NTC) thermistor decreases as temperature increases. This property is used to estimate the temperature based on the output of a voltage divider circuit. The humidity sensor comprises a nonmetallic conductive substance(such as conductive polymers) sandwiched between two electrodes. The resistivity of such substances decreases as humidity increases.

However, we do not need to worry about the internal circuit of DHT11 while interfacing it with Rasberry Pi Pico. A chip inside the DHT11 reads the analog voltages from the temperature and humidity sensor and gives us a digital output that can be easily read by a microcontroller.

DHT11 Pinout

Here is the pin diagram of the DHT11 sensor:

Some DHT11 modules have resistors and an LED built into the module. Here are the pin diagrams of some common DHT11 modules:

DHT11 Specifications

Operating Voltage3V-5.5V
Humidity Range20% – 90%(RH)
Humidity Accuracy±4%RH
Temperature Range0 to 50 degrees Celcius
Temperature Accuracy±2°C maximum

Prerequisites

  • A Raspberry Pi Pico/ Pico W.
  • DHT11 module.
  • Breadboard and connecting wires.

Please ensure that the MicroPython UF2 file is uploaded to your Raspberry Pi Pico RP2040. If you are a beginner, we suggest that you take a look at our guide for getting started with Raspberry Pi Pico on Windows, where we discuss how to set up Raspberry Pi Pico with the UF2 file and transfer MicroPython scripts to RP2040. You can also follow our guide to program Raspberry Pi Pico on macOS using Thonny IDE.

Wiring Raspberry Pi Pico with DHT11

Connect your Pi Pico to DHT11 as shown in the diagram below. We use Pin1 (GPIO0) to get data from DHT11. You can use any pin you like with appropriate changes in your code.

If you use a 3-pin DHT11 module(such as the KY-015), your connection will be as shown below. However, verify that the DHT11 pins match. Some modules may have the pins swapped.

Although some breakout boards for DHT11 feature an onboard 10 KΩ resistor, you need to add one between the 3.3V pin and the data pin if your variant of the sensor does not have it.

Advertisement

DHT11 MicroPython Code

Let us write a simple MicroPython script to read data from DHT11 and print it in the shell of IDE. The following steps are demonstrated using Thonny IDE.

Open Thonny IDE, and create a new project by clicking File>New. Copy and paste the following script in the blank editor space.

#Code and circuit tutorial on Electrocredible.com

from machine import Pin
import time
import dht 

dSensor = dht.DHT11(Pin(0))

def readDHT():
    try:
        dSensor.measure()
        temp = dSensor.temperature()
        temp_f = (temp * (9/5)) + 32.0
        hum = dSensor.humidity()
        print('Temperature= {} C, {} F'.format(temp, temp_f))
        print('Humidity= {} '.format(hum))
    except OSError as e:
        print('Failed to read data from DHT sensor')
    
while True:
    readDHT()
    time.sleep(1)
Code language: Python (python)

Save the script to your Pico by clicking on File>Save as>Raspberry Pi Pico.

Thonny Save to

Save the script with the file name main.py. When you run the script, you should see the following output in the Thonny shell.

Wrapping Up

I hope you found this DHT11 Raspberry Pi Pico interfacing guide to be helpful. DHT22 is a better temperature and humidity sensor than DHT11. DHT22 has a humidity measuring range of 0-100%. Also, DHT22 has better accuracy than DHT11. You can also check our article on interfacing DHT22 with Raspberry Pi Pico.

BME280 and BMP280 are more accurate atmospheric sensors than DHT11, with a wider range of operating conditions. BMP280 can sense temperature and pressure, while BME280 can sense temperature, pressure, and humidity. You can read about these sensors in our articles:

Please leave your suggestions and queries in the comments below. Thank you for reading.


Posted

in

by

Comments

One response to “Raspberry Pi Pico W with DHT11 (MicroPython Code)”

  1. Wonseok Lim Avatar
    Wonseok Lim

    Since new microPython include the dht library, we can read the value of dht very simply.
    dht.DHT() > measure() > temperture(), humdity()
    Please update your article with the newest version because I like your articles in this site and hope other people to learn some new information easily from your articles.

Leave a Reply

Your email address will not be published. Required fields are marked *