Arduino - Piezo Buzzer

About Piezo Buzzer

Piezo Buzzer is used to generate sound, beep or even melody of a song.

Pinout

Piezo Buzzer usually has two pins:

  • Negative (-) pin needs to be connected to GND (0V)
  • Positive (+) pin receive the control signal from Arduino
  • Piezo Buzzer Pinout

How It Works

  • If connecting VCC to the positive pin, piezo buzzer generates the constant sound
  • How Piezo Buzzer Works
  • If generating a square wave of the specified frequency (and 50% duty cycle) on the positive pin, the piezo buzzer generates tones. The different frequency makes a different tone. By changing the frequency of the signal on the positive pin, we can create the melody of a song.
  • How Piezo Buzzer Works

Wiring Diagram

Arduino Piezo Buzzer Wiring Diagram

Image is developed using Fritzing

How To Program

Thanks to the Arduino library, playing a melody is easy. We do NOT need to know how to generate the square wave. We just need to use the library.

Arduino Code

Quick Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • On Arduino IDE, Go to File Examples 02.Digital toneMelody example
/* Melody Plays a melody circuit: - 8 ohm speaker on digital pin 8 created 21 Jan 2010 modified 30 Aug 2011 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Tone */ #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. }
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Arduino IDE Upload Code
  • Enjoy the melody

Modifying Arduino Code

Now, we are going to modidy the code to play "Jingle Bells" song.

We only need to change value of two arrays: int melody[] and int noteDurations[].

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-piezo-buzzer */ #include "pitches.h" // notes in the melody: int melody[] = { NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5, NOTE_D5, NOTE_G5 }; // note durations: 4 = quarter note, 8 = eighth note, etc, also called tempo: int noteDurations[] = { 8, 8, 4, 8, 8, 4, 8, 8, 8, 8, 2, 8, 8, 8, 8, 8, 8, 8, 16, 16, 8, 8, 8, 8, 4, 4 }; void setup() { // iterate over the notes of the melody: int size = sizeof(noteDurations) / sizeof(int); for (int thisNote = 0; thisNote < size; 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. }

Video

incoming

Challenge Yourself

  • Use Piezo Buzzer to play the song you love
  • Automatically make alarm when someone approaches your valuable stuff. Hint: Refer to Arduino - Motion Sensor .

Alternative Piezo Buzzers

The above code also works with the following piezo buzzers:

Piezo Buzzer on Commercial Products

Piezo Buzzer on commercial products is usually very small in size, which can be integrated into small devices.

Comments

If you have any questions or suggestions, please comment on our YouTube channel