import machine
import time
buzzer_pin = machine.Pin(15, machine.Pin.OUT)
buzzer_pwm = machine.PWM(buzzer_pin)
def play_note(frequency, duration):
buzzer_pwm.freq(frequency)
buzzer_pwm.duty_u16(32768)
time.sleep_ms(duration)
buzzer_pwm.duty_u16(0) # Turn off the buzzer after the duration
notes = {
'C': 261, 'D': 294, 'E': 329, 'F': 349, 'G': 392, 'A': 440, 'B': 493
}
melody = ['C', 'C', 'G', 'G', 'A', 'A', 'G']
note_duration = 500
for note in melody:
if note in notes:
play_note(notes[note], note_duration)
else:
time.sleep_ms(note_duration)