Arduino: Making Sound with Buzzer
In this project, we will create a melody by playing various notes one after the other with Buzzer.
What is Buzzer?
We can make a tiny speaker analogy for the buzzer.
It would not be correct to say exactly the speaker. Because the Buzzer cannot produce sounds as detailed as the speaker. The volume is also not as loud as the speaker.
So, what can we do with Buzzer? Buzzer is often used to beep in various tones.
Required Ingredients List
Arduino
USB Connection Cable Breadboard
buzzer
100 Ohm resistance Jumper cables
Note and Frequency Information
Each sound has its own frequency value. The notes used in music also have their own frequency values. A higher/higher frequency will produce a higher pitched sound. You can use the list in the link below to learn the frequency values of the notes.
Notes can be displayed as names or as letters. Do – Re – Mi – Fa – Left – La – Si
C- D – E – F – G – A – B
Application - Sheet Music
In this application, we will create a melody by playing the notes we learned when we first met with the music lesson. "do-re-mi-fa-sol-la-si-do”
While writing our codes, we will work with the letters of the notes, not the names. If we want to work with names, it should be noted that we cannot use do as variable name during coding. The word do has a special meaning in programming, it is defined as a keyword and is used in do-while loops. Keywords in programming language cannot be used as variable names.
Arduino Circuit Diagram
Project Codes
#define buzzer A1
// frequency values of notes
int C = 262; // do
int D = 294; // re
int E = 330; // mi
int F = 349; // fa
int G = 392; // left
int A = 440; // la
int B = 494; // one
int Cc = 523; // do (thin)
//We collect the notes into a string in order to be able to move on the notes comfortably.
int []notes = {C, D, E, F, G, A, B, Cc};
int number of notes = 8;
//code block (setup) that will run only once at startup
void setup()
{
// Play all the notes in order. Leave a half-second (500 milliseconds) gap between two notes.
for(int i = 0; i < noteNum ; i++)
{
tone(buzzer,notes[i]); delay(500); noTone(buzzer); delay(20);
}
}
// code block (loop) that will run recursively after the initialization codes (setup) are run and completed
void loop()
{
}
Explanation of Codes
We memorized the frequency equivalent of each note by defining a variable with the appropriate name. Variables are used to hold information in memory while programming. The aim is to make it possible to use that information again when needed.
Since we will need to reach the note information in the memory in order from the do note to the thin do note, we have collected this information in an array. Simple counter logic is used to programmatically access the array elements sequentially, increasing by 1 each time to reach the next element. We use for loops on arrays to achieve this.
We prepared our for loop, we accessed every element (every note) in the array one by one, and we made a sound at the appropriate frequency from the buzzer using the tone function. tone function
When it starts, it will continue to make the same tone until the stop command is received. We want each note to play for half a second (500 milliseconds) and then stop.
We ran the delay function by giving the value 500 as a parameter, and we made our Arduino board wait for 500 milliseconds, that is, half a second, without doing anything. During these 500 milliseconds we continued to hear our note.
Afterwards, we cut the buzzer sound with the noTone function. If we had not waited 500 milliseconds by using the delay function, perhaps we would not have heard the note at all, since the tone command and the noTone command would work one after the other.
Finally, we give a tiny 20-millisecond pause before ending a note and moving on to the next note. To do this, we used the delay function again.
How Does the Arduino Board Work?
The setup function runs only once when the Arduino board starts to work (power on). When the setup function is finished running, the loop function is executed. The loop function is run again each time it completes. This repetitive operation continues until the Arduino board is de-energized or an external factor presses the reset button on the board.
Application Operation
In this example, we did not use the loop function and wrote all of our codes in the setup function. When our Arduino board gets electricity and connects it to work, the codes in the setup function will work and the notes will be heard from start to finish. Then there will be silence. Because there are codes for playing each note only once in the setup and there is no code in the loop function. In order to hear the same notes again, we can cut off the electricity of our Arduino board and give it again, or we can press the reset button on the board.
Duty
We want the same melody to play continuously. After the melody is complete, there should be a 3 second silence before it starts again. The same circuit diagram can be used, but the codes need to be arranged according to our needs. Good luck to you in this task.
http://www.ercanbozkurt.blogspot.com ercanbozkurt.c64@gmail.com
This article is taken from the 6th issue of Çırak Magazine.