Buzzer Access: CircuitPython

In case anyone is interested in the buzzer functionality of the Wio Terminal using CircuitPython.

Why? Because.

import board
import pulseio
import time

##MARIO THEME
##ADAPTED BY: https://hackaday.io/project/25888-broadcast-super-mario-bros-with-pwm


melody = [
330, 330, 330, 262, 330, 392, 196, 262, 196, 165, 220, 247, 233, 220, 196, 330, 392, 
440, 349, 392, 330, 262, 294, 247, 262, 196, 165, 220, 247, 233, 220, 196, 330, 392,
440, 349, 392, 330, 262, 294, 247, 392, 370, 330, 311, 330, 208, 220, 262, 220, 262,
294, 392, 370, 330, 311, 330, 523, 523, 523, 392, 370, 330, 311, 330, 208, 220, 262,
220, 262, 294, 311, 294, 262, 262, 262, 262, 262, 294, 330, 262, 220, 196, 262, 262,
262, 262, 294, 330, 262, 262, 262, 262, 294, 330, 262, 220, 196]
 
noteDurations = [
8,4,4,8,4,2,2,
3,3,3,4,4,8,4,8,8,8,4,8,4,3,8,8,3,
3,3,3,4,4,8,4,8,8,8,4,8,4,3,8,8,2,
8,8,8,4,4,8,8,4,8,8,3,8,8,8,4,4,4,8,2,
8,8,8,4,4,8,8,4,8,8,3,3,3,1,
8,4,4,8,4,8,4,8,2,8,4,4,8,4,1,
8,4,4,8,4,8,4,8,2
]


volume = 512

for i in range(len(melody)): 
  buz = pulseio.PWMOut(board.BUZZER, frequency=melody[i]*2, duty_cycle=volume)
  noteDuration = 800/noteDurations[i]
  time.sleep((noteDuration * 1.4)/1000)
  buz.deinit()
1 Like