Music Made Easy with tone()
Generating organized sound on the Arduino got a whole lot simpler with the release of revision 0018 of the software. The PWM logic needed to control the pitch produced by a simple speaker or Piezo buzzer is now wrapped in a simple call to tone.
Calling tone allows you to generate a square wave of a specific frequency (at a 50% duty cycle) on a single pin. This new function has two forms:
- tone( pin, frequency )
- tone( pin, frequency, duration )
the first of which will cause the wave to continue until a call to noTone(), and the second which will cause the wave to continue for the millisecond duration you specify.
A few notes to keep in mind when using tone():
- the PWM generator can affect only one pin at a time. That is, it’s not possible to generate multiple waves on different pins. So sorry, but no multitonal 8-bit harmony. In fact, if you call tone() using a different pin while an existing pin is tone()ing, the call will have no effect.
- neither of these calls block, so if you’re using them to create music you need to add calls to delayto allow the tone to play for any period of time. If you don’t subsequent calls to tone() will change the frequency on the pin, regardless of any duration you specified.
A Fun Demo
Here’s a demo of the use of tone() to play a familiar tune. This code was adapted from the original tone sample by Tom Igoe, and the physical setup is the same here - you’ll need to hook up a small speaker (8-ohm) or Piezo buzzer on pin 8, like so:

The music was transcribed from the original sheet music into an array of pitch / duration pairs (see lines 30-114), showing you just how literal the process of making music with your Arduino can be.
/* Melody Demo of building a melody as a simple data structure for easy playback. circuit: * 8-ohm speaker or Peizzo buzzer on digital pin 8 created 21 Jan 2010 by Tom Igoe Adapted from the Tone example by Tom Igoe: http://arduino.cc/en/Tutorial/Tone sheet music reference: http://www.scribd.com/doc/14547104/Mario-Theme-Song-Sheet-Music */ #include "pitches.h" #define THIRTYSECOND 32 #define SIXTEENTH 16 #define EIGHTH 8 #define TRIPLET 12 #define QUARTER 4 #define HALF 2 #define WHOLE 1 //notes in the melody: int melody[] = { NOTE_C6, SIXTEENTH, 0, EIGHTH, NOTE_G5,SIXTEENTH, 0,EIGHTH, NOTE_E5,SIXTEENTH, // 10 0,EIGHTH, NOTE_A5,SIXTEENTH, 0,SIXTEENTH, NOTE_B5,SIXTEENTH, 0,SIXTEENTH, //20 NOTE_AS5,SIXTEENTH, NOTE_A5,SIXTEENTH, 0,SIXTEENTH, NOTE_G5, TRIPLET, NOTE_E6, TRIPLET, //30 NOTE_G6, TRIPLET, NOTE_A6,SIXTEENTH, 0, SIXTEENTH, NOTE_F6, SIXTEENTH, NOTE_G6,SIXTEENTH, //40 0, SIXTEENTH, NOTE_E6,SIXTEENTH, 0,SIXTEENTH, NOTE_C6, SIXTEENTH, NOTE_D6, SIXTEENTH, //50 NOTE_B5,SIXTEENTH, 0, EIGHTH, // 54 -- end of first phrase 0, EIGHTH, NOTE_G6, SIXTEENTH, NOTE_FS6,SIXTEENTH, //60 NOTE_F6,SIXTEENTH, NOTE_DS6,SIXTEENTH, 0, SIXTEENTH, NOTE_E6, SIXTEENTH, 0,SIXTEENTH, //70 NOTE_GS5, SIXTEENTH, NOTE_A5, SIXTEENTH, NOTE_C6, SIXTEENTH, 0, SIXTEENTH, NOTE_A5, SIXTEENTH,// 80 NOTE_C6, SIXTEENTH, NOTE_D6, SIXTEENTH, 0, EIGHTH, NOTE_G6, SIXTEENTH, NOTE_FS6,SIXTEENTH, //90 NOTE_F6,SIXTEENTH, NOTE_DS6,SIXTEENTH, 0, SIXTEENTH, NOTE_E6, SIXTEENTH, 0,SIXTEENTH, //100 NOTE_C7, SIXTEENTH, 0,SIXTEENTH, NOTE_C7, SIXTEENTH, NOTE_C7, SIXTEENTH, 0, SIXTEENTH, //110 0, EIGHTH, 0, EIGHTH, NOTE_G6, SIXTEENTH, NOTE_FS6,SIXTEENTH, NOTE_F6,SIXTEENTH, //120 NOTE_DS6,SIXTEENTH, 0, SIXTEENTH, NOTE_E6, SIXTEENTH, 0,SIXTEENTH, NOTE_GS5, SIXTEENTH, //130 NOTE_A5, SIXTEENTH, NOTE_C6, SIXTEENTH, 0, SIXTEENTH, NOTE_A5, SIXTEENTH, NOTE_C6, SIXTEENTH, //140 NOTE_D6, SIXTEENTH, 0, EIGHTH, NOTE_DS6,SIXTEENTH, 0, EIGHTH, NOTE_D6, SIXTEENTH, //150 0, EIGHTH, NOTE_C6, SIXTEENTH, 0,EIGHTH, NOTE_G4, SIXTEENTH, NOTE_G4, SIXTEENTH, // 160 0, SIXTEENTH, NOTE_C4, SIXTEENTH, 0, SIXTEENTH }; void setup() { } void loop() { for( int repeat = 0; repeat < 2; repeat++ ) { for (int thisNote = 0; thisNote < 54; thisNote += 2) { playNoteByIndex( thisNote ); } } for (int thisNote = 54; thisNote < 168; thisNote += 2) { playNoteByIndex( thisNote ); } } void playNoteByIndex( int 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 = 1500/melody[thisNote + 1]; tone(8, 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; delay(pauseBetweenNotes); }
Enjoy!