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 = 'caagafcccaabgCCddbbagfcaagafCddbbagfcaagaf' rhythm = [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 3, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 3, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 3, 2] 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)