from machine import Pin, PWM import time _init() tempo = 2 tones = { 'c': 262, 'd': 294, 'e': 330, 'f': 349, 'g': 392, 'a': 440, 'b': 494, 'C': 523 } speaker = PWM(Pin(15, Pin.OUT)) melody = 'abcdefgC' rhythm = [8, 2, 1, 8, 3, 4, 6, 8] for tone, length in zip(melody, rhythm): speaker.duty(512) speaker.freq(tones[tone]) time.sleep(tempo/length) speaker.duty(0) time.sleep(tempo/length/4)