Built-in project · Raspberry Pi Pico W

Pico W DHT11 temperature and humidity on GP16

MicroPython on a Pico W: the dht module reads a DHT11 on GP16 and prints the temperature and humidity every two seconds, retrying a failed read. The whole build, a Raspberry Pi Pico W and 1 more part, 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 $10. 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
  • Catching an error with try and except
  • Sensors need time between readings
  • Formatting printed numbers
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 3

How it works

The sensor's data wire goes to GP16, and it is powered from 3.3 V. MicroPython's dht module handles the timing needed to talk to it.

The program waits a second at the start, then every 2 seconds it asks the sensor to measure and prints the temperature and humidity. If a reading fails, it prints a message and tries again.

Parts list

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

Kits that can build it

These 2 starter kits have 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 W pin GP16
Temperature and humidity, DHT22 pin VCC3.3 V
Temperature and humidity, DHT22 pin GNDGround

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 W
# Temperature and humidity: a DHT11 on GP16. MicroPython's dht module
# reads it; a DHT11 gives a new reading about once a second, so read
# every few seconds and try again if a reading fails.
from machine import Pin
import dht
import time

sensor = dht.DHT11(Pin(16))
time.sleep(1)

while True:
    try:
        sensor.measure()
        print("Temperature: %d C  Humidity: %d %%" % (sensor.temperature(), sensor.humidity()))
    except OSError:
        print("no reading, trying again")
    time.sleep(2)

Troubleshooting

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

  • It prints no reading, trying again over and over. This project
    Check the data wire on GP16, and that power goes to VCC and ground to GND.
  • Readings come too fast and fail. This project
    Keep the pause in time.sleep(2). A DHT11 gives a new reading only about once a second.
  • 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.
  • Pins are powered from 5 V, or a 5 V signal goes into a GPIO. The RP2040 pins are 3.3 V only and are not 5 V tolerant. Raspberry Pi Pico W
    Power the board from VBUS or VSYS at 5 V, but keep GPIO signals at 3.3 V. Use a level shifter or a divider for 5 V parts.
  • WiFi code fails to connect, or the on-board LED code does nothing. The LED on the Pico W is not on GP25 like on the plain Pico. Raspberry Pi Pico W
    Use the board's LED_BUILTIN name so the library drives it through the WiFi chip. Join a 2.4 GHz network only, with the right SSID and password, and wait for the link before sending.

Change it and keep it

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