from machine import Pin
import time
PIR_PIN = 16
LED_PIN = 15
pir_sensor = Pin(PIR_PIN, Pin.IN)
light = Pin(LED_PIN, Pin.OUT)
state = False
motion_count = 0
print("Raspberry Pi Pico PIR Motion Detection")
try:
while True:
start_time = time.ticks_us()
motion_detected = pir_sensor.value()
if motion_detected == 1:
light.on()
response_time = time.ticks_diff(time.ticks_us(), start_time)
if not state:
motion_count += 1
print("Motion #" + str(motion_count) + " - Response Time: " + str(response_time) + " μs")
state = True
else:
light.off()
if state:
print("Motion stopped")
state = False
time.sleep(0.5)
except KeyboardInterrupt:
light.off()