from machine import Pin, PWM
from time import sleep
# Initialize the buzzer and buttons
buzzer = PWM(Pin(15))
button_A = Pin(16, Pin.IN, Pin.PULL_UP)
button_B = Pin(17, Pin.IN, Pin.PULL_UP)
button_C = Pin(18, Pin.IN, Pin.PULL_UP)
button_D = Pin(19, Pin.IN, Pin.PULL_UP)
# Define the frequencies for the notes
notes = {
'C7': 2093,
'D7': 2349,
'E7': 2637,
'F7': 2794
}
# Function to play a note
def play_note(note, duration=0.5):
freq = notes[note]
buzzer.freq(freq)
buzzer.duty_u16(int(65535 * (freq / 440)))
sleep(duration)
buzzer.duty_u16(0)
# Main loop
while True:
if button_A.value() == 0:
play_note('C7')
print('Botton A')
elif button_B.value() == 0:
play_note('D7')
print('Botton B')
elif button_C.value() == 0:
play_note('E7')
print('Botton C')
elif button_D.value() == 0:
play_note('F7')
print('Botton D')