ES - 07 - Buzzers
ES - 07 - Buzzers
A.Y. 2021/22
Embedded Systems
Buzzers
• The main function of this is to convert the signal from audio to sound.
• Based on the various designs, it can generate different sounds like alarm,
music, bell & siren.
Types of Buzzer
A buzzer is available in different types which include the following.
• Piezoelectric
• Electromagnetic
• Mechanical
• Electromechanical
• Magnetic
Piezoelectric Effect
• Piezoelectric Effect is the ability of certain materials to
generate an electric charge in response to applied
mechanical stress.
• The active buzzer is the simplest module to produce a sound of about 2 kHz
The main difference between the active buzzer and the passive buzzer is that the active buzzer
generates sound independently, the user must simply turn it on or off by applying a voltage to the
contacts, because an active buzzer has a built-in oscillating source, so it will generate a sound when
electrified.
A passive buzzer requires a signal source, it does not have such a source so it will not tweet if DC
signals are used; instead, you need to use square waves whose frequency is between 2K and 5K
to drive it.
The active buzzer will produce a louder sound signal than its competitor. The frequency of the
sound emitted by the active buzzer is 2,5 kHz +/- 300 Hz.
The power supply voltage varies from 3.5V to 5V.
Active Buzzer
Connection Schematic & Wiring diagram
First Sketch – Active Buzzer
• The sound produced by the int buzzerPin = 12;
buzzer is not controlled by the
sketch in terms of frequency. void setup() {
pinMode (buzzerPin, OUTPUT);
• So we are not controlling the }
frequency of the sound.
void loop(){
• We're just switching the buzzer digitalWrite (buzzerPin, HIGH);
on and off. delay(500);
digitalWrite (buzzerPin, LOW);
This is OK for some applications delay(500);
• if you just want an alarm to tell }
the user that something is
Producing the sound is not
happening or something is not blocking for your sketch because
happening you can use just one you can just put the pin high.
single tone, maybe switching it.
The buzzer will produce the sound
and then the sketch can go on an
perform other operations.
Changing Frequency of an Active Buzzer
• In this case we are producing a square
wave signal
int buzzer = 7;
• Because the pin is going HIGH and LOW
every 1 ms. void setup(){ pinMode(buzzer,OUTPUT
}
We can compute actually the
void loop(){
frequency:
• in the first loop we have 500 Hz
unsigned char i;
• in the second loop we have 250 Hz for(i=0;i<80;i++) {
digitalWrite(buzzer,HIGH);
In the first loop, for 160 ms we are producing a sound
delay(1); digitalWrite(buzzer,LOW);
that has a frequency that is a combination between
500Hz and 2300Hz (Buzzer’s frequency), while in the delay(1);
second loop for 400 ms the frequency will be a }
combination between 250Hz and 2300Hz for(i=0;i<100;i++) {
digitalWrite(buzzer,HIGH);
An active buzzer is not meant to be controlled by a delay(2);
not constant Voltage value. digitalWrite(buzzer,LOW);
It is built to be used by a constant value which we
delay(2);
can provide for a certain amount of time to produce
the sound and then to be switched off in order to }
stop the sound. }
Basically what we did in the original sketch.
Passive Buzzer
Frequency of a Passive Buzzer
• If we connect instead of the active buzzer a passive one, with the
previously sketch, we can hear a frequency created only by using the
instruction inside the sketch.
• But in this case, in order to produce the sound by using a loop for
changing the value of the pin, we can’t do something else while the sound
is produced.
• A duration can be specified, otherwise the wave continues until a call to noTone().
• The pin can be connected to a piezo buzzer or other speaker to play tones.
• Only one tone can be generated at a time. If a tone is already playing on a different pin, the call
to tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency.
• Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other
than the Mega).
Syntax
tone(pin, frequency)
tone(pin, frequency, duration)
Parameters
pin: the Arduino pin on which to generate the tone.
frequency: the frequency of the tone in hertz. Allowed data types: unsigned int.
duration: the duration of the tone in milliseconds (optional). Allowed data types: unsigned long.
Play a song with a passive Buzzer
Why we are using the pin number 8?