from machine import Pin
import time
# PIR sensor pin
pir = Pin(13, Pin.IN)
# LED pin (simulating room light)
light = Pin(2, Pin.OUT)
# Track last motion time
last_motion_time = 0
motion_timeout = 120 # 2 minutes in seconds
while True:
if pir.value() == 1: # Motion detected
print("Motion detected! Light ON.")
light.value(1)
last_motion_time = time.time() # reset timer
else:
# Check how long since last motion
if time.time() - last_motion_time > motion_timeout:
light.value(0)
print("No motion for 2 minutes. Light OFF.")
time.sleep(1) # check every 1s