from machine import Pin
import time
# Define the GPIO pins for the buzzer and motion sensor
BUZZER_PIN = 16 # Update this to match your wiring
MOTION_SENSOR_PIN = 14 # Update this to match your wiring
# Set up the GPIO pins
buzzer = Pin(BUZZER_PIN, Pin.OUT) # Output pin for the buzzer
motion_sensor = Pin(MOTION_SENSOR_PIN, Pin.IN, Pin.PULL_UP) # Input pin for the motion sensor with pull-up
try:
while True:
motion_state = motion_sensor.value() # Read the motion sensor state
if motion_state == 1: # Motion detected
print("The movement is detected")
buzzer.on() # Turn the buzzer on
else:
print("The movement is stopped")
buzzer.off() # Turn the buzzer off
# Add a slight delay to debounce the motion sensor (optional)
time.sleep(0.1)
# Allow the user to stop the buzzer by pressing Ctrl+C
except KeyboardInterrupt:
buzzer.off() # Turn off the buzzer
print("Program terminated")