import time
time.sleep(0.1) # Wait for USB to become ready
from machine import Pin
BUZZER_PIN = 3 # You can change this if you wired it to a differen
# Set up the buzzer pin as an output
buzzer =Pin(BUZZER_PIN, Pin.OUT)
try:
#Beep the buzzer on and off 5 times
for i in range(5):
buzzer.value (1) # Turn the buzzer on
print("Buzzer ON")
time.sleep(0.5) # Buzzer stays on for 0.5 seconds
buzzer.value (0) # Turn the buzzer off
print("Buzzer OFF")
time.sleep(0.5) # Buzzer stays off for 0.5 seconds
finally:
buzzer.value(0) # Make sure the buzzer is off after the loop
print("Program ended")
'''
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
BUZZER_PIN = 18
GPIO.setup(BUZZER_PIN, GPIO.OUT)
try:
for i in range(5):
GPIO.output(BUZZER_PIN, GPIO.HIGH)
print("Buzzer ON")
time.sleep(0.5)
GPIO.output(BUZZER_PIN, GPIO.LOW)
print("Buzzer OFF")
time.sleep(0.5)
except KeyboardInterrupt:
GPIO.cleanup()
print("Program stopped")
finally:
GPIO.cleanup()'''