What is a buzzer?
A buzzer is an audio signaling device -Converts a signal to sound. It's a device which produces sound and may be electromechanical, piezoelectric or purely mechanical. Buzzers can be used in communication circuits, alarm circuits, timers among others. A buzzer has two pins; the positive and negative. When using buzzers with Arduino, the positive terminal is connected to 5V while the negative terminal is grounded.
Types of buzzers
There are two main types of buzzers;
The active buzzer - This type of buzzer can produce sound directly by just connecting it into a battery ( A current passing through the buzzer produces sound). This is because active buzzers contain an inbuilt oscillator.
The passive buzzer - This type of buzzer requires an ac voltage signal source in order to produce sound. Passive buzzers can produce a number of different tones by varying the frequency of the input voltage signal.
Since the main focus of this article is to get these buzzers to produce sound with Arduino, we are not going to discuss the internal functionality of the buzzers.
How to differentiate between an active buzzer and a passive buzzer
If you have two buzzers or even more than that and would like to determine whether they are an active or passive buzzer, the surest method is to connect a voltage (typically 5V for Arduino but practical buzzers have different kinds of rating depending on the application) to the positive terminal and ground the other end. An active buzzer will produce a continuous tone until you disconnect it from the power source. A passive buzzer on the other hand, will produce a pop/ click sound. Most of the time, the active buzzers used with Arduino or any micro-controller out there have a backside cover while the passive buzzers do not.
Connecting the active buzzer to Arduino
As previously stated, you only need to provide 5V to the positive terminal and ground the other end. The positive and negative signs should be indicated on the buzzer. We could connect the positive terminal directly to the 5V pin in our Arduino but that would make it difficult to control the behaviour of the buzzer.For us to be able to control the output of our active buzzer we connect the positive terminal to a digital pin, allowing us to decide programmatically when the buzzer should be turned ON/ OFF.
Connect the buzzer to digital pin 8 and the negative terminal to ground. Instead of buzzing continuously we are going to produce a tone by turning the buzzer ON(HIGH) for sometime ( the delay dt in the program), turn it OFF (LOW) for sometime (dt2). Repeating this process will produce a tone. You can vary the delay time and see what kind of sound is produced.
The last part of the program is some form of pause before the tone plays again. See the program below.
//Buzzer connected to pin 8
int buzzPin = 8;
//delay variables for the buzzer
int dt = 100;
int dt2 = 80;
//delay for the program
int dt3 = 1000;
int i;
void setup() {
// buzzPin (pin 8) is an output
pinMode(buzzPin, OUTPUT);
}
void loop() {
// during each iteration of loop() buzz 10 times repeatedly
for (i = 0; i<=10; i=i+1) {
digitalWrite(buzzPin, HIGH);
delay(dt);
digitalWrite(buzzPin, LOW);
delay(dt2);
}
// Turn the buzzer off, then delay for a second before looping through the loop again
digitalWrite(buzzPin,LOW);
delay(dt3);
}
Connecting the passive buzzer to Arduino
Just like the active buzzer, the connection for the passive buzzer is no different. Connect the positive terminal to a digital pin - say digital pin 9 and the negative terminal to ground. The passive buzzer can produce a tone by varying the frequency of the input signal. As a simple example, we can just turn the buzzer on and off at given intervals. The intervals can be changed by varying the delay times, producing a different input wave signal and hence a different tone.
// Passive buzzer connected at digital pin 9
int passive = 9;
//Delay times fot the passive buzzer
int dt = 400;
int dt2 = 300;
void setup() {
// The passive buzzer at pin 9 is an output
pinMode(passive, OUTPUT);
}
void loop() {
// Make a tone by turning the buzzer on and off
digitalWrite(passive,HIGH);
delayMicroseconds(dt2);
digitalWrite(passive,LOW);
delayMicroseconds(dt);
}
If you encountered any problems following through the code, please see my other article on getting started with Arduino.