This tutorial will show you how to add a small speaker to your cyber:bot board that can emit different frequencies. Using the cyberbot module’s play note or tone blocks, you will learn to create programs that make sounds and even write simple songs using lists (similar to arrays).
You will need:
You will be ready to continue with the main cyber:bot tutorial sequence. You will also be equipped to add your own custom audio alerts and sound effects to personalize your cyber:bot projects.
To emit sound, the cyber:bot uses a device called a piezoelectric speaker (piezospeaker). This speaker can make different tones depending on the rate of high/low electrical signals it receives. The schematic symbol and part drawing are shown below.
A piezoelectric element is a crystal that changes shape slightly when voltage is applied to it. Applying high and low voltages alternating at a rapid rate causes the crystal to rapidly change shape. The resulting vibration in turn vibrates the air around it, which is what the ear detects as sound.
Piezoelectric elements have many uses. As opposed to sending voltage to the piezoelectric element to make it change shape, when force is applied to a piezoelectric element (which impacts its shape), it actually creates voltage.
Some electronic drum sets use piezoelectric elements to detect when, and how hard, a drummer has hit a sensor.
Some barbeque grills have push-button lighters that use a piezoelectric element to cause a spark to start the grill. A spring-loaded hammer inside the button impacts a piezoelectric element to generate a small spark.
Piezoelectric elements, depending on their shape and thickness, have a frequency at which they naturally vibrate. This natural vibration can be used to create voltages at set frequencies that function as the clock oscillator for many computers and microcontrollers. If you have ever seen a clock or watch that says “quartz”, there is a piezoelectric quartz crystal inside.
Adapted Wikimedia Commons image “Inside Quartz Crystal Tuning Fork” by Chribbe76 [Public domain]
It is time to add a piezospeaker to your cyber:bot board.
(1) piezospeaker from the Small Robot Electronics Component Pack. (Just peel off the “Remove the seal after washing” sticker if it has one).
The picture above shows the schematic for the piezospeaker connected to P22. Notice that the cyber:bot board has two slots that fit the piezospeaker, and will connect it to P22.
The cyber:bot board was designed for a piezospeaker with long leads, like the one on the left. But due to supply chain challenges, some kits may include a piezospeaker with short leads, like the one shown on the right. Either will work!
The speaker should stay in place for the rest of the activities as it does not interfere with any other circuits since it’s not positioned on the breadboard.
If your piezospeaker has short leads, it may fall out of the socket when inserted from the top of the cyber:bot board. But that is okay, it can still be plugged into the socket from the bottom of the board.
Note: you can also build a piezospeaker circuit on the breadboard. To use a piezospeaker on the breadboard, make sure its legs go in different breadboard rows. Using jumper wires, connect its (+) leg to an I/O pin socket (P4 for example) and connect its other leg to a GND socket. In your projects, you will need to change Pin22 to Pin4 or the number for whichever pin you used.
Let’s test the piezospeaker using calls to the cyber:bot module’s tone block. True to its name, this block instructs an I/O pin to alternate high/low electrical signals at a specific frequency, allowing a piezospeaker to emit a tone.
The tone block allows you to specify the frequency with the freq parameter, and the duration of the tone in milliseconds with the dur parameter.
This piezospeaker is designed to play 4.5 kHz tones for smoke alarms, but it can also play a variety of audible tones and usually sounds best in the 1 kHz to 3.5 kHz range. The start-alert tone we’ll use is:
That will make P22 send a series of high/low signals repeating at 3 kHz (3000 times per second). The tone will last for 1000 ms, which is 1 second. The micro:bit will wait until the tone block is complete before moving on to the next command.
This example project makes a beep when it starts running, then it sends the message “Waiting for Reset” scrolling across the micro:bit LED matrix. These messages will continue indefinitely because they are in the forever loop. If the reset button is pressed, the speaker will replay the sound.
It is easy to make sound effects by using a for loop and a range block from the support subcategory.
You should hear a series of tones that increase in pitch.
Just for fun, modify the sound_effect project by varying the values in the range block’s start, end, and step parameters.
You can use musical note frequencies in the chart below for the tone block’s f parameter to play music.
Example:
This block plays a B6 note for half of a second. Using this chart, you’re able to create songs with the piezospeaker and the cyber:bot.
The notes to the first line of the Happy Birthday song go in the following order: D – D – E – D – G – F. Matching the notes with their frequencies, we can make the following program.
This example project plays out the first line of “Happy Birthday.”
Did you hear your cyber:bot play Happy Birthday?
A good way to keep track of notes in a song is to use a list. Sometimes, in other languages, these structures are referred to as an array. For now, they can be thought of as the same thing but there are some behind-the-scenes differences between these two which will be covered at some other time.
A list is a collection of values that can be referred to as a single, cohesive unit. Think about how a song is a single collection comprised of several individual notes. While each note is important, it is all the notes together that make up the song.
Taking the previous example, each of the notes in the project happy_birthday are grouped in order into a list called notes. The lengths of each note are grouped, in order, into another list called lengths.
To make use of data stored in a list, each element of a list is referred to by its index. The index of a list is simply the number of each element beginning with zero. So, the first element is referred to as element 0. To refer to element 0 of notes would be the notes get value at (0) block. The code to refer to the fifth element, would be the notes get value at (4) block. Notice how the index appears to be “off by one”. When working with lists, this can be a common mistake.
Element | 0 | 1 | 2 | 3 | 4 |
Contents | “Do” | “Re” | “Me” | “Fa” | “So” |
Block | ![]() |
With lists, you do not have to hard code numbers for the index. A variable, like index, can stand in for a number and then that variable can be changed as the program runs. Doing this allows blocks to “walk” through a list.
In the example above, the variable index is created after the two lists. The while loop checks the value of index each time it repeats, or iterates. As long as index is less than or equal to 5, the loop continues. Inside the loop, the tone block gets the values from each of the arrays depending on the current value of index. The second block in the loop increases the value of index by 1. This occurs over and over until the value of index becomes 6 and the loop stops. (And it’s a good thing it stops there because there is not an element number 6 in either of these lists!)