from machine import Pin
import time
# Define the pins
pir_sensor = Pin(15, Pin.IN) # PIR motion sensor output
buzzer = Pin(16, Pin.OUT) # Buzzer or LED
def ring_bell():
# Activate the buzzer or LED
buzzer.value(1) # Turn on
time.sleep(1) # Ring for 1 second
buzzer.value(0) # Turn off
try:
print("Waiting for motion...")
while True:
if pir_sensor.value() == 1: # Motion detected
print("Motion detected! Ringing the bell...")
ring_bell()
time.sleep(2) # Prevents multiple triggers too quickly
time.sleep(0.1) # Small delay to avoid high CPU usage
except KeyboardInterrupt:
print("Program stopped by user.")