1. Overview
Buzzer is an integrated structure of electronic sound device, which is powered by DC voltage. In application, it is widely used in computers, printers, copiers, alarms, electronic toys, automotive electronic equipment, telephones and timers.
Buzzers can be divided into active ones(built-in drive circuits) and passive ones(external drive) according to that whether they includes an excitation source.
Active buzzers contain oscillation source inside, which can sound at a fixed frequency once be triggered. They are convenient in program control and features high sound pressure.
Passive ones, however, do not include oscillating sources. If we directly power a passive buzzer via DC voltage, it will emit no sound. According to needs, we generally drive through square waves, whose frequency determines the sound tones.
To sum up, The active buzzer contains a vibration source, and its sound frequency is fixed. Yet there is no vibration source inside the passive one, so it must be driven by square waves whose frequency can be changed to control sounds.
2. Parameters
Operating voltage: DC 3.3 ~ 5V
Operating temperature: -10°C ~ +50°C
Control signal: Digital signal
Dimensions: 48 x 24 x 18 mm (without housing)
Positioning hole: Diameter of 4.8 mm
Interface: Telephone socket
3. Principle
Music is an invisible art. It is a language that narrates emotions and thoughts.
The foundation of music, as we all know, is note. We can compose a variety of melodies and rhythms with different notes. Of all the notes, the most basic are seven:
We can compose a variety of melodies and rhythms with these notes.
Passive buzzer module must be drive by square waves to emit sound. We can change the duty cycle of PWM to control square waves.
The greater the duty cycle is, the lauder the sound will be.
And the tones vary from different frequency of PWM.
The higher the frequency is, the higher the tone will be.
Frequencies in Tone C:
Note |
Frequency(Hz) |
Note |
Frequency(Hz) |
Note |
Frequency(Hz) |
---|---|---|---|---|---|
Flat 1 Do |
262 |
Natural 1 Do |
523 |
Sharp 1 Do |
1047 |
Flat 2 Re |
294 |
Natural 2 Re |
587 |
Sharp 2 Re |
1175 |
Flat 3 Mi |
330 |
Natural 3 Mi |
659 |
Sharp 3 Mi |
1319 |
Flat 4 Fa |
349 |
Natural 4 Fa |
698 |
Sharp 4 Fa |
1397 |
Flat 5 So |
392 |
Natural 5 So |
784 |
Sharp 5 So |
1568 |
Flat 6 La |
440 |
Natural 6 La |
880 |
Sharp 6 La |
1760 |
Flat 7 Si |
494 |
Natural 7 Si |
988 |
Sharp 7 Si |
1967 |
4. Wiring Diagram
The passive buzzer is a digital module. It needs to be connected to a blue socket of the main board.
Here we connect the module to socket 3. From the port view of the kidsIOT board, socket 3 is digital port io26.
5. Test Code
/*
Project name: 5_Buzzer
Function: buzzer plays Flat DO,Re,Mi,Fa,So,La,Si
Author: keyestudio
Hardware connection:
- Connect to port 3
Library:
- none (no need to import additional library)
Cautions:
- Ensure connect to port 3
- Before uploading test code, please correctly connect to the development board and port
*/
#define beeppin 26 //Set pin to IO26
void setup() {
pinMode(beeppin, OUTPUT); //Set the pin to output
}
void loop() {
tone(beeppin, 262); //Flat DO plays 500ms
delay(500);
tone(beeppin, 294); //Flat Re plays 500ms
delay(500);
tone(beeppin, 330); //Flat Mi plays 500ms
delay(500);
tone(beeppin, 349); //Flat Fa plays 500ms
delay(500);
tone(beeppin, 392); //Flat So plays 500ms
delay(500);
tone(beeppin, 440); //Flat La plays 500ms
delay(500);
tone(beeppin, 494); //Flat Si plays 500ms
delay(500);
noTone(beeppin); //stop playing
delay(1000);
}
6. Test Result
After uploading the test code, the buzzer repeats the tones in sequence: Do, Re, Mi, Fa, So, La, Si.
7. Extension
Now that we’ve learned how to make the buzzer sound, let’s play a song on it. Here I decided to play Happy Birthday. The first thing is to find a numerical score of the music:
And if we want to know whether it’s high or low, we have to look at this:
Test Code:
/*
Project name: 5_Buzzer_Music
Function: buzzer plays Happy Birthday
Author: keyestudio
Hardware connection:
- Connect to port 3
Library:
- none (no need to import additional library)
Cautions:
- Connect to port 3
- Before uploading test code, please correctly connect to the development board and port
*/
#define beeppin 26 //Set pin to IO26
// do、re、mi、fa、so、la、si
int doremi[] = {262, 294, 330, 370, 392, 440, 494, //falt(low) 0-6
523, 587, 659, 698, 784, 880, 988, //natural(middle) 7-13
1047,1175,1319,1397,1568,1760,1967}; //sharp(high) 14-20
// Locate the position number in the doremi[] array based on the low, middle and high frequencies of the simplified numbers
int happybirthday[] = {5,5,6,5,8,7,5,5,6,5,9,8,5,5,12,10,8,7,6,11,11,10,8,9,8};
// beats
int meter[] = {1,1,2,2,2,4, 1,1,2,2,2,4, 1,1,2,2,2,2,2, 1,1,2,2,2,4};
void setup() {
pinMode(beeppin, OUTPUT); //set the pin to output
}
void loop() {
for( int i = 0 ; i <= 24 ;i++){ //i<=24: here are only 24 keys in the score
//Emit a waveform with frequency using the tone() function
tone(beeppin, doremi[happybirthday[i] - 1]);
delay(meter[i] * 300); //beats
noTone(beeppin);//stop
}
}