The buzzer consists of a vibrating blade with the piezoelectric effect.
Piezoelectricity is a property possessed by certain minerals that deform when subjected to an electric field. It allows the transformation of electrical energy into vibration, enabling the production of simple notes and melodies.
Buzzers can be found in alarm devices, computers, timers, and more. There are two types of buzzers: Active and Passive buzzers.
There are some fundamental differences between Active and Passive buzzers:
Active Buzzer: It can generate sound when it receives a continuous voltage.
Passive Buzzer: Functions like a speaker, so an audio signal needs to be provided for it to produce sound.
To differentiate between the two, you can apply a continuous voltage to both buzzers. The one that emits sound will be the active buzzer. However, both buzzers operate similarly, which is why the programs we will propose will be the same for both types.
The electronic symbol of the buzzer is composed of two circles with a plus sign (+).
To make the program work, you need to install the library dedicated to the buzzer. This library contains all the notes playable by the buzzer. The buzzer library is in .zip format, so to install it, click on “Sketch,” then “Include Library,” and “Add .Zip Library.”
If you encounter any problems installing the library, we recommend checking out our dedicated tutorial.
There are other types of possible notes that you can play. For this, you can use the library and input the note reference as in the melody list of the program or directly input the frequency.
By directly inputting the frequency, you no longer need to add the library to your code.
As we have just seen, we can program the buzzer with or without the library.
Here is the function to program your buzzer with the library:
tone(Pin,Note,Duration);
You will find an example of using this function in the programs below.
Here is the function to program your buzzer without the library:
digitalWrite(Pin_buzzer,HIGH);
You will find an example of using this function in the programs below.
Now, let’s see the circuits you need to create to use the programs. All codes work with these circuits.
Here is the schema for the passive buzzer:
Here is the schema for the active buzzer:
To replicate the schema of the active buzzer on Fritzing, you need to install the active buzzer component. We recommend our Fritzing course if you have difficulty installing the component.
We will present two codes to make your buzzer work. The first one works with the library, and the second one works without it.
Here you have a sequence of notes from low to high using the library:
#include "pitches.h" // Library for the passive buzzer
// note for the melody
int melody[] = {NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_C6}; // Here is the note included in the Library
int duration = 500; // duration for each melody
void setup() {
}
void loop() {
for (int thisNote = 0; thisNote < 8; thisNote++) { // For all Note of the melody list
tone(13, melody[thisNote], duration); // duration of the note played
delay(1000); // New notes after one second of break
}
delay(2000);
}
Here you have an alarm without the library:
int buzzer = 13;// Pin of the active buzzer
void setup()
{
pinMode(buzzer,OUTPUT);//We initialize the pin of the buzzer as an output
}
void loop()
{
unsigned char i; // counting each loop
while(1) // Infinite loop
{
//first note
for(i=0;i<80;i++)
{
digitalWrite(buzzer,HIGH); // Buzzer on
delay(1);//break
digitalWrite(buzzer,LOW); // buzzer off
delay(1);//break
}
//Second note
for(i=0;i<100;i++)
{
digitalWrite(buzzer,HIGH); // buzzer on
delay(2);//break
digitalWrite(buzzer,LOW); // buzzer offf
delay(2);//break
}
}
}
As we have seen, we can play multiple notes with our buzzer. But it’s essential to find a good melody. For this, we recommend translating the notes from a sheet music you have chosen into frequency Hz.
For example, we have translated the song “Jingle Bells” as follows:
#define Pin_buzzer 13
// Jingle Bells
int melody = {
659, 659, 659,
659, 659, 659,
659, 784, 523, 587,
659,
698, 698, 698, 698,
698, 659, 659, 659, 659,
659, 587, 587, 659,
587, 784
};
int tempo[] = {
8, 8, 4,
8, 8, 4,
8, 8, 8, 8,
2,
8, 8, 8, 8,
8, 8, 8, 16, 16,
8, 8, 8, 8,
4, 4
};
void setup(void) {
pinMode(13, OUTPUT); // Buzzer
}
void loop() {
song_func();
}
int song = 0;
void song_func() {
int size = sizeof(melody) / sizeof(int);
for (int Note = 0; Note < size; Note++) {
int duration_note = 1000 / tempo[Note]; // To calculate the duration we divide the seconds by the type of the note
buzz(Pin_Buzzer, melody[Note], duration_note); // We call the buzz function
delay(duration_note * 1.30); // Break between each notes.
buzz(Pin_Buzzer, 0, duration_note); // Stop the music
}
}
void buzz(int pin_buzzer, long frequence, long length) {
long wainting_time = 1000000 / frequency / 2; // We caculate the value between each transition
long numCycles = frequency * length / 1000;
for (long i = 0; i < numCycles; i++) {
digitalWrite(pin_buzzer, HIGH); // We play the music
delayMicroseconds(temps_attente);
digitalWrite(pin_buzzer, LOW); // We stop the music
delayMicroseconds(wainting_time);
}
}