import machine
import time
buzzer = machine.PWM(machine.Pin(13)) # Create PWM object on Pin 13
# Define note frequencies (in Hz)
notes = {
'C4': 262,
'D4': 294,
'E4': 330,
'F4': 349,
'G4': 392,
'A4': 440,
'B4': 494,
'C5': 523,
' ': 0 # Silence
}
# Define the melody (notes + beats)
melody = [
('C4', 0.5), ('D4', 0.5), ('E4', 0.5), ('F4', 0.5),
('G4', 0.5), ('A4', 0.5), ('B4', 0.5), ('C5', 1.0),
(' ', 0.5) # Short pause at the end
]
# Play the melody
for note, duration in melody:
if notes[note] == 0:
buzzer.duty_u16(0) # Silence
else:
buzzer.freq(notes[note]) # Set frequency
buzzer.duty_u16(30000) # Set volume (0-65535)
time.sleep(duration)
buzzer.deinit() # Turn off PWM when done