Buzzer
Interfacing Piezo Buzzer with ESP32 for Generating Sound and Music
| Piezo Buzzer Interface | |
|---|---|
| Controller | ESP32 |
| Component | Piezoelectric Buzzer |
| Number of Pins | 2 |
| Data Pin | + Pin |
| Ground Pin | − Pin |
| Control | Digital Output |
| Function | Sound and Music Generation |
Interfacing Piezo Buzzer with ESP32 for Generating Sound and Music demonstrates how a piezoelectric buzzer can be connected to an ESP32 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.
ESP32 → 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 |
|---|---|---|
| + | ESP32 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 ESP32.
The pin marked with − is the ground pin and is connected to the GND pin of the ESP32.
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 ESP32 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 | ESP32 | Function |
|---|---|---|---|
| Piezo Buzzer | + | Digital GPIO | Sound signal |
| Piezo Buzzer | − | GND | Ground |
Note: The exact ESP32 GPIO number depends on the GPIO selected in the program.
Schematic Diagram
The piezo buzzer is connected between an ESP32 digital output pin and ground.
ESP32 Digital GPIO → Buzzer (+)
ESP32 GND → Buzzer (−)
Algorithm
For Controlling Buzzer ON/OFF
The following algorithm is used for controlling the buzzer using digital output:
- Start the Arduino IDE.
- Declare a variable to assign a pin number of the ESP32 board.
- In the void setup() function, configure the selected pin as OUTPUT.
- In the void loop() function, make the selected pin HIGH.
- Give an appropriate delay.
- Make the selected pin LOW.
- Give an appropriate delay.
- 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 ESP32 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:
- The ESP32 initializes the buzzer pin as an output.
- A frequency is applied to the buzzer.
- The piezoelectric element vibrates according to the applied signal.
- The vibration of the diaphragm produces sound.
- The tone continues for the specified duration.
- A delay is provided before the next tone.
- The next frequency is applied.
- The process continues for the complete sequence.
ESP32 → 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
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 ESP32 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.