Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 32: Passive Buzzer Module
In this lesson, you will learn how to use the passive buzzer on Raspberry Pi Pico W to play single notes and perform music. You will understand how to use PWM (Pulse Width Modulation) to set up the buzzer on GPIO 16 and use the music class in the buzzer_music library to play complete songs. This course will guide you step by step through playing single notes, and then further execute full melodies such as “Happy Birthday”. This project is very suitable for beginners, providing a practical way to understand musical tones and integrate external libraries in MicroPython on Raspberry Pi Pico W.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Raspberry Pi Pico W |
|
Wiring
Code
import machine
import time
# Initialize the PWM on GPIO 16 for the buzzer
buzzer = machine.PWM(machine.Pin(16))
def tone(pin, frequency, duration):
"""Play a tone on the given pin at the specified frequency and duration."""
pin.freq(frequency)
pin.duty_u16(30000)
time.sleep_ms(duration)
pin.duty_u16(0)
# Play individual notes
tone(buzzer, 440, 250) # A4
time.sleep(0.5)
tone(buzzer, 494, 250) # B4
time.sleep(0.5)
tone(buzzer, 523, 250) # C5
time.sleep(1)
# Import the music class from the buzzer_music module for easy song playback.
from buzzer_music import music
# Find some music on onlinesequencer.net, click edit, select all notes with CTRL + A and then copy them with CTRL + C
# Paste the string to song, making sure to remove the "Online Sequencer:120233:" from the start and the ";:" from the end
# https://onlinesequencer.net/2474257 Happy Birthday (by Sudirth)
song = "0 G4 3 0;3 G4 1 0;4 A4 4 0;8 G4 4 0;12 C5 4 0;16 B4 8 0;24 G4 3 0;27 G4 1 0;28 A4 4 0;32 G4 4 0;36 D5 4 0;40 C5 8 0;48 G4 3 0;51 G4 1 0;52 G5 4 0;56 E5 4 0;60 C5 4 0;64 B4 4 0;68 A4 4 0;72 F5 3 0;75 F5 1 0;76 E5 4 0;80 C5 4 0;84 D5 4 0;88 C5 8 0"
# Initialize the music class with the song and set the buzzer pin
mySong = music(song, pins=[machine.Pin(16)])
# Play music using the music class.
while True:
print(mySong.tick())
time.sleep(0.04)
Code Analysis
Initialization
Import necessary modules and initialize the PWM on a specific GPIO pin to control the buzzer.
import machine import time # Initialize the PWM on GPIO 16 for the buzzer buzzer = machine.PWM(machine.Pin(16))
Defining the tone function
This function allows playing a single tone at a specified frequency and duration. It sets the frequency and duty cycle (volume) of the PWM signal.
def tone(pin, frequency, duration): """Play a tone on the given pin at the specified frequency and duration.""" pin.freq(frequency) pin.duty_u16(30000) time.sleep_ms(duration) pin.duty_u16(0)
Playing individual notes
Here, the
tonefunction is used to play individual notes. The parameters include the note’s frequency (in Hz) and its duration (in milliseconds).# Play individual notes tone(buzzer, 440, 250) # A4 time.sleep(0.5) tone(buzzer, 494, 250) # B4 time.sleep(0.5) tone(buzzer, 523, 250) # C5 time.sleep(1)
Using the buzzer_music library
The
buzzer_musiclibrary is imported, and a song string is prepared.You can find some music on onlinesequencer.net, click edit, select all notes with CTRL + A and then copy them with CTRL + C. Paste the string to
song, making sure to remove the “Online Sequencer:120233:” from the start and the “;:” from the end.For more information about the
buzzer_musiclibrary, please visit james1236/buzzer_music.# Import the music class from the buzzer_music module for easy song playback. from buzzer_music import music # https://onlinesequencer.net/2474257 Happy Birthday (by Sudirth) song = "0 G4 3 0;3 G4 1 0;4 A4 4 0;8 G4 4 0;12 C5 4 0;16 B4 8 0;24 G4 3 0;27 G4 1 0;28 A4 4 0;32 G4 4 0;36 D5 4 0;40 C5 8 0;48 G4 3 0;51 G4 1 0;52 G5 4 0;56 E5 4 0;60 C5 4 0;64 B4 4 0;68 A4 4 0;72 F5 3 0;75 F5 1 0;76 E5 4 0;80 C5 4 0;84 D5 4 0;88 C5 8 0"
Initializing and playing the song
The
musicclass is initialized with the song string and the GPIO pin for the buzzer. The music is played in a loop using thetickmethod of themusicclass.# Initialize the music class with the song and set the buzzer pin mySong = music(song, pins=[machine.Pin(16)]) # Play music using the music class. while True: print(mySong.tick()) time.sleep(0.04)