DHT11  Temperature & Humidity Sensor and Raspberry Pi Pico example using micropython

In this article we connect the ever popular DHt11  Temperature & Humidity Sensor to a raspberry Pi Pico and we will then display the temperature and humidity to the repl window in Thonny using micropython

Now the DHT11 is not the fastest or most impressive temperature sensor but it is low cost and very common and is not that difficult to use.

We can use this as the start of a environmental example eventually displaying the readings on an LCD or even to a webpage or a data logging service like thingspeak

Lets look at the sensor first

Features

DHT11 Temperature & Humidity Sensor features a temperature & humidity sensor complex with a calibrated digital signal output. By using the exclusive digital-signal-acquisition technique and temperature & humidity sensing technology, it ensures high reliability and excellent long-term stability.

This sensor includes a resistive-type humidity measurement component and an NTC temperature measurement component, and connects to a high performance 8-bit microcontroller, offering excellent quality, fast response, anti-interference ability and cost-effectiveness.

The single-wire serial interface makes system integration quick and easy. Its small size, low power consumption and up-to-20 meter signal transmission making it the best choice for various applications, including those most demanding ones. The component is 4-pin single row pin package.

It is commonly available as a module which contains the recommended 5K pull-up resistor is recommended; when the connecting cable is longer than 20 metres, choose a appropriate pull-up resistor as needed.

If you simply purchase a component rather than a module with a built-in resistor you will need to add one like this

 

 

Parts Required

 

Name Link
Pico Raspberry Pi Pico Development Board
DHT11

Aliexpress link

Connecting cables Aliexpress product link

Lysee 3D Printer Parts & Accessories – AHT20 Temperature and Humidity Sensor Module DHT11 Upgrade I2C XD Humidity Sensor Probe – (Color: Green)

Ebay link

 

Schematic/Connection

To connect the display to your Raspberry PI Pico requires just 3 wires, you just need to wire the Vcc and GND PINs from display to VBuS and a GND PINs of RPI Pico, then th Data pin from the module to suitable pin from your Raspberry PI Pico, in this case we use pin 16

Here is a layout in fritzing to show this

Code Example

I use thonny for all development

You will need to copy the dht library to your Pico or similar Rp2040 board

Here it is, download and unzip the python file – DHT11 library

Now for the code example

import utime
from dht import DHT11
import os

from machine import Pin

def main():

    sensor = DHT11(Pin(16, Pin.OUT, Pin.PULL_DOWN))

    while True:
        utime.sleep(1)

        sensor.measure()
        t  = sensor.temperature()
        h = sensor.humidity()
        
        print("Temperature: {}".format(t))
        print("Humidity: {}".format(h))

if __name__ == "__main__":
    main()

In the repl window I saw this is

>>> %Run -c $EDITOR_CONTENT
Temperature: 25.2
Humidity: 85.0
Temperature: 24.5
Humidity: 45.0
Temperature: 24.5
Humidity: 44.0
Temperature: 24.5
Humidity: 45.0
Temperature: 24.3
Humidity: 45.0
Temperature: 24.6
Humidity: 45.0

Links

DHT11 library

Related posts

A Raspberry Pi Pico and KY-006 Passive Buzzer

A Raspberry Pi Pico and KY-012 Active Buzzer

A Raspberry Pi Pico and KY-032 Obstacle Detector

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More