from machine import Pin
import time
# Initialize PIR sensor and LED
pir = Pin(16, Pin.IN)
led = Pin(15, Pin.OUT)
# Variable to keep track of the LED state
led_on = False
led_timer = 0
while True:
# Check for motion
if pir.value():
if not led_on:
led.on() # Turn on LED
led_on = True
led_timer = time.time() # Start the timer
else:
if led_on and (time.time() - led_timer) >= 1:
led.off() # Turn off LED after 1 second
led_on = False
# Small delay to avoid excessive polling
time.sleep(0.1)