MicroPython on the Pico W ships with a dht driver, so reading a DHT22 takes about ten lines and no libraries.
Wire the sensor to 3V3 and GP15 (physical pin 20). The script measures every two seconds and prints the result; if the sensor doesn’t answer, it reports the error and tries again instead of crashing.
Wiring
| Raspberry Pi Pico W | DHT22 (AM2302) | Wire | Notes |
|---|---|---|---|
| 3V3 | 1 VCC | red | 3V3 is physical pin 36 |
| GP15 | 2 DATA | yellow | Physical pin 20 |
| 3V3 | 2 DATA | red | Via 10 kΩ — Pull-up for the bare 4-pin sensor. 3-pin breakouts already have it — skip this wire. |
| GND | 4 GND | black |
3.3–6 V part, 3.3 V board. Works at 3.3 V and 5 V. Power it from the same voltage as your board (3.3 V on ESP32 and Pico, 5 V on Uno), so the data line never exceeds the GPIO limit.
Board quirks
- Use 3V3 (pin 36), not VBUS or VSYS — those carry 5 V on USB, and the Pico’s pins are 3.3 V only.
- The
dhtmodule is built into MicroPython for the Pico W; there is nothing to install. - Save the file as
main.pyon the board so it runs at power-up without Thonny.
Code
# DHT22 temperature + humidity on a Raspberry Pi Pico W (MicroPython).
# DATA -> GP15, VCC -> 3V3 (pin 36), GND -> GND, 10 kOhm pull-up DATA -> 3V3 (breakouts have one).
from machine import Pin
import dht
import time
sensor = dht.DHT22(Pin(15))
print("DHT22 on GP15 - first reading in 2 s")
while True:
time.sleep(2) # the DHT22 can't be read faster than once every 2 s
try:
sensor.measure()
except OSError:
print("Failed to read from DHT sensor - check wiring and pull-up")
continue
t = sensor.temperature()
h = sensor.humidity()
print("Temp {:.1f} C Humidity {:.1f} %".format(t, h))# DHT22 temperature + humidity on a Raspberry Pi Pico W (MicroPython).
# DATA -> GP15, VCC -> 3V3 (pin 36), GND -> GND, 10 kOhm pull-up DATA -> 3V3 (breakouts have one).
from machine import Pin
import dht
import time
sensor = dht.DHT22(Pin(15))
print("DHT22 on GP15 - first reading in 2 s")
while True:
time.sleep(2) # the DHT22 can't be read faster than once every 2 s
try:
sensor.measure()
except OSError:
print("Failed to read from DHT sensor - check wiring and pull-up")
continue
t = sensor.temperature()
h = sensor.humidity()
print("Temp {:.1f} C Humidity {:.1f} %".format(t, h))Expected serial output
DHT22 on GP15 - first reading in 2 s
Temp 27.6 C Humidity 62.3 %
Temp 27.6 C Humidity 62.1 %DHT22 on GP15 - first reading in 2 s Temp 27.6 C Humidity 62.3 % Temp 27.6 C Humidity 62.1 %
Common errors
OSError: [Errno 110] ETIMEDOUT
The sensor did not answer. Check the pull-up on a bare sensor, that DATA is on GP15 (physical pin 20), and the 3V3 supply.
ImportError: no module named dht
The official MicroPython builds for every Raspberry Pi Pico board include dht, so you are on a very old or third-party firmware, or a file named dht.py on the board is hiding the built-in one. Reflash the RPI_PICO_W build from micropython.org and delete any dht.py you copied over.