Play a Melody using the tone() function

Play a melody with a Piezo speaker.

This example shows how to use the

tone()
command to generate notes. It plays a little melody you may have heard before.

Hardware Required

  • Arduino board

  • piezo buzzer or a speaker

  • hook-up wires

Circuit

circuit

Schematic

schematic

Code

The code below uses an extra file, pitches.h. This file contains all the pitch values for typical notes. For example, NOTE_C4 is middle C. NOTE_FS4 is F sharp, and so forth. This note table was originally written by Brett Hagman, on whose work the tone() command was based. You may find it useful whenever you want to make musical notes.

The main sketch is as follows:

1/*
2
3 Melody
4
5 Plays a melody
6
7 circuit:
8
9 - 8 ohm speaker on digital pin 8
10
11 created 21 Jan 2010
12
13 modified 30 Aug 2011
14
15 by Tom Igoe
16
17 This example code is in the public domain.
18
19 https://www.arduino.cc/en/Tutorial/Tone
20
21*/
22
23#include "pitches.h"
24
25// notes in the melody:
26int melody[] = {
27
28 NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
29};
30
31// note durations: 4 = quarter note, 8 = eighth note, etc.:
32int noteDurations[] = {
33
34 4, 8, 8, 4, 4, 4, 4, 4
35};
36
37void setup() {
38
39 // iterate over the notes of the melody:
40
41 for (int thisNote = 0; thisNote < 8; thisNote++) {
42
43 // to calculate the note duration, take one second divided by the note type.
44
45 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
46
47 int noteDuration = 1000 / noteDurations[thisNote];
48
49 tone(8, melody[thisNote], noteDuration);
50
51 // to distinguish the notes, set a minimum time between them.
52
53 // the note's duration + 30% seems to work well:
54
55 int pauseBetweenNotes = noteDuration * 1.30;
56
57 delay(pauseBetweenNotes);
58
59 // stop the tone playing:
60
61 noTone(8);
62
63 }
64}
65
66void loop() {
67
68 // no need to repeat the melody.
69}

To make the pitches.h file, either click on the button just below the serial monitor icon and choose "New Tab", or use Ctrl+Shift+N.

Then paste in the following code:

1/*************************************************
2
3 * Public Constants
4
5 *************************************************/
6
7#define NOTE_B0 31
8#define NOTE_C1 33
9#define NOTE_CS1 35
10#define NOTE_D1 37
11#define NOTE_DS1 39
12#define NOTE_E1 41
13#define NOTE_F1 44
14#define NOTE_FS1 46
15#define NOTE_G1 49
16#define NOTE_GS1 52
17#define NOTE_A1 55
18#define NOTE_AS1 58
19#define NOTE_B1 62
20#define NOTE_C2 65
21#define NOTE_CS2 69
22#define NOTE_D2 73
23#define NOTE_DS2 78
24#define NOTE_E2 82
25#define NOTE_F2 87
26#define NOTE_FS2 93
27#define NOTE_G2 98
28#define NOTE_GS2 104
29#define NOTE_A2 110
30#define NOTE_AS2 117
31#define NOTE_B2 123
32#define NOTE_C3 131
33#define NOTE_CS3 139
34#define NOTE_D3 147
35#define NOTE_DS3 156
36#define NOTE_E3 165
37#define NOTE_F3 175
38#define NOTE_FS3 185
39#define NOTE_G3 196
40#define NOTE_GS3 208
41#define NOTE_A3 220
42#define NOTE_AS3 233
43#define NOTE_B3 247
44#define NOTE_C4 262
45#define NOTE_CS4 277
46#define NOTE_D4 294
47#define NOTE_DS4 311
48#define NOTE_E4 330
49#define NOTE_F4 349
50#define NOTE_FS4 370
51#define NOTE_G4 392
52#define NOTE_GS4 415
53#define NOTE_A4 440
54#define NOTE_AS4 466
55#define NOTE_B4 494
56#define NOTE_C5 523
57#define NOTE_CS5 554
58#define NOTE_D5 587
59#define NOTE_DS5 622
60#define NOTE_E5 659
61#define NOTE_F5 698
62#define NOTE_FS5 740
63#define NOTE_G5 784
64#define NOTE_GS5 831
65#define NOTE_A5 880
66#define NOTE_AS5 932
67#define NOTE_B5 988
68#define NOTE_C6 1047
69#define NOTE_CS6 1109
70#define NOTE_D6 1175
71#define NOTE_DS6 1245
72#define NOTE_E6 1319
73#define NOTE_F6 1397
74#define NOTE_FS6 1480
75#define NOTE_G6 1568
76#define NOTE_GS6 1661
77#define NOTE_A6 1760
78#define NOTE_AS6 1865
79#define NOTE_B6 1976
80#define NOTE_C7 2093
81#define NOTE_CS7 2217
82#define NOTE_D7 2349
83#define NOTE_DS7 2489
84#define NOTE_E7 2637
85#define NOTE_F7 2794
86#define NOTE_FS7 2960
87#define NOTE_G7 3136
88#define NOTE_GS7 3322
89#define NOTE_A7 3520
90#define NOTE_AS7 3729
91#define NOTE_B7 3951
92#define NOTE_C8 4186
93#define NOTE_CS8 4435
94#define NOTE_D8 4699
95#define NOTE_DS8 4978

and save it as pitches.h

Learn more

You can find more basic tutorials in the built-in examples section.

You can also explore the language reference, a detailed collection of the Arduino programming language.

Last revision 2015/08/11 by SM

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.