from machine import Pin
import time
# Define pins
pir_sensor = Pin(16, Pin.IN) # PIR motion sensor on GP16
led = Pin(17, Pin.OUT) # LED on GP17
buzzer = Pin(18, Pin.OUT) # Buzzer on GP18
def alert():
"""Blink LED and turn on buzzer when motion is detected"""
for _ in range(5): # Blink LED 5 times
led.on()
buzzer.on()
time.sleep(0.2)
led.off()
buzzer.off()
time.sleep(0.2)
# Main loop
while True:
if pir_sensor.value() == 1: # Motion detected
print("Motion detected!")
alert()
time.sleep(0.1) # Small delay to optimize performance