from machine import Pin, PWM
import time
buzzer = PWM(Pin(4))
# Happy Birthday Melody Frequencies (in Hz)
melody = [264, 264, 294, 264, 349, 330, 264, 264, 294, 264, 392, 349, 264, 264, 264, 440, 349, 330, 294]
note_duration = 0.5
buzzer.duty(512) # Duty cycle set to 50%
buzzer.freq(melody[0])
while True:
for freq in melody:
buzzer.freq(freq)
buzzer.duty(1023)
time.sleep(note_duration)
buzzer.duty(0)
time.sleep(0.1)