DHT11 on a Pico with MicroPython
MicroPython’s built-in dht module on a Raspberry Pi Pico: a DHT11 on GP15 prints the temperature and humidity every two seconds, and the Pico’s LED blinks. The whole build, a Raspberry Pi Pico 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 Runs in your browser. Free, and no account needed.
The parts retail for about $8. On Mokxi they're free.
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.
# A DHT11 on a Raspberry Pi Pico, in MicroPython.
# The DHT11 module runs from 3V3 (pin 36) and ground (pin 38), with DATA on
# GP15 (pin 20); the module has its own pull-up on DATA.
from machine import Pin
import dht
import time
sensor = dht.DHT11(Pin(15))
led = Pin("LED", Pin.OUT) # the Pico's own LED blinks with each reading
time.sleep(1) # the DHT11 answers nothing for a second after power-up
while True:
led.toggle()
try:
sensor.measure()
print("Temperature: %d C Humidity: %d %%" % (sensor.temperature(), sensor.humidity()))
except OSError as e:
print("Could not read the DHT11:", e)
time.sleep(2) # the DHT11 needs a second or more between readings
Parts list
2 parts, plus the jumper wires. They retail for about $8; on Mokxi they're free.
How it is wired
3 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Raspberry Pi Pico pin GND; Temperature and humidity, DHT22 pin GND
- Raspberry Pi Pico pin GP15; Temperature and humidity, DHT22 pin DATA
- Raspberry Pi Pico pin 3V3; Temperature and humidity, DHT22 pin VCC
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.