import machine
import time
# Define the GPIO pin number where the buzzer is connected
BUZZER_PIN = 13 # Change this to the actual GPIO pin number
# Create a PWM object for the buzzer pin
buzzer_pwm = machine.PWM(machine.Pin(BUZZER_PIN))
# Function to play a tone on the buzzer for a certain duration
def play_tone(frequency, duration_ms=100):
buzzer_pwm.freq(frequency) # Set the PWM frequency for the specified tone
buzzer_pwm.duty(512) # Set the duty cycle (50%) to generate the tone
time.sleep_ms(duration_ms) # Play the tone for the specified duration
buzzer_pwm.duty(0) # Turn off the buzzer
# Example usage: Play a tone of 1000Hz for 500ms
while True:
for i in range(3):
play_tone(1000, 500)
time.sleep_ms(500);
time.sleep_ms(1000);