Build the circuit
Write the code
Read more- Put the buzzer on two different rows on the breadboard
- Use a wire to connect one leg to ground
- Use a wire to connect one leg to pin 26
- Review the breadboard nodes, Raspberry PI Pico W pinout and Fritzing diagram in the following figures
- In PlatformIO create the.py file and paste in the following code
- Save and right click to "Run the current file on the Pico"
#CAB 9.14.23 buzzer.py
#use buzzer to play music on Lily∞Bot
#buzzer on pin 27
#sonar on pins 2 and
#https://www.noiresteminist.com/shop
from machine import Pin, ADC, PWM
from utime import sleep
#define inputs and outputs
buzzerPin = 26
buzzer = PWM(Pin(buzzerPin))
buzzer.freq(500)
delay = 500
song = ["E5","G5","A5","P","E5","G5","B5","A5","P","E5","G5","A5","P","G5","E5"]
tones = {
"C5": 523,
"CS5": 554,
"D5": 587,
"DS5": 622,
"E5": 659,
"F5": 698,
"FS5": 740,
"G5": 784,
"GS5": 831,
"A5": 880,
"AS5": 932,
"B5": 988,
}
################# function definitions
def playtone(frequency):
buzzer.duty_u16(1000)
buzzer.freq(frequency)
def bequiet():
buzzer.duty_u16(0)
def playsong(mysong):
for i in range(len(mysong)):
if (mysong[i] == "P"):
bequiet()
else:
playtone(tones[mysong[i]])
sleep(0.3)
bequiet()
print("Buzzer on Lily...")
while True:
playsong(song)
while True:
bequiet()
Verify it works- Watch the following video to confirm your circuit and code are working correctly
- If not, please debug and troubleshoot the wiring and code.
Carlotta Berry
13 projects • 19 followers
Carlotta Berry is a Professor and Dr. Lawrence J. Giacoletto Endowed Chair for Electrical and Computer Engineering at Rose-Hulman.
Comments