Carlotta Berry
Published

Lily∞Bot with Raspberry Pi Pico W: Play music on buzzer

This project will describe how to play a song/music on the buzzer mounted to the breadboard on the Lily∞Bot programmed in MicroPython.

BeginnerFull instructions provided1 hour195
Lily∞Bot with Raspberry Pi Pico W: Play music on buzzer

Things used in this project

Hardware components

LilyBot
×1
Buzzer
Buzzer
×1
Raspberry Pi Pico W
Raspberry Pi Pico W
×1

Software apps and online services

PlatformIO IDE
PlatformIO IDE
Visual Studio 2015
Microsoft Visual Studio 2015
MicroPython
MicroPython

Story

Read more

Custom parts and enclosures

Lily Bot GIT HUB Repository

Schematics

Lily Bot GIT HUB Repository

Lily Bot Buzzer Fritzing

Code

Lily Bot GIT HUB Repository

MicroPython
#CAB 9.14.23 buzzer.py
#use buzzer to play music on LilyBot
#buzzer on pin 27
#sonar on pins 2 and 
#https://www.noiresteminist.com/shop


from machine import Pin, ADC, PWM
from utime import ticks_us, sleep_us, sleep

#define inputs and outputs
ledPin = 28
buzzerPin = 26
led = Pin(ledPin, Pin.OUT)
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()

Buzzer.py

MicroPython
#CAB 9.14.23 buzzer.py
#use buzzer to play music on LilyBot
#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()

Credits

Carlotta Berry

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