This article will provide you with a circuit diagram, code, and working Arduino simulation of a piezoelectric buzzer.
You find a lot of applications of Piezo buzzers around. CPU warning sound, haptics, keypress confirmation, etc. It is straightforward to interface Piezoelectric to an Arduino UNO.
In this article, you will see how to connect a Piezo buzzer to the Arduino UNO. The same code and concept work with Arduino Mega or Arduino Nano.
How does a Piezoelectric buzzer work?The piezoelectric buzzer works by creating vibrations at a certain frequency. There will be an internal excitation and oscillation circuitry whose frequency we can change to mimic different notes. Higher notes will have a higher frequency (in the audible range).
Making it work with Arduino is easy due to the libraries already available. We will see about them now in the next section.
Piezoelectric buzzer simulation part details:
Documents page: https://docs.wokwi.com/parts/wokwi-buzzer
Let us build a simple tone generator project in the next section
Example 1: Christmas Jingle Bells on Wokwi Arduino SimulatorChange the variable speakerOut
to change the pin number to which you have connected the positive pin of the buzzer. To change the speed of the song, play with the tempo variable.
The code
// * Jingle Bells
// * Original Composition :
// * Composed by :
// * Coded By - http://www.instructables.com/id/Arduino-Controlled-Flashing-Christmas-Fairy-Lights/step5/Code-for-Jingle-Bells/
// * Use BSD Clause 2 License for Distribution
// * Collection by GitHub User @abhishekghosh - https://github.com/AbhishekGhosh/Arduino-Buzzer-Tone-Codes
// TONES ========================================== // Start by defining the relationship between
// note, period, & frequency.
#define C 2100
#define D 1870
#define E 1670
#define f 1580 // Does not seem to like capital F
#define G 1400
// Define a special note, 'R', to represent a rest
#define R 0
// SETUP ============================================
// Set up speaker on a PWM pin (digital 9, 10 or 11)
int speakerOut = 10;
// Do we want debugging on serial out? 1 for yes, 0 for no
int DEBUG = 1;
void setup() {
pinMode(speakerOut, OUTPUT);
if (DEBUG) {
Serial.begin(9600); // Set serial out if we want debugging
}
}
// MELODY and TIMING =======================================
// melody[] is an array of notes, accompanied by beats[],
// which sets each note's relative length (higher #, longer note)
int melody[] = {E, E, E,R,
E, E, E,R,
E, G, C, D, E, R,
f, f, f,f, f, E, E,E, E, D ,D,E, D, R, G ,R,
E, E, E,R,
E, E, E,R,
E, G, C, D, E, R,
f, f, f,f, f, E, E, E, G,G, f, D, C,R };
int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.
// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES
// Initialize core variables
int tone_ = 0;
int beat = 0;
long duration = 0;
// PLAY TONE ==============================================
// Pulse the speaker to play a tone for a particular duration
void playTone() {
long elapsed_time = 0;
if (tone_ > 0) { // if this isn't a Rest beat, while the tone has
// played less long than 'duration', pulse speaker HIGH and LOW
while (elapsed_time < duration) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tone_ / 2);
// DOWN
digitalWrite(speakerOut, LOW);
delayMicroseconds(tone_ / 2);
// Keep track of how long we pulsed
elapsed_time += (tone_);
}
}
else { // Rest beat; loop times delay
for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
delayMicroseconds(duration);
}
}
}
// LET THE WILD RUMPUS BEGIN =============================
void loop() {
for (int i=0; i<MAX_COUNT; i++) {
tone_ = melody[i];
beat = 50;
duration = beat * tempo; // Set up timing
playTone();
// A pause between notes...
delayMicroseconds(pause);
}
}
Connection diagram
Here is the simulation link:https://wokwi.com/arduino/projects/309974695864697409
Support/feedback/suggestions?you have many ways to ask for help, suggest a feature or share your feedback
- Open an issue on GitHub
- Visit Facebook group
- Hop on to Discord Server!
- leave a comment here π
Comments