HOW TO PLAY THE MUSICAL NOTES ON THE ARDUINO
Making sound on the arduino is an interesting project, this con be accomplish using different modules and devices depending on your project and choices. In this project, we’ll be looking at the way you can make sound with a buzzer. Buzzer used by hobbyist come in two types: The active buzzer and the passive buzzer. For this project, we are going to be using a passive buzzer.
A passive buzzer requires an AC signal to make a sound. It is like an electromagnetic speaker, where a changing input signal produces the sound, rather than producing a tone automatically. Unlike the active buzzer that only requires a one-shot DC, the passive buzzer needs some technicality in producing note. Note that trying to use the passive buzzer without setting the output frequency will lead to production of no sound by the passive buzzer.
The Major Musical notes includes A, B, C, D, E, F, G. Each of these notes has its own frequency. These frequencies depend on the octave in which the note is played. The frequencies 440Hz and 880Hz both correspond to the musical note A, but one octave apart. The next higher A in the musical scale would have the frequency 1760Hz, twice 880Hz. In the western musical scale, there are 12 notes in every octave. These notes are evenly distributed (geometrically), so the next note above A, which is B flat, has frequency 440 × β where β is the twelfth root of two, or approximately 1.0595. The next note above B flat, which is B, has frequency 440 × β 2.
Some Notes frequencies are shown below. To play an octave higher, you simply multiply the current octave by 2
A 440
B flat 466
B 494
C 523
C sharp 554
D 587
D sharp 622
E 659
F 698
F sharp 740
G 784
A flat 831
A 880
What will I need?
Arduino
Passive Buzzer (1)
Jumper Wires
The circuit diagram is shown below
In this tutorial, we’ll be producing the musical notes from A to G on two different octaves. The code is shown below
// Code to make the major musical notes using a passive buzzer
//Author: Adesola Samuel
//Date: November, 2020.
void setup() {
pinMode(7, OUTPUT); // This line is nor really necessary for the program to function, you can experiment by removing this line ad running the code
}
void loop() {
// the tone function takes three parameters (the pin number, the tone frequency, the number of seconds to sound)
tone(7, 440, 2000); //A
delay(1000);
tone(7, 494, 2000); //B
delay(1000);
tone(7, 523, 2000); //C
delay(1000);
tone(7, 587, 2000); //D
delay(1000);
tone(7, 659, 2000); //E
delay(1000);
tone(7, 698, 2000); //F
delay(1000);
tone(7, 784, 2000); //G
delay(1000);
tone(7, 880, 2000); //A
delay(1000);
//Playing the melodies an octave higher
tone(7, 494*2, 2000);
delay(1000);
tone(7, 523*2, 2000);
delay(1000);
tone(7, 587*2, 2000);
delay(1000);
tone(7, 659*2, 2000);
delay(1000);
tone(7, 698*2, 2000);
delay(1000);
tone(7, 784*2, 2000);
delay(1000);
tone(7, 880*2, 2000);
delay(1000);
//You can use the notone() function instead of the delay()
}