Professional Documents
Culture Documents
Syntax
tone(pin, frequency); or
tone(pin, frequency, duration);
For example:
tone(12, 440);
//pin 12 will play a tone of 440Hz
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?
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?
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.
Frequency(Hz)
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!