Built-in project · Raspberry Pi Pico

MicroPython DHT11 on a Pico: temperature and humidity

MicroPython on a Raspberry Pi Pico: a DHT11 on GP15 with a 10k pull-up reports temperature and humidity every two seconds through the dht module. The whole build, a Raspberry Pi Pico and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $8.05. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Reading a sensor with MicroPython's dht module
  • A pull-up resistor on a data line
  • Catching an error with try and except
  • Giving a sensor time to start up
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Raspberry Pi Pico
  • Parts: 5

How it works

The sensor's data wire goes to GP15, with a 10k pull-up resistor to 3.3 V. The sensor is powered from 3.3 V. MicroPython's dht module handles the timing needed to talk to it.

The program waits a second at power-up, then every 2 seconds it asks the sensor to measure and prints the temperature and humidity. If the sensor does not answer, it prints the error and tries again.

Parts list

5 parts, plus the jumper wires. They retail for about $8.05; on Mokxi they're free.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Temperature and humidity, DHT22 pin DATARaspberry Pi Pico pin GP15
Resistor, 10k Ω pin 2Raspberry Pi Pico pin GP15
Temperature and humidity, DHT22 pin VCC3.3 V
Temperature and humidity, DHT22 pin GNDGround
Resistor, 10k Ω pin 13.3 V

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · Raspberry Pi Pico
# A DHT11 on GP15 (with a 10k pull-up): temperature and humidity every
# two seconds, through MicroPython's own dht module.
from machine import Pin
import dht
import time

sensor = dht.DHT11(Pin(15))
time.sleep(1)                         # the sensor wants a moment at power-up

while True:
    try:
        sensor.measure()
        print("temperature %d C   humidity %d %%" % (sensor.temperature(), sensor.humidity()))
    except OSError as e:
        print("no answer from the DHT11:", e)
    time.sleep(2)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • It keeps printing no answer from the DHT11. This project
    Check the data wire on GP15, the 10k pull-up to 3.3 V, and power and ground.
  • The numbers update too slowly. This project
    The loop waits 2 seconds with time.sleep(2). A DHT11 is slow, so do not set it much lower.
  • Readings are NaN or fail, or never appear. Temperature and humidity
    Put a 10 k ohm pull-up between DATA and VCC (bare sensors need it). A 4-pin DHT22 has pin 3 unused. Check the data pin number in your code.
  • Values freeze or errors show up when you read often. Temperature and humidity
    The DHT22 can only be read about once every 2 seconds. Wait between reads, and check for NaN before using the numbers.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The Pico does not show up as a drive, or the program will not load. Raspberry Pi Pico
    Hold the BOOTSEL button while you plug in USB. A drive named RPI-RP2 appears. Then copy the UF2 file onto it.
  • A 5 V signal or sensor is wired to a GPIO pin. Raspberry Pi Pico
    Pico GPIO pins are 3.3 V only and are not 5 V tolerant. Use a level shifter or a voltage divider for any 5 V output going into the board.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.