Jump to content

Buzzer

From cognifront

Interfacing Piezo Buzzer with Arduino UNO for Generating Sound and Music

Interfacing Piezo Buzzer with Arduino UNO for Generating Sound and Music demonstrates how a piezoelectric buzzer can be connected to an Arduino UNO board to generate sound and musical tones.

The piezoelectric buzzer is an electronic device commonly used for producing sound and music.

The buzzer can generate different sounds by applying electrical signals of different frequencies.

Arduino UNO → Digital Pin → Piezo Buzzer → Sound

Theory

A piezoelectric buzzer is an electronic device commonly used to produce sound and music.

The piezoelectric buzzer works on the inverse principle of piezoelectricity.

The buzzer contains a piezoelectric crystal and a diaphragm.

When voltage is applied to the piezoelectric element, the diaphragm gets stretched or compressed. These mechanical movements produce vibrations, which generate sound.

By applying signals of different frequencies, different tones can be generated.

Electrical Signal → Piezoelectric Element → Diaphragm Vibration → Sound

Using high-performance piezoelectric elements, loud, clear and pleasant electronic tones can be generated.

An appropriate frequency can be used to generate a particular musical tone.

Piezo Buzzer Pins

The piezo buzzer has two pins:

Pin Connection Function
+ Arduino UNO digital pin Data / signal input
− GND Ground

The pin marked with + is the data or signal pin and is connected to a digital output pin of the Arduino UNO.

The pin marked with − is the ground pin and is connected to the GND pin of the Arduino UNO.

Sound Generation

Sound is generated by applying an electrical signal to the piezoelectric element.

The frequency of the signal determines the frequency of the sound produced by the buzzer.

A higher frequency produces a higher-pitched sound, while a lower frequency produces a lower-pitched sound.

The Arduino UNO can generate different tones by changing the frequency of the signal supplied to the buzzer.

Applications

Piezoelectric buzzers are commonly used in:

  • Refrigerators
  • Typewriters
  • Automotive instruments
  • Security equipment
  • Electronic instruments
  • Sound and alert systems

Connections

Device Pin Arduino UNO Function
Piezo Buzzer + Digital GPIO Sound signal
Piezo Buzzer − GND Ground

Note: The exact Arduino UNO digital pin number depends on the pin selected in the program.

Algorithm

For Controlling Buzzer ON/OFF

The following algorithm is used for controlling the buzzer using digital output:

  1. Start the Arduino IDE.
  2. Declare a variable to assign a pin number of the Arduino UNO board.
  3. In the void setup() function, configure the selected pin as OUTPUT.
  4. In the void loop() function, make the selected pin HIGH.
  5. Give an appropriate delay.
  6. Make the selected pin LOW.
  7. Give an appropriate delay.
  8. Repeat the process continuously.

The overall operation is:

GPIO HIGH → Buzzer ON → Delay → GPIO LOW → Buzzer OFF → Repeat

Generating Different Frequencies Using Tone Function

The buzzer can also be used to generate different frequencies using the tone() function.

The tone function is used in the following format:

tone(pin no, frequency, duration);

Where:

Parameter Description
pin no Digital pin connected to the buzzer
frequency Frequency of the generated tone in Hz
duration Duration of the tone in milliseconds

Different frequencies can be supplied to the buzzer to generate different musical tones.

Musical Tone Generation

The buzzer can be used to generate a sequence of musical tones such as:

Sa → Re → Ga → Ma → Pa → Dha → Ni → Sa

The frequencies specified in the practical are:

Musical Tone Frequency Duration
Sa 3000 Hz 500 ms
Re 3100 Hz 500 ms
Ga 3200 Hz 500 ms
Ma 3300 Hz 500 ms
Pa 3400 Hz 500 ms
Dha 3500 Hz 500 ms
Ni 3600 Hz 500 ms
Sa 3700 Hz 500 ms

A delay of 1000 ms is provided between the tones.

Working

The Arduino UNO generates an electrical signal on the digital pin connected to the piezo buzzer.

For simple ON/OFF control, the digital pin is alternately set to HIGH and LOW with a suitable delay.

For generating musical tones, the tone() function is used to specify the frequency and duration of the sound.

The working sequence is:

  1. The Arduino UNO initializes the buzzer pin as an output.
  2. A frequency is applied to the buzzer.
  3. The piezoelectric element vibrates according to the applied signal.
  4. The vibration of the diaphragm produces sound.
  5. The tone continues for the specified duration.
  6. A delay is provided before the next tone.
  7. The next frequency is applied.
  8. The process continues for the complete sequence.

Arduino UNO → Frequency Signal → Piezoelectric Element → Vibration → Sound

Tone Sequence

The example tone sequence is:

tone(2, 3000, 500);
delay(1000);

tone(2, 3100, 500);
delay(1000);

tone(2, 3200, 500);
delay(1000);

tone(2, 3300, 500);
delay(1000);

tone(2, 3400, 500);
delay(1000);

tone(2, 3500, 500);
delay(1000);

tone(2, 3600, 500);
delay(1000);

tone(2, 3700, 500);
delay(1000);

Observation

Simple Buzzer

#define buz 2

void setup()
{
  pinMode(buz, OUTPUT);
}

void loop()
{
  digitalWrite(buz, HIGH);
  delay(1000);
  digitalWrite(buz, LOW);
  delay(1000);
}

Melody Playing

Download the melody playing buzzer example. It contains the .ino file and the pitches.h header file with tones frequency set.

Observation

When the buzzer is controlled using digital HIGH and LOW signals, the buzzer produces sound according to the applied signal.

When different frequencies are supplied using the tone() function, the buzzer generates different audible tones.

The sequence of frequencies:

3000 Hz → 3100 Hz → 3200 Hz → 3300 Hz → 3400 Hz → 3500 Hz → 3600 Hz → 3700 Hz

produces a sequence of different musical tones.

Result

The Arduino UNO successfully interfaces with the piezoelectric buzzer to generate sound.

The buzzer can be controlled using digital HIGH and LOW signals for ON/OFF operation and different frequencies can be generated using the tone() function to produce musical tones and sequences.