How To Use: Tone Function - Arduino Playing The James Bond Theme

On this tutorial you will see how to use the tone function to play monophonic music.

Materials List: 

1 x Arduino Uno R3

1 x Breadboard

1 x Speaker 8 Ω / 0,25 W

1 x Rotary Potentiometer 10 KΩ

1 x Capacitor 100μF / 25 V


How it works:

The syntax of tone function is:

tone (pin, frequency, duration);

The parameters are:


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 tone function generates a square wave of the specified frequency on a pin during the time of duration field (optional).

Each musical note is determined by a frequency and you can see on table below the frequencies:

Based on this table and pentagram above the archive pitches.h (of toneMelody example) was created.

The array melody[] is used to store the notes, using names to correspond to the note and frequency.

For example, the frequency of note A4 (A for "La" and 4 for the 4th octave) is 440 Hz in the array it will be called NOTE_A4.

The musical score represents the notes and the duration by symbols on the pentagram.

Below you see clef-G and clef-F.

The duration is determined by the following symbols:

In array noteDurations[], the durations were represented by numbers.See table below.


Value in array

whole note


half note


quarter note


eighth note


sixteenth note


           thirty-second note


            sixty-fourth note



The potentiometer was used to control volume and the capacitor as coupling.

Below you see the circuit on Fritzing.


The Sketch:

Verify circuit, and upload Sketch to Arduino:

#include "pitches.h"
#define NO_SOUND 0 // make the rests in music

//array of notes
int melody[] = {

// note duration: 1 = whole note, 2 = half note, 4 = quarter note, 8 = eighth note, etc.
int noteDurations[] = {

int pace = 1450; // change pace of music("speedy")
void setup() {
  for (int Note = 0; Note <54; Note++) {//counter of Notes (54 limit the array)
    int duration = pace/noteDurations[Note];//Adjust duration with the pace of music
    tone(8, melody[Note],duration); //Play note

// to distinguish the notes, set a minimum time between them.

void loop() {
  //to repeat song, push reset button.
//End of Sketch

Let's listen James Bond!!!



Views: 20205

Tags: Arduino, Bond, James, Library, Music, Tone

Comment by نهاد عبدالله حسين السلمان on January 14, 2013 at 4:31am
Hi - this truly stunning what spatial, which could create such programs audio Effects
Comment by نهاد عبدالله حسين السلمان on January 14, 2013 at 4:43am

You could get this project file


You need to be a member of GarageLab (arduino, electronics, robotics, hacking) to add comments!

Join GarageLab (arduino, electronics, robotics, hacking)

Latest Activity

Jim Kelley joined Marcelo Rodrigues's group


Group for the Arduino communitySee More
15 hours ago
Jim Kelley posted a discussion

Help with selecting sensors

I am new to Arduino but am a quick study and can get up to speed fast. I can see some tremendous…See More
16 hours ago
GarageLab posted a blog post

These 3D-Printed Ants Could Be The Factory Workers Of The Future

These 3D-printed ants scuttle around much like the real thing. But loaded with sensors and wireless…See More
16 hours ago
Profile IconAdele Smurthwaite, Neil Cheng, John Hardie and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
17 hours ago
GarageLab posted blog posts
Profile IconOleg and Scott Runner joined GarageLab (arduino, electronics, robotics, hacking)
GarageLab left a comment for RMullen
"Hi RMullen! I'd like to let you know that FRANK KWABENA is no longer a member of…"
daniel gabriel updated their profile
daniel gabriel left a comment for daniel gabriel
"Good Day Mr. Wabena, i dont see any interesting on my profile but i really appreciated that you…"
RMullen replied to Lauren Elizabeth's discussion Scammed...?
"Yes there is another one sending out the same message now.  This is the message I got: FRANK…"
Rick Lin is now a member of GarageLab (arduino, electronics, robotics, hacking)
GarageLab posted a blog post
Mar 24
Profile IconCalvin M and harsha joined GarageLab (arduino, electronics, robotics, hacking)
Mar 24
GarageLab posted a blog post

These Colorful Liquid Droplets Chase Each Other Like Living Organisms

Scientists at Stanford University were at first perplexed by the strange behavior they observed…See More
Mar 23
GarageLab posted blog posts
Mar 20
Profile IconJody Bowie, Kevin Kvarda, Tony Stoklosa and 2 more joined GarageLab (arduino, electronics, robotics, hacking)
Mar 19
Sergey commented on GarageLab's blog post Tutorial: How to use the Monster Moto Shield
"Good day. Can you help me? I took this sketch, but it doesn't working( "
Mar 19
Profile IconDonald Soji Adedokun, Ankur Deo and Hannu Miettinen joined GarageLab (arduino, electronics, robotics, hacking)
Mar 18
GarageLab posted a blog post
Mar 18
GarageLab posted a blog post

Iron Man Delivers Kid’s Robotic Arm: Proof that Tony Stark Has a Heart

Alex Pring now has one heck of a story to tell his friends. Not only did the 7-year old meet Iron…See More
Mar 16

© 2015   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service