I had some problems that the LED stays on now and then.
By moving the “led on” instruction to the interrupt function the problem was solved.
This is the adapted version:
from machine import Pin, Timer
import time
# PIR sensor pin
pir_pin = 28
# LED pin
led_pin = 20
# Set up LED and PIR sensor
led = Pin(led_pin, Pin.OUT)
pir = Pin(pir_pin, Pin.IN)
# Create timer
motion_timer = Timer()
# Create global variables
motion = False
motion_printed = False
def handle_motion(pin):
global motion
motion = True
motion_timer.init(mode=Timer.ONE_SHOT, period=1000, callback=turn_off_led)
#REMARK modification proposal
led.on()
def turn_off_led(timer):
global motion, motion_printed
led.off() # Turn off the LED
print(‘Motion stopped!’)
motion = False
motion_printed = False
# Attach interrupt to the PIR motion sensor
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_motion)
while True:
if motion and not motion_printed:
print(‘Motion detected!’)
#REMARK modification proposal
#led.on()
motion_printed = True
elif not motion:
time.sleep(0.1) # Other tasks that you might need to do in the loop
eBook Raspberry Pi pico remark example pag168 PIR detector
3 Answers