Arduino - Piezo Buzzer
Hardware Required
1 | × | Arduino UNO or Genuino UNO | |
1 | × | USB 2.0 cable type A/B | |
1 | × | Piezo Buzzer | |
1 | × | Breadboard | |
n | × | Jumper Wires |
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

How It Works
- If connecting VCC to the positive pin, piezo buzzer generates the constant sound
- 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.


Wiring Diagram

Image is developed using Fritzing. Click to enlarge image
How To Program For Piezo Buzzer
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 two functions: tone() and noTone() 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
- Click Upload button on Arduino IDE to upload code to Arduino
- 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[].
Video Tutorial
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.
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.