Arduino is an excellent way to simplify and speed up your microcontroller projects, thanks to its community of developers who have made almost everything look simple. There are lots of Arduino Projects out here for you to try and have fun. Some of your projects might need some sounds action to notify about something or just to impress the viewers. What if I told you that almost any theme songs that could be played on a piano can be mimicked on your Arduino with the help of a simple program and a cheap Piezo speaker?

In this tutorial we will learn how simple and easy it is to Play Melody on Piezo Buzzer or Speaker using the Arduino tone () function. At the end of this tutorial you will be able to play some famous tones of Pirates of Caribbean, Crazy Frog, Super Mario and Titanic. You will also learn how to play any piece of piano music with Arduino. Check the Video at the end.

Hardware Required:

Arduino (any version – UNO is used here)

Piezo Speaker/Buzzer or any other 8ohm speaker.

Breadboard

Connecting Wires

Push buttons

1k resistor (optional)

Understanding the Tone() function of Arduino:

Before we can understand how a tone () works we should know how a Piezo buzzer works. We might have learnt about Piezo crystals in our school, it is nothing but a crystal which converts mechanical vibrations into electricity or vice versa. Here we apply a variable current (frequency) for which the crystal vibrates thus producing sound. Hence in order to make the Piezo buzzer to make some noise we have to make the Piezo electric crystal to vibrate, the pitch and tone of noise depends on how fast the crystal vibrates. Hence the tone and pitch can be controlled by varying the frequency of the current.

Okay, so how do we get a variable frequency from Arduino? This is where the tone () function comes in. The tone () can generate a particular frequency on a specific pin. The time duration can also be mentioned if required. The syntax for tone () is

Syntax
tone(pin, frequency)
tone(pin, frequency, duration)
Parameters
pin: the pin on which to generate the tone
frequency: the frequency of the tone in hertz - unsigned int
duration: the duration of the tone in milliseconds (optional) - unsigned long

The values of pin can be any of your digital pin. I have used pin number 8 here. The frequency that can be generated depends on the size of the timer in your Arduino board. For UNO and most other common boards the minimum frequency that can be produced is 31Hz and the maximum frequency that can be produced is 65535Hz. However we humans can hear only frequencies between 2000Hz and 5000 Hz.

The pitches.h header file:

Now, we know how to produce some noise using the arduino tone() function. But, how do we know what kind of tone will be generated for each frequency?

Arduino have given us a note table which equates each frequency to a specific musical note type. This note table was originally written by Brett Hagman, on whose work the tone() command was based. We will use this note table to play our themes. If you are someone familiar with sheet music you should be able to make some sense of this table, for others like me these are just another block of code.

Above code is given in pitches.h header file in this zip file, you just need to download and include this file in our Arduino code as given at the end this tutorial or use the code given in the zip file.

Playing Musical Notes on Arduino:

To play a decent melody using Arduino we should know what constitutes these melodies. The three main factors required to play a theme are

Note value

Note Duration

Tempo

We have the pitches.h header file to play any note value, now we should find out its specific note duration to play it. Tempo is nothing but how fast the melody should be played. Once you know the Note value and Note duration you can use them with the tone() like

tone (pinName, Note Value, Note Duration);

For the tones played in this tutorial I have given you the note Value and Note duration inside the “themes.h” header file using which you can play them in your projects. But if you have any specific tone in your mine and you want to play it in your project read on.... Else skip this topic and fall down to the next.

To play any specific tone you have to get the sheet music of that particular music and convert sheet music to Arduino sketch by reading the note value and note duration from it. If you are a musical student it would be a piece of cake for you, else spent some time and break you head like I did. But at the end of the day when your tone plays on the Piezo buzzer you will find your effort worth it.

Once you have the note value and note duration, load them into the program inside the “themes.h” header file as shown below

The above block of code shows the note value and note duration of “He is a Pirate” theme form the movie Pirates of the Caribbean. You can add your theme similarly like this.

Schematic and Hardware:

The schematic of this Arduino Tone Generator Project project is shown in the figure below:

The connection is pretty simple we have a Piezo speaker which is connected to pin 8 and Ground of the Arduino through a 1K resistor. This 1k resistor is a current limiting resistor, which is used to keep the current within the safe limits. We also have four switches to select the required melody. One end of the switch is connected to ground and the other end is connected to pin 2, 3, 4 and 5 respectively. The switches will have pull up resistors enabled internally using the software. Since the circuit is pretty simple it can be connect using a bread board as shown below:

Arduino Program Explanation:

Once you have understood the concept, the Arduino program is pretty straight forward. The complete code is given at the end of the tutorial. If you are not familiar with adding header files you can download the code as a ZIP file from here and directly upload it to your Arduino.

The above two are the header files that have to be added. “pitches.h” is used to equate each musical note to a particular frequency and “themes.h” contains the note value and note duration of all the four tones.

#include "pitches.h"
#include "themes.h"

A function is created to play each tone when required. Here when the function Play_Pirates() is called the “He is a Pirate” tone will be played. This function consists of the tone function which produces the frequency at pin number 8. The noTone(8) is called to stop the music once it’s played. If you want to play your own tone, change the Pirates_note and Pirates_duration to the new note and duration values that you have saved in “themes.h” value

The pin 2, 3, 4 and 5 are used to select the particular tone to be played. These pins are held high by default using the internal pull up resistors by using the above line of code. When the button is pressed it is pulled down to ground.

Below block of code is used to play the song when a button is pressed. It reads the digital value of each button and when it gets low (zero) it assumes that the button is pressed and plays the respective tone by calling the required function.

Working of this Melody Player Arduino Circuit:

Once your Code and Hardware is ready, simply burn the program into your Arduino and you should be able to play the tone by simply pressing the buttons. If you have any problems take a look at your serial monitor for debugging or use the comment section to report the problem and I will be happy to help you out.

The complete working of the project is shown in the video below. Hope you enjoyed the project and would use it in some of your project or create a new tone for your project. If yes feel free to share your work in the comment section.

Firstly, thank you for the thorough tutorial! The way you broke down the steps really helped me understand what was going on (well, a little more than I did before, which isn't much to be honest).

My questions are this:
Is there a way to increase the sound. I am using the correct resistors but my sound if very soft. if I remove the resistor, I can hear the Pziezo really struggles.

Secondly, is there a way to have one button (button pin 2) cycle through songs being played. e.g. I press the button, the first song plays. I press it again, the second song, third time, the 3rd song and so on.

Firstly, thank you for the thorough tutorial! The way you broke down the steps really helped me understand what was going on (well, a little more than I did before, which isn't much to be honest).

Thank you for your words, well yes I dint wanna pump too much of information as it might get boring to read.

Is there a way to increase the sound. I am using the correct resistors but my sound if very soft. if I remove the resistor, I can hear the Pziezo really struggles.

Don't remove the resistor just decrease the value of it. If you want much better sound you should try an audio amplifier circuit, which might be a bit of overkill but if you are into learning things then its worth giving a shot.

Secondly, is there a way to have one button (button pin 2) cycle through songs being played. e.g. I press the button, the first song plays. I press it again, the second song, third time, the 3rd song and so on.

Yes, it is possible but not with the digital pin. You have to use an analog pin and form a potential divider with different values of resistors with each switch, this way when each switch is pressed a different voltage will be supplied to the analog pin using which we can detect which button was pressed.