MicroPython motion sensor on a Pico: PIR on GP18, LED
MicroPython on a Raspberry Pi Pico: an HC-SR501 motion sensor on GP18 lights the LED on GP15 for as long as its output says something moved. The whole build, a Raspberry Pi Pico and 3 more parts, 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 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $6.15. On Mokxi they're free.
What you'll learn
- Reading a PIR motion sensor as a digital input
- Copying an input onto an output
- Printing only when something changes
- Powering a sensor from 5 V and reading it on a GPIO
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Raspberry Pi Pico
- Parts: 5
How it works
The HC-SR501 motion sensor has an output that goes high while it sees movement. That output goes to GP18, and the sensor is powered from 5 V. GP15 drives a red LED through a 220 ohm resistor.
The loop reads the sensor every 50 milliseconds and copies that value to the LED. It also prints motion or still, but only when the state changes.
Parts list
5 parts, plus the jumper wires. They retail for about $6.15; on Mokxi they're free.
- 1 × Raspberry Pi PicoIn Fight Stick Kit.
- 1 × Motion sensorIn Halloween Prop Kit.
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × Power, 5 V
Kits that can build it
This starter kit has 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 pin | Connects to |
|---|---|
| Resistor, 220 Ω pin 1 | Raspberry Pi Pico pin GP15 |
| Motion sensor pin OUT | Raspberry Pi Pico pin GP18 |
| Motion sensor pin VCC | 5 V |
| Motion sensor pin GND | Ground |
| LED, red pin A | Resistor, 220 Ω pin 2 |
| LED, red pin C | Ground |
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 motion detector: the HC-SR501's output on GP18 goes high while it
# sees something move, and the LED on GP15 follows it.
from machine import Pin
import time
sensor = Pin(18, Pin.IN)
led = Pin(15, Pin.OUT, value=0)
was = None
while True:
moving = sensor.value()
led.value(moving)
if moving != was:
print("motion" if moving else "still")
was = moving
time.sleep_ms(50)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED never lights This project
Check that sensor OUT goes to GP18 and the LED circuit goes to GP15. Then move the simulated motion in front of the sensor. - The sensor gives no signal This project
Check that VCC is on 5 V and GND is connected to ground. - The sensor reads high for ages after power up. Motion sensor
A real PIR needs 30 to 60 seconds to settle. Ignore its output during that time, and keep still in front of it until the delay passes. - Wrong output or it never triggers. Motion sensor
Power it from 5 V, join GND, and read OUT as a digital input (3.3 V logic high). Check the trimmers for delay and sensitivity. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.