from machine import Pin
import time
# Define pins
pir_sensor = Pin(16, Pin.IN) # PIR sensor connected to GPIO16
buzzer = Pin(15, Pin.OUT) # Buzzer connected to GPIO15
while True:
if pir_sensor.value() == 1: # Motion detected
buzzer.value(1) # Turn on the buzzer
time.sleep(0.5) # Buzz for 0.5 seconds
buzzer.value(0) # Turn off the buzzer
time.sleep(0.5) # Delay before the next check
else:
buzzer.value(0) # Ensure buzzer is off if no motion is detected
time.sleep(0.1) # Check again after a short delay