Projects / MicroPython on Pico with sensors

MicroPython on Pico with sensors

Beginner45 minGuided buildReviewed

Build it step by step

0 of 7 done
  1. PICOUSB

    Install MicroPython

    Hold BOOTSEL, plug in the Pico, drag the MicroPython UF2 file onto the drive that appears.

  2. PICOUSB

    Open Thonny

    Choose the MicroPython (Raspberry Pi Pico) interpreter in the bottom right.

  3. PICO14.9 mA

    Blink from the REPL

    Type from machine import Pin, then led = Pin(15, Pin.OUT) and led.toggle().

  4. PICODHT2226.2°C

    Wire the button and sensor

    Button to GP14 and ground, DHT22 to GP16 with a pull-up.

  5. PICO14.9 mA

    Run the script

    Paste it and press Run. Press the button and watch the LED.

  6. PICO

    Read the sensor

    Every two seconds it prints temperature and humidity in the shell.

  7. PICO

    Save as main.py

    Choose Save As, Raspberry Pi Pico, name it main.py. It now runs on every power up.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
LED anodePico GP15 through 220 ΩOutput
LED cathodePico GNDReturn
ButtonPico GP14 and GNDInput with the internal pull-up
DHT22 VCC and GNDPico 3V3 and GNDSensor supply
DHT22 DATAPico GP16, with 10 kΩ to 3V3Data 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.

CheckExpected
Press the buttonLED toggles
Shell outputTemperature and humidity every 2 seconds
Breathe on the sensorHumidity jumps
Unplug and replugmain.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 →

Ask Lark AI

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.