3.4. Ringing a Buzzer
3.4.1. Our goal
In this tutorial, we are making a Grove Buzzer play different musical tones on our GoPiGo3 robot. We start off with 3 musical notes and finish by playing the well-known “Twinkle Twinklle Little Star” song.
3.4.2. The code we analyse
The code we’re analyzing in this tutorial is this.
# import the time library for the sleep function
import time
# import the GoPiGo3 drivers
import easygopigo3 as easy
# Create an instance of the GoPiGo3 class.
# GPG will be the GoPiGo3 object.
gpg = easy.EasyGoPiGo3()
# Create an instance of the Buzzer
# connect a buzzer to port AD2
my_buzzer = gpg.init_buzzer("AD2")
twinkle = ["C4","C4","G4","G4","A4","A4","G4"]
print("Expecting a buzzer on Port AD2")
print("A4")
my_buzzer.sound(440)
time.sleep(1)
print("A5")
my_buzzer.sound(880)
time.sleep(1)
print("A3")
my_buzzer.sound(220)
time.sleep(1)
for note in twinkle:
print(note)
my_buzzer.sound(my_buzzer.scale[note])
time.sleep(0.5)
my_buzzer.sound_off()
time.sleep(0.25)
my_buzzer.sound_off()
The source code for this example program can be found here on github.
3.4.3. The modules
Start by importing 2 important modules:
import time
import easygopigo3 as easy
The easygopigo3
module is used for interacting with the GoPiGo3 robot, whilst
the time
module is generally used for delaying actions, commands, setting timers etc.
3.4.4. The objects
After this, we need to instantiate an easygopigo3.EasyGoPiGo3
object.
We will be using the EasyGoPiGo3
object for creating an instance of Buzzer
class,
which is necessary for controlling the Grove Buzzer device.
gpg = easy.EasyGoPiGo3()
Now that we have an EasyGoPiGo3
object, we can instantiate
a Buzzer
object.
The argument of the initializer method is the port to which we connect the Grove Buzzer and
it’s set to "AD2"
.
my_buzzer = gpg.init_buzzer("AD2")
Note
See the following graphical representation as a reference to where the ports are.
3.4.5. Setting variables
To play the “Twinkle Twinkle Little Star” song, we need to have a sequence of musical notes that describe this song.
We’re encoding the musical notes into a list (called twinkle
) of strings, where each string represents a musical note.
twinkle = ["C4","C4","G4","G4","A4","A4","G4"]
3.4.6. Main part
The main zone of the code is divided into 2 sections:
The 1st section, where we only play 3 musical notes with a 1 second delay.
The 2nd section, where we play the lovely “Twinkle Twinkle Little Start” song.
In the 1st section, we use the easysensors.Buzzer.sound()
method, which takes as a paramater,
an integer that represents the frequency of the emitted sound. As you can see in the following code snippet,
each musical note corresponds to a certain frequency:
The frequency of A4 musical note is 440Hz.
The frequency of A5 musical note is 880Hz.
The frequency of A3 musical note is 220Hz.
print("A4")
my_buzzer.sound(440)
time.sleep(1)
print("A5")
my_buzzer.sound(880)
time.sleep(1)
print("A3")
my_buzzer.sound(220)
time.sleep(1)
In the 2nd section we are using the scale
dictionary.
In this dictionary there are stored the frequencies of each musical note.
So, when using the twinkle
list in conjuction with scale
attribute,
we’re basically retrieving the frequency of a musical note (found in twinkle
attribute) from the scale
dictionary.
for note in twinkle:
print(note)
my_buzzer.sound(buzzer.scale[note])
time.sleep(0.5)
my_buzzer.sound_off()
time.sleep(0.25)
3.4.7. Running it
The only thing left to do is to connect the Grove Buzzer to your GoPiGo3 robot to port "AD2"
.
Then, on your Raspberry Pi, from within a terminal, type the following commands:
cd ~/Desktop/GoPiGo3/Software/Python/Examples
python easy_Buzzer.py
Tip
Please don’t expect to hear a symphony, because the buzzer wasn’t made for playing tones. We use the buzzer within this context to only demonstrate that it’s a nice feature.