Raspberry Pi - Piezo Buzzer
This tutorial instructs you how to use Raspberry Pi to control the piezo buzzer. In detail, we will learn :
- How a piezo buzzer works
- How to the piezo buzzer to an Raspberry Pi board
- How to program the Raspberry Pi to make piezo buzzer generate sound.
- How to program the Raspberry Pi to make piezo buzzer play a melody of a song
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of Piezo Buzzer
A Piezo Buzzer is used to produce sound, beeps, or even a melody from a song.
Available in the market is a 3V-24V active buzzer that serves dual purposes: functioning both as a 3-5V active buzzer and a high-voltage buzzer (12V and beyond).
- Connecting this buzzer directly to an Arduino pin produces a standard sound, making it ideal for applications such as sound indicators, like keypad sounds.
- On the other hand, when connected to a high voltage through a relay, it emits a loud sound, making it suitable for warning signals.
The Piezo Buzzer Pinout
Piezo Buzzer generally has two pins:
- The Negative (-) pin should be connected to GND (0V)
- The Positive (+) pin receives the control signal from Raspberry Pi (directly, or indirectly via relay)
How Piezo Buzzer Works
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Raspberry Pi Code
Detailed Instructions
- Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
- Make sure your Raspberry Pi is connected to the same local network as your PC.
- Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
- If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
- Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
- Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
- Create a Python script file buzzer.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
- Listen to the melody of a song emmited by the piezo buzzer.
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.
Modifying Raspberry Pi Code
Now, we will be altering the code to play the "Jingle Bells" song.
- Create a Python script file buzzer_Jingle_Bells.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
- You can compare this code with the previous code to see the differences
With this modification, the code will now play the melody of "Jingle Bells" using the piezo buzzer attached to the Raspberry Pi. Enjoy the festive tune!
Video Tutorial
Challenge Yourself
- Employ a Piezo Buzzer to play your favorite song.
- Utilize a Raspberry Pi Motion Sensor to automatically sound an alarm when someone is near your valuable items. Refer to Raspberry Pi - Motion Sensor for more information.