In this tutorial we learn how a buzzer can play music notes. See how easy it is to play a basic melody with a Python script running on your Raspberry Pi. When you have completed this tutorial, you will have the basic code to let your buzzer play a melody.
What you’ll need for this tutorial
First, you need to have a Raspberry Pi 3 running on the latest version of Raspbian. This version includes “Thonny”. We’ll use this user-friendly IDE to write our Python code. If you’re not familiar with Python or with Thonny or GPIO-pins, I suggest to have a look at our tutorials “First Python program on the Raspberry Pi” and/or “Raspberry Pi first program using GPIO” to have a quick introduction. For a basic understanding about the active buzzer and how to connect it to your Pi, have a look at our tutorial “Buzzer on Raspberry Pi generating beeps”.
Next, you’ll need some extra hardware:
- a breadboard (we are using a 400 points breadboard)
- a 5V active buzzer
- Dupont jumper wires
- NPN transistor (if you don’t have a transistor, have a look at our tutorial “Buzzer on Raspberry Pi generating beeps”, to see how you can setup the hardware without a transistor)
- a T-cobbler (optional)
- a 40 pin GPIO cable (optional)
If you miss any equipment, don’t hesitate to visit our shop. We have a nice kit which contains all the things you need tot start.
About music notes
In Wikipedia we can learn that a music note is the pitch and duration of a sound. Each pitch is an element of a pitch class and has a well defined frequency. This frequency can be calculated. But we won’t handle these topics in this tutorial. We just keep in mind we need the frequency and the duration for each note we want to play on the buzzer. We’ll use these two elements in our script.
For more information on music notes, visit this Wikipedia page.
For the frequencies of the pitches, have a look at this table.
Setting up the hardware part
We use the same hardware configuration as the one we used in our tutorial “Buzzer on Raspberry Pi generating beeps”.
- connect the 40 pin cable on the GPIO pins of your Pi (if necessary, remove the cover of your Pi first)
- plug the cobbler onto the breadboard as shown in the figure above or below
- plug the other end of the 40 pin cable in the T-cobbler
- place the buzzer on the breadboard, the long leg is the positive leg, make sure to place it near the border of the breadboard
- place the NPN transistor: on our image, the current flows from the right pin (3) to left pin (1), the middle pin (2) is the gate.
- connect transistor pin (3) to a 5V pin of the Pi (red wire)
- connect transistor pin (2) to pin 23 (yellow wire)
- make sure the long leg (+) of the buzzer is placed in the same breadboard row as transistor pin (1)
- connect the short leg (-) of the buzzer to a GPIO GND pin (black wire)
Writing the code
The aim of this tutorial is to write a simple Python script that allows the buzzer to play a simple melody. To write the code, we use the Thonny IDE. You can find Thonny under the application menu of your Raspberry Pi.
Write or paste following code in the IDE:
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) BUZZER = 23 GPIO.setup(BUZZER, GPIO.OUT) def buzz(noteFreq, duration): halveWaveTime = 1 / (noteFreq * 2 ) waves = int(duration * noteFreq) for i in range(waves): GPIO.output(BUZZER, True) time.sleep(halveWaveTime) GPIO.output(BUZZER, False) time.sleep(halveWaveTime) def play(): t=0 notes=[262,294,330,262,262,294,330,262,330,349,392,330,349,392,392,440,392,349,330,262,392,440,392,349,330,262,262,196,262,262,196,262] duration=[0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1,0.5,0.5,1,0.25,0.25,0.25,0.25,0.5,0.5,0.25,0.25,0.25,0.25,0.5,0.5,0.5,0.5,1,0.5,0.5,1] for n in notes: buzz(n, duration[t]) time.sleep(duration[t] *0.1) t+=1 #buzz(262, 0.5) play()
Some explanations about the code:
GPIO.setmode(GPIO.BCM): The GPIO.BCM option means that we are referring to the pins by the “Broadcom SOC channel” number, these are the numbers after “GPIO”
GPIO.setwarnings(False): We use this line of code to avoid warning messages in the case we don’t end the GPIO connection properly while interrupting the program
GPIO.setup(BUZZER, GPIO.OUT): We define the BUZZER pin (=23) as an output pin
def buzz(noteFreq, duration):Here we define the “buzz” function with the arguments “noteFreq” and “duration”
- Be careful, Python is whitespace-sensitive. Don’t remove the “tab” before the next lines of code
halveWaveTime = 1 / (noteFreq * 2 ): Calculating the time for halve of the wave of the given frequency
int(duration * noteFreq): Calculating the amount of waves to buzz
for i in range(waves):: A loop that will iterate until the number of waves has been reached
def play():: Defining the “play” function
notes=[…]: An array with all the frequencies of the music notes to play (Hz)
duration=[…]: An array with all the durations of the music notes to play (seconds)
for n in notes:: What follows will be repeated for each element of the array “notes”
buzz(n, duration[t]): Calling the “buzz” function with the elements of both arrays
time.sleep(duration[t] *0.1): Wait a short moment between playing each music note
#buzz(262, 0.5): This line has been commented. Uncomment to just play one music note
play(): By calling the “play” function, the melody will be played.
When you are done with the code, click on the Run button. You will hear a well-known melody.
Congratulations! With this setup you can now play a melody with a simple active buzzer on your Raspberry Pi. Feel free to change or add the “notes” and “duration” arrays for different melodies. Have fun with it!