from machine import Pin
from time import sleep
# Initialize the LED pin (GPIO 2 as output)
led = Pin(2, Pin.OUT)
# Initialize the buzzer pin (GPIO 21 as output)
buzzer = Pin(21, Pin.OUT)
# Initialize the PIR motion sensor pin (GPIO 34 as input)
sensor = Pin(25, Pin.IN)
while True:
# Check if motion is detected by the PIR sensor
if sensor.value() == 1:
led.value(1) # Turn on the LED
buzzer.value(1) # Turn on the buzzer
print("Motion Detected")
sleep(5)
else:
led.value(0) # Turn off the LED
buzzer.value(0) # Turn off the buzzer
print("Motion Not Detected")
# Delay for a short period to avoid rapid toggling
sleep(0.5)