This is an Arduino project that uses the native tone generating ability of the Arduino to make music. You can use the sketch provided to start composing music of your own! Make it play 'Happy Birthday' for a friend. You'll see how to make music with your Arduino in the steps that follow.

For more information on Arduino's tone command see


The Arduino sketch provided here uses some advanced string methods - you will see them explained in the code comments. Here we acknowledge the help of our programming advisor, Andrew Wendt - thanks again.

Be an Arduino toneTune Composer: it will be fun.

  • What we don't get:
  • multiple note chords. toneTune will play only one note at a time.
  • What we do get:
  • a full 7 octaves, which is the full piano keyboard
  • note duration from sixteenths to full notes, sharps and flats, dotted notes, and rests
  • a very responsive program that handles note changes, trills, and quick tempo
  • short rests will give a staccato effect
  • Change the sketch:
  • You can change the sketch to make modifications on your own - for example to increase or decrease the tempo or use a loop to play your melody more than once.

Step 1: The ToneTune Arduino Sketch

This sketch will turn your Arduino into a mini music player.

See Step 5 How it Works for an explanation of the program.

Step 2: How It Sounds

Here is a demonstration of what you can do with the Arduino and toneTune. Take this as a starting point, and enter notes that play familiar melodies or something that you make up on your own.

Step 3: How to Get Sound From Arduino - Connecting a Buzzer, Speaker or Amplified Speaker

You can get sound output from your Arduino in various ways. For the purposes of this project we will use one of these simple methods.

  1. connect one lead of a piezo buzzer to Arduino digital pin 9 and the other lead to Arduino ground. This option has the advantage of producting sound from a very small package and being easily portable.
  2. connect an 8 ohm speaker to digitial pin 9 with a 100 ohm resistor between the Arduino and the speaker lead; then connect the other speaker lead to Arduino ground. As you would expect, this will produce better sound quality than the buzzer in option 1.
  3. connect Arduino digital pin 9 and Arduino ground to an amplified speaker, such as the Monobox amp featured in Make Magazine's issue 34. This will give the best sound of the three options. See:


Step 4: Music Notation Used in This Project

There are various ways of representing musical notes in computer programs. Our goal was to devise or adopt a notation that would be the easiest for someone to use when entering a string of notes for the Arduino to play. We examined Ringtone Text Transfer Language (RTTTL), Music Macro Language (MML), Midi - and after some consideration developed our own, which you will see below. (If you think some modification would be more convenient for entering notes, please leave a comment with this Instructable.)

This project uses the following convention to represent a musical note.
Third octave, C sharp, quarter note, dotted. (Dotted notes play for 1.5 x the normal duration.)
An eighth rest.
The range of octaves is 1 to 7.
Sharps and flats are entered as in MML: either '#' or '+' represents a sharp, '-' represents a flat.
When you enter notes, it is not necessary to repeat the octave or duration if it has not changed from the previous note.
That is, you can enter 3c4 3d4 3g4 r4 for three quarter notes and a quarter rest in the third octave, or if you find it easier you could enter 3c4 d g r and get the same result.

The part of the sketch that contains the musical notes is located near the end of the sketch, in the setup() function. This is the part you will change to enter your own toneTune. Substitute your musical notes for the ones you find there and you will be composing Arduino music of your own.

Step 5: How It Works

The Arduino tone command produces a sound when it is given a sound frequency and a duration. Here is the Arduino reference page on the tone command:


tone (pin, frequency, duration) produces a sound. In this project we have used digital pin 9 for the tone output. Duration is in milliseconds, so we have selected a standard duration of 1500 milliseconds to represent a whole note. You could change this if you prefer a different musical tempo.

How the sketch converts a string of musical notes in this format -"4f#2 g8 a16 " - to musical frequencies:

  1. the notes are separated by spaces - the Arduino sketch uses this to divide the string into separate notes.
  2. then each note is examined to produce a note frequency. For instance with the note 4f#2 the sketch first determines that this note is in the fourth octave, the base note is F, its frequency is raised to F# and it has the duration of a half note.
  3. the Arduino sketch uses an array of note frequencies at the seventh octave. To determine the proper frequency at a specified octave, this seventh octave frequency is divided by a divisor. The divisors are also in an array.
  4. you can find out more about musical notes and their frequencies here:


Step 6: Write Your Own Tunes

This section of the setup() function contains the musical notes and rests:

playMusic(String( // Tone Tune for Arduino
"5f4 3f8 e f "

"5f16 r e r f r "

"4f16 r e r f r8 "


(That is not all of the notes in the demonstration tune - it's just a sample of the notes in the first three lines of the toneTune tune.)

Comments on this part of the sketch:

  1. 'playMusic' is the name of the function that holds the string of notes and rests.
  2. '// Tone Tune for Arduino' is a comment that gives the title of the tune.
  3. "5f4 3f8 e f " is the first line of notes. The first note is 5th octave F quarter note. The second is 3rd octave F eighth note. The third is 3rd octave E eighth note. The fourth is 3rd octave F quarter note.
  4. This line of notes plays exactly the same as if the notes had been entered like this: "5f4 3f8 3e8 3f8 ". You can enter the octave and duration for each note, but if you prefer you can enter these elements only when they change.
  5. Rests are entered as simply 'r' and the duration, so 'r4' is a quarter rest.
  6. Regardless of the foregoing you will likely find it helpful to enter the octave and note duration for the first note in each line. That makes it easier to read.
  7. Each line of notes and rests ends with a space before the final " (double quotation mark). If you neglect to include this final space you could have trouble loading the data line.
  8. You can use dotted notes (such as '5f4.') to increase the duration by adding an extra half duration to the note length.
  9. Go ahead and change the notes in the demonstration tune to play a melody of your own. If you want to save separate tunes, give each .ino file a different file name.

A further example: these are the notes used in the version of 'Happy Birthday' that you hear in the demonstration video (Step 2 of this Instructable)

"4d8 d# d d# e4 d g f# "
"2f16 r 6f r 3f r 6f r "

"4d8 d# d d# e4 d a g "

"2f16 r 6a r 3as r 6g r "

"4g8 g# g g# 5g4 e "

"5c8 c# c c# 4b+4 d "

"3c16 r d r c r d r 4d "

"5g16 r g r e r e r c r 2c r "

"5d r 3d r 5c r 6c r 7c "

<p>I programmed something like this a long time ago into my arduino. It's so great! I'm excited to see more stuff from you in the future. </p>

About This Instructable




More by turnturtle:Arduino Ukulele Tuner Arduino + TFT --> Turtle Graphics Arduino + TFT =  Visual Music 
Add instructable to: