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)