import RPi.GPIO as GPIO
import time

# Define GPIO pin for the piezo speaker
BUZZER_PIN = 5

# Set up GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUZZER_PIN, GPIO.OUT)

# Create PWM instance
buzzer = GPIO.PWM(BUZZER_PIN, 1000)  # Set initial frequency to 1kHz

try:
    buzzer.start(50)  # Start PWM with 50% duty cycle
    time.sleep(2)  # Sound the buzzer for 2 seconds
    buzzer.stop()
except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()  # Clean up GPIO settings
