← Back to Lab Hub RP2040 JHS Lab

HC-SR501 PIR Motion Sensor Interrupt Simulator

Direct GPIO Digital Signals, Object Distance Triggering & MicroPython Interrupts

šŸ’” Object Detection Zone: Move the object slider below! When an object enters the detection range (< 6 cm), the PIR sensor pin (GP16) instantly transitions from 0V (LOW) to 3.3V (HIGH), triggering the MicroPython pir_handler interrupt routine!
RP2040 MASTER GP25 (LED) GP16 (PIR IN) HC-SR501 PIR DOME OUT Pin šŸ–ļø
DIGITAL SIGNAL WAVEFORM (GP16) GP16 GP25
GP16 Sensor Output: LOGIC 0 (LOW / 0V)
Onboard LED (GP25): OFF
MicroPython Console Output:
> MicroPython REPL Ready...
šŸ MicroPython Reference Code Inspector
from machine import Pin
import time

# Declare onboard LED to display status
led = Pin(25, Pin.OUT)

# PIR Signal Pin connected to GP16
# Note: HC-SR501 drives active HIGH/LOW outputs, so Pin.PULL_DOWN
pir = Pin(16, Pin.IN, Pin.PULL_DOWN)

# Interrupt Handler Function (ISR)
def pir_handler(pin):
  # Check simple logic 1 or 0
  if pin.value() == 1:
    led.value(1)  # Turn LED ON
    print(" [LOGIC 1] Motion Detected!")
  else:
    led.value(0)  # Turn LED OFF
    print(" [LOGIC 0] Area Clear.")

# Attach IRQ for BOTH RISING (1) and FALLING (0) edges
pir.irq(handler=pir_handler, trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING)

# Main loop is completely free
while True:
  # CPU does not need to poll the PIR sensor here
  time.sleep(1)
šŸ“ Concept Check Quiz
Why is trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING used for the PIR sensor instead of only Pin.IRQ_RISING?