Built-in project · ESP32-DevKitC V4

MicroPython motion sensor lights an LED on an ESP32-DevKitC V4

MicroPython on an ESP32: an HC-SR501 motion sensor on GPIO 14, powered from 5 V, lights the LED on GPIO 13 while it sees movement. The whole build, an ESP32-DevKitC V4 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 $12.15. On Mokxi they're free.

MicroPython motion sensor lights an LED · ESP32-DevKitC V4live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Reading a PIR motion sensor as a digital input
  • Mirroring an input onto an output
  • Printing only when something changes
  • Powering a sensor from 5 V while reading it on a GPIO
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: ESP32-DevKitC V4
  • Parts: 5

How it works

The HC-SR501 motion sensor has an output that goes high while it sees movement. That output goes to GPIO 14, and the sensor is powered from 5 V. GPIO 13 drives a red LED through a 220 ohm resistor.

The loop checks the sensor every 50 milliseconds and copies its state to the LED. It also prints a message only when the state changes, so the Serial Monitor shows Somebody is in this area! or No one! once per change.

Parts list

5 parts, plus the jumper wires. They retail for about $12.15; on Mokxi they're free.

Kits that can build it

These 3 starter kits have 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 pinConnects to
Motion sensor pin OUTESP32-DevKitC V4 pin 14
Resistor, 220 Ω pin 1ESP32-DevKitC V4 pin 13
LED, red pin AResistor, 220 Ω pin 2
LED, red pin CGround
Motion sensor pin VCC5 V
Motion sensor pin GNDGround

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.

main.py · ESP32-DevKitC V4
# Motion detector: the HC-SR501's output on GPIO 14 goes high while it
# sees movement, and the LED on GPIO 13 follows it.
from machine import Pin
import time

sensor = Pin(14, Pin.IN)
led = Pin(13, Pin.OUT, value=0)
was = None

while True:
    moving = sensor.value() == 1
    led.value(1 if moving else 0)
    if moving != was:
        print("Somebody is in this area!" if moving else "No one!")
        was = moving
    time.sleep_ms(50)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED never turns on This project
    Check that sensor OUT goes to GPIO 14 and the LED circuit is on GPIO 13. Also move the simulated motion in front of the sensor.
  • The sensor does nothing This project
    Check that VCC is on 5 V and GND is connected to ground.
  • 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.
  • 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.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.