User Menu

Brain Board: play a tune with Buzz’s Piezo (Preview)

This is a preview lesson. Please sign up for the course to access all lessons.

This tutorial will show you how to use the Brain Board to play a melody with Buzz’s Piezo – what we call Buzz’s Buzzer. The code we use will work for the Brain Board as well as a Arduino, just change the pinouts accordingly.

This tutorial is based on the  the Arduino Melody Tutorial and the Arduino Tone Melody Tutorial.

During this lesson we will cover:

  • What is Piezo – How Buzz’s Buzzer works
  • Pulse Width Modulation also referred to as PWM
  • the tone() function

Gather your Hardware and USB Cable

What you will need for this lesson:

  • Buzz
  • Brain Board
  • USB Cable
  • Arduino IDE installed with support libraries for the Brain Board.

What is a Piezo Anyway?

Buzz’s buzzer is a Piezo Electric Actuator. It is a fancy name for a special type of speaker. It works by applying electrical voltage to the crystals attached to the plates inside of it.

Certain crystals when stressed (mechanically moved) create and store electrical charges. They also have the reverse property, when you apply electricity to them they move. By using these properties and applying voltage in the correct amounts they can become a speaker. In reverse they can be used as a sensor. For example, place the same Piezo on a guitar. When you play the guitar the sound vibrations would move the Piezo and generate electric signals. We can then use an Arduino to detect those electrical signals.

To make Buzz play music we are going to send electric signals to his Buzzer in very controlled formats (called Pulse Width Modulation or PWM). Those signals will make the Piezo vibrate and move the large disc and move the air. Those vibrations will create the music notes we hear. There is a lot more science behind this, and you can learn more on Wikipedia.

"SchemaPiezo" by Tizeff - Template:Ownnn. Licensed under CC BY-SA 3.0 via Commons -

“SchemaPiezo” by Tizeff – Template:Ownnn. Licensed under CC BY-SA 3.0 via Commons –

Pulse Width Modulation

To make our Piezo buzz we need to make it distort by sending electrical signals. We need to essentially turn it “on” and “off” very fast and for certain intervals so we can make it vibrate and produce the sounds and musical notes we want.

Let us imagine we have a battery, a big switch, and a piezo buzzer all connected. If we turn the switch to on, the piezo would deform. If we turn it off, the piezo would return to normal. Fun! But we did not hear any sound. Why not? Because it is not moving fast enough. Now imagine you could turn that switch on and off about 261 times a second. Now you would see that piezo starting to buzz.

Why 261 times per second? At 261 Hz or 261 cycles per second you would make the sound of a C note (it would sound somthing like this :  1 Hz means that an event repeats once every second. A previous name for this unit was cycle per second.  It would sound nice, but we would be tired. We would not only need to turn on and off the electric, we would need to keep it on and off for very specific times each time. Our brain would get tired trying to count that out. It would look something like this.


If we want to make different notes we need to alter how many times per second we turn the electric on and off.


When we alter the number of times that we turn the signal on and off per second, that is what we call Frequency.  We can change the notes we want to play by altering the frequency of the on and off signal.

How long the signal is on or off is called the Duty Cycle.  The above two examples have a different Frequency. The first one is on only 4 times and the second one is on 8 times. However, they both have the same Duty Cycle. Their on and off (low and high) are half the time (50%) of the cycle. See it? If this was one second, the graph shows they are each on for half a second total and off for half a second. What would you expect that to sound like?

So what happens if we keep the frequency the same, but change the Duty Cycle? What will it look like?

PWM Duty Cycle 1

You can see in the chart that the the Frequency is off the same number of times as it is on but that the signal is off longer than it is on, we have changed the Duty Cycle. What if we reverse that?

PWM - Duty Cycle 2

The Frequency is the same, but the Duty Cycle is reversed. The signal is on longer than it is off.

If we had to do this by hand, switching that switch on and off it would be very tiring indeed. Luckily for us we can leverage the ATTiny chip in our Brain Board to do this for us. In our code we can instruct the ATTiny chip to send these type of signals to our Piezo. We call this type of signal a Pulse Width Modulation. We can use it to control LEDs, motors, and of course Piezo devices.

In our first example we will tell the ATTiny to turn Buzz’s Piezo on Pin 0 HIGH (on) and LOW (off) repeatedly at certain frequencies.

Let’s make some noise

  • Launch the Arduino IDE.
  • Start a new sketch. On the Arduino menu go to FILE -> NEW
  • Copy the code from the example sketch below. Paste into your Arduino IDE sketch.
  • Save the sketch. File -> SAVE (name it something such as sketch_Buzz_Buzz)
  • Under TOOLS – > Board, make sure you have selected the “Digispark (Default 16.5mhz)”


The code we are using here is from the Arduino Melody Tutorial. 

Copy the code from the right side and paste it into your Arduino IDE.

Note that Buzz’s Buzzer when in the Brain Board is on Pin 0. If you plug Buzz into an Arduino UNO or the like, it will be Pin 9.

int speakerPin = 0;

int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

void playTone(int tone, int duration) {
 for (long i = 0; i < duration * 1000L; i += tone * 2) {
 digitalWrite(speakerPin, HIGH);
 digitalWrite(speakerPin, LOW);

void playNote(char note, int duration) {
 char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
 int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

 // play the tone corresponding to the note name
 for (int i = 0; i < 8; i++) {
 if (names[i] == note) {
 playTone(tones[i], duration);

void setup() {
 pinMode(speakerPin, OUTPUT);

void loop() {
 for (int i = 0; i < length; i++) {
 if (notes[i] == ' ') {
 delay(beats[i] * tempo); // rest
 } else {
 playNote(notes[i], beats[i] * tempo);

 // pause between notes
 delay(tempo / 2); 

Arduino Verify

Verify the Sketch – click on the check mark icon. This will verify that your code does not have any syntax or procedural errors. However, it does not verify that your code will do what you want. That is up to you.

If the code is ok, you will not see an errors. If there are any errors, double check that you did not miss anything when you copied it from the above example.

sketch_Chip_Blink Arduino upload

Brain Board Upload

You are ready to upload the code from your computer to the Brain Board.

Disconnect the Brain Board from USB.

Click the Upload Icon arrow, which is next to the verify button.

When prompted, plug in the Brain Board to USB.

After the upload is successful, Buzz should be playing a familiar tune. Can you tell what it is?

Note Definitions


In our code each note has a specific frequency and duty cycle. For musical notes the duty cycle is always 50%. Each note is “ON” or HIGH for the same amount of time it is “off” or LOW in it’s period.

We can see this mathematically:

timeHigh = period / 2 = 1 / (2 * toneFrequency)
note    frequency    period    timeHigh
c       261 Hz       3830       1915 	
d       294 Hz       3400       1700 	
e       329 Hz       3038       1519 	
f       349 Hz       2864       1432 	
g       392 Hz       2550       1275 	
a       440 Hz       2272       1136 	
b       493 Hz       2028       1014	
C       523 Hz       1912        956

Try changing values in the code, what happens? You want to edit this line:

int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

A Better Way

Introducing the tone() function

In our example we we defined the frequency and duty cycle value of each note and then we turned the speaker pin HIGH and LOW using the digitalWrite function. We looped through it to play each note until the song was complete. If we had a longer, more complicated, song we wanted to play this would be tedious and use a lot of resources. We are light years ahead of our battery, switch, and buzzer that we started with but we can still do better.

Arduino has built into it a tone() function that automatically does all this for us. All we need to do is call it with the Pin we want to play the tone on and the frequency of the note we want. We can also add duration if we like, if we do not specify a duration the default is 1000 milliseconds, or 1 second. To play our C note on Buzz’s Buzzer it looks like this:

tone(0, 261)

Tone() Melody

This example is from the Arduino Tone() Melody tutorial



  • Start a new sketch. On the Arduino menu go to FILE -> NEW
  • Add a new TAB and name it Pitches.h
  • Copy the code from the example sketch below. Paste into your Arduino IDE sketch.
  • Save the sketch. File -> SAVE (name it something such as Buzz_ToneMelody )
  • Under TOOLS – > Board, make sure you have selected the “Digispark (Default 16.5mhz)”

Since musical notes are constants we do not need to clutter up our code with their definitions. A c note is a c note always. Instead we use a “.h” file which is a library that we can reference from our code. This is why we add a new tab and name it with a “.h” extension. To create the new tab, and the .h file, either click on the button just below the serial monitor icon and choose “New Tab”, or use Ctrl+Shift+N.

Click this icon to add your new tab

Click this icon to add your new tab

Copy in the code from this link and paste into your pitches.h tab:

Click back to the main tab, your Buzz_ToneMelody sketch. Note that in the Arduino Tutorial, they use Pin 8 – we are using Pin 0 for the Brain Board.

Copy the code below into your sketch:

#include "pitches.h"

// notes in the melody:
int melody[] = {

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(0, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    // stop the tone playing:

void loop() {
  // no need to repeat the melody.

Verify and then upload the code to the Brain Board. Hear a familiar tune?

It is the tune Buzz shipped with. It will only Play once because it is in the setup(). If you would like it to play continuously, what would you need to do? That is right – put the same code in the loop() function.

Play around with notes, duration, and see what happens. Can you blink Buzz’s eyes while it plays?

Give it a try. If you have any questions post them here.

Don’t forget to verify and upload your code after each change. Also, don’t forget to unplug the Brain Board before uploading your sketch.

Play around, you can not break anything. You can always re-copy and upload the original sketch.

Access all the code for The Brain Board and The Pi Pals

GitHub is a website where you can store, share, and manage code for projects. We use it store all the code for our lessons so that it is easy for you to download. If you have code you would like to contribute, please do so. You can access Soldering Sunday’s Github Repository here

Lesson tags: Arduino, Brain Board, Buzz
Back to: Brain Board