Introduction
Piezo buzzers are electronic components that produce sound by vibrating a thin metal plate inside a cylindrical housing when an electrical current is applied to them. Interfacing a piezo buzzer with an Arduino board allows us to control the sound output of the buzzer and integrate it with other electronic devices.
In this article, we will explore how to interface a “Piezo Buzzer” with an Arduino and write a simple program to generate different sounds.
What is Piezo Buzzer?
A piezo buzzer is an electronic component that produces sound by vibrating a thin metal plate, typically made of ceramic or quartz crystal, inside a cylindrical housing when an electrical current is applied to it. The vibration of the metal plate generates sound waves that can be heard as a tone or beep.
Hardware Components
To interface a Piezo Buzzer with Arduino, you’ll need the following hardware components to get started:
Components | Value | Qty |
---|---|---|
Arduino UNO | – | 1 |
USB Cable Type A to B | – | 1 |
DC Power for Arduino | – | 1 |
Piezo Buzzer | – | 1 |
Push button | – | 8 |
Breadboard | – | 1 |
Jumper Wires | – | 1 |
Piezo Buzzer Pinout
Pin Name | Pin Description |
---|---|
GND | Ground Pin |
VCC | +5v Pin |
Piezo Buzzer Circuit
Make connections according to the circuit diagram given below.
Wiring / Connections
Arduino | Buzzer |
---|---|
D8 | Red wire |
GND | GND |
Installing Arduino IDE
First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.
Installing Libraries
Before you start uploading a code, download and unzip the following libraries at /Program Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.
A Simple Melody Tone On Arduino Using A Buzzer
#include "pitches.h"
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}
void loop() {
// no need to repeat the melody.
}
Now copy the following code and upload it to Arduino IDE Software.
Code Explanation
This is an Arduino code that generates a simple melody using a piezo buzzer. The code includes a library called “pitches.h,” which contains pre-defined values for musical notes. The melody is stored in an array called “melody,” which contains the pitch values for each note. The duration of each note is stored in another array called “noteDurations,” which specifies the length of each note.
In the “setup” function, the code iterates over the notes of the melody and calculates the duration of each note based on its type (quarter note, eighth note, etc.). It then plays the note using the “tone” function, which generates a square wave at the specified frequency for the specified duration. After each note is played, there is a short pause to distinguish it from the next note.
The “loop” function is empty in this code because there is no need to repeat the melody. Once the melody has played through once in the “setup” function, the program exits.
Piano Project Using Buzzer And Arduino
#include "pitches.h"
#define SPEAKER_PIN 8
const uint8_t buttonPins[] = { 12, 11, 10, 9, 7, 6, 5, 4 };
const int buttonTones[] = {
NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};
const int numTones = sizeof(buttonPins) / sizeof(buttonPins[0]);
void setup() {
for (uint8_t i = 0; i < numTones; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
}
void loop() {
int pitch = 0;
for (uint8_t i = 0; i < numTones; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
pitch = buttonTones[i];
}
}
if (pitch) {
tone(SPEAKER_PIN, pitch);
} else {
noTone(SPEAKER_PIN);
}
}
This code creates a mini piano using Arduino. The pitches library is included, and a speaker pin and button pins are defined. The notes for each button are defined in an array. In the setup function, the button pins are set to input mode with pull-up resistors, and the speaker pin is set to output mode. In the loop function, the code checks if any button is pressed by iterating over the button pins and checking their digital state. If a button is pressed, the corresponding tone is played on the speaker using the tone() function. If no button is pressed, the speaker is turned off using the noTone() function.
Applications
Here are some common applications of Piezo Buzzer:
- Alarms and warning systems
- Electronic toys and games
- Timer and clock functions
- Vehicle reverse warning systems
- Home automation systems
- Musical instruments
- Robotics and drones
- Medical equipment
- Industrial automation systems
- Security systems
- Elevator and escalator systems
- Personal safety devices
- Telecommunications equipment.
Conclusion
interfacing a piezo buzzer with an Arduino board is a simple and fun way to experiment with sound generation in your electronic projects. With just a few lines of code, you can create different types of sounds, including melodies, alarms, and beeps.