You are on page 1of 11

tone();

Generates a square wave of the specified frequency (and 50% duty


cycle) on a pin. A duration can be specified, otherwise the wave
continues until a call to noTone(). The pin can be connected to a
piezo buzzer or other speaker modules to play tones.

Only one tone can be generated at a time. If a tone is already


playing on a different pin, the call to tone() will have no effect. If
the tone is playing on the same pin, the call will set its frequency.

Syntax
tone(pin, frequency); or
tone(pin, frequency, duration);
For example:

tone(12, 440);
//pin 12 will play a tone of 440Hz

tone(12, 440, 500);


//pin 12 will play a tone of 440Hz for 500ms

Note: If you want to play different pitches on multiple pins, you need to call noTone() on
one pin before calling tone() on the next pin. That means, you can't play a chord, you can
only play 1 note at any 1 time.
How to play C D E F G musical notes like a musical
instrument?

Hint: First, setup your piezo buzzer. Input the frequency


of the note one at a time. You must use delay to provide
ample time for the note to finish playing and pause
between each note.
Answer:
void setup(){
pinMode(13, OUTPUT);
}
void loop(){
tone(13, 262, 500);
delay(600);
tone(13, 294, 500);
delay(600);
tone(13, 330, 500);
delay(600);
tone(13, 349, 500);
delay(600);
tone(13, 392, 500);
delay(600);
}

Note: The 500ms duration for each tone require delay of an equivalent or higher duration in order to
complete the full 500ms duration for that particular tone. The difference of 100ms between tone and delay
is intended for the “pause” between each note, if not the notes will join together continuously without a
pause in between.
How to play Twinkle Twinkle Little Stars without typing
the frequencies of the notes?

Hint: You will need to use a shortcut called a header file


(file with extension .h). This file is part of a library code
(which you will learn in detail in the future lessons) that
stores code that enable the programmer to conveniently
retrieve the required variables for the main program.
We can refer to 7 octaves of musical notes and frequency
of the notes using the link below:

https://www.arduino.cc/en/Tutorial/ToneKeyboard?fro
m=Tutorial.Tone3
The code will use 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.

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. Save the file as “pitches.h”. Type in what
you see on the next slide.
Note Name

Frequency(Hz)

Type in what you see here as


“pitches.h” file to produce our
first song:

Twinkle Twinkle Little Stars!


Answer:
#include “pitches.h”

void setup(){
pinMode(13, OUTPUT);
}
void loop(){
tone(13, NOTE_C4, 500);
delay(600);
tone(13, NOTE_C4, 500);
delay(600);
tone(13, NOTE_G4 500);
delay(600);
tone(13, NOTE_G4, 500);
delay(600);
tone(13, NOTE_A4, 500);
delay(600);
tone(13, NOTE_A4, 500);
delay(600);
tone(13, NOTE_G4, 1000);
delay(1200);
Continued:
tone(13, NOTE_F4, 500);
delay(600);
tone(13, NOTE_F4, 500);
delay(600);
tone(13, NOTE_E4 500);
delay(600);
tone(13, NOTE_E4, 500);
delay(600);
tone(13, NOTE_D4, 500);
delay(600);
tone(13, NOTE_D4, 500);
delay(600);
tone(13, NOTE_C4, 1000);
delay(1200);
}

Note: The fornt part of the song has been shown to you. Note how NOTE_(X)4 helps you to internally
replace the note with the respective frequency of the musical note. That's the usefulness of a library!

You might also like