MicroPython on Pico with sensors
Build it step by step
0 of 7 doneInstall MicroPython
Hold BOOTSEL, plug in the Pico, drag the MicroPython UF2 file onto the drive that appears.
Open Thonny
Choose the MicroPython (Raspberry Pi Pico) interpreter in the bottom right.
Blink from the REPL
Type from machine import Pin, then led = Pin(15, Pin.OUT) and led.toggle().
Wire the button and sensor
Button to GP14 and ground, DHT22 to GP16 with a pull-up.
Run the script
Paste it and press Run. Press the button and watch the LED.
Read the sensor
Every two seconds it prints temperature and humidity in the shell.
Save as main.py
Choose Save As, Raspberry Pi Pico, name it main.py. It now runs on every power up.
How do you build a microPython on Pico with sensors?
Use MicroPython on a Pico to read a temperature sensor, a button and an LED from the REPL. You need Raspberry Pi Pico H, DHT22 Sensor, Tactile push button 6 mm, LED 5 mm red, Resistor 220 Ω, Resistor 10 kΩ, Breadboard 830 Tie Points, Jumper wires male to male (40 pcs, 20 cm) and USB A to micro-USB cable (data). It takes 7 steps and about 45 minutes.
- Level
- Beginner
- Time
- About 45 min
- Parts
- 9
Parts list
- Development BoardsBuy
- SensorsBuyDHT22 Sensor × 1
- SwitchesBuy
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 220 Ω × 1
- PassivesBuyResistor 10 kΩ × 1
- PrototypingBuy
- PrototypingBuy
- Cables & WiresBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| LED anode | Pico GP15 through 220 Ω | Output |
| LED cathode | Pico GND | Return |
| Button | Pico GP14 and GND | Input with the internal pull-up |
| DHT22 VCC and GND | Pico 3V3 and GND | Sensor supply |
| DHT22 DATA | Pico GP16, with 10 kΩ to 3V3 | Data and pull-up |
Code
Thonny IDE with MicroPython on a Raspberry Pi Pico. Hold the BOOTSEL button while plugging the Pico in, copy the MicroPython UF2 file from micropython.org, then pick the Pico interpreter in Thonny. Save this file to the Pico as main.py and it will run when the Pico powers up.
from machine import Pin
import dht
import time
led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_UP) # pressed reads 0
sensor = dht.DHT22(Pin(16))
last_read = time.ticks_ms()
while True:
if button.value() == 0: # button toggles the LED
led.toggle()
time.sleep(0.3) # simple debounce
if time.ticks_diff(time.ticks_ms(), last_read) > 2000:
last_read = time.ticks_ms()
try:
sensor.measure()
print("Temp:", sensor.temperature(), "C Humidity:", sensor.humidity(), "%")
except OSError:
print("Sensor read failed")
time.sleep(0.01)Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Press the button | LED toggles |
| Shell output | Temperature and humidity every 2 seconds |
| Breathe on the sensor | Humidity jumps |
| Unplug and replug | main.py runs on boot |
If it does not work
Sensor read failed
Check the pull-up and wiring. The DHT22 needs a 2 second gap between readings.
Thonny cannot find the Pico
MicroPython not installed or a charge-only cable. Use a data cable.
LED does not toggle
Wrong pin number. Pico pin names are GP numbers, not board pin numbers.
Button seems to double-toggle
Increase the debounce delay.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the microPython on Pico with sensors?
Raspberry Pi Pico H, DHT22 Sensor, Tactile push button 6 mm, LED 5 mm red, Resistor 220 Ω, Resistor 10 kΩ, Breadboard 830 Tie Points, Jumper wires male to male (40 pcs, 20 cm) and USB A to micro-USB cable (data).
How long does the microPython on Pico with sensors take to build?
About 45 minutes for a beginner maker, in 7 steps.
How do I know the microPython on Pico with sensors works?
Press the button: LED toggles. Shell output: Temperature and humidity every 2 seconds. Breathe on the sensor: Humidity jumps.
Sensor read failed: what should I check?
Check the pull-up and wiring. The DHT22 needs a 2 second gap between readings.
Thonny cannot find the Pico: what should I check?
MicroPython not installed or a charge-only cable. Use a data cable.
LED does not toggle: what should I check?
Wrong pin number. Pico pin names are GP numbers, not board pin numbers.
Button seems to double-toggle: what should I check?
Increase the debounce delay.
