Arduino UNO R4 - Piezo Buzzer
In this guide, we will learn how to control a buzzer using an Arduino UNO R4. We will cover the details on how to do this.
- Difference between active and passive buzzer
- How piezo buzzer works
- How to connect a piezo buzzer to Arduino UNO R4
- How to program Arduino UNO R4 to make piezo buzzer produce sounds and melodies
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 Buzzer
Buzzers create sounds like beeps or melodies. They are available in different types, each with unique features. Let's look into these types based on specific characteristics:
- Method of Control:
- Active Buzzers
- Passive Buzzers
- Mechanism for Creating Sound:
- Piezo Buzzers
- Standard Buzzers
- Voltage Requirements:
- Low Voltage (3-5V)
- High Voltage (12V)
- Active Buzzer:
- It makes noise when electricity is given to it.
- It must have constant electricity to keep making noise.
- It is simple to use with a power supply.
- Usually found in simple alarm systems.
- Passive Buzzer:
- It needs a separate signal that changes to make noise.
- It can make various sounds by adjusting the signal.
- It needs a more complex setup to work.
- Often used for making music or different sounds.
- Piezo Buzzer:
- Operates with piezoelectric crystals.
- Emits high-pitched, clear sounds.
- Flexible in producing different tones and frequencies.
- Efficient and often used in alarms and musical devices.
- Regular Buzzer:
- Works with electromagnetic coils.
- Generates simple, buzzy sounds.
- Has few variations in tone.
- Commonly found in simple devices like doorbells and basic alarms.
- If you connect this buzzer to the Arduino UNO R4 pin, it makes a standard sound, good for things like keypad noises.
- If you connect this buzzer through a relay to high-voltage, it makes a loud sound, good for alarms.
- Negative (-) pin: connect to GND (0V)
- Positive (+) pin: connect directly or through a relay to receive the control signal from Arduino UNO R4
- When you connect VCC to the positive pin, the piezo buzzer makes a continuous sound.
- To create different tones with a piezo buzumper, send a square wave of a specific frequency to the positive pin. Different frequencies produce different tones. To play a melody, change the frequency of the square wave.
- The passive buzzer does not make a continuous sound by just connecting VCC to the positive pin, unlike the active buzzer.
- Similar to the active buzzer, when you apply a square wave of a certain frequency (and 50% duty cycle) to the positive pin, the piezo buzzer will produce sounds. Different frequencies result in different tones. You can form a melody by altering the frequency of the signal on the positive pin.
Let's look at it more closely.
Active Buzzer vs Passive Buzzer
Piezo Buzzer vs Regular Buzzer
In the market, you can find a 3V-24V active buzzer. You can use it as a 3-5V active buzzer or as a high voltage buzzer (12V...).
In this guide, we will cover how to use buzzers that need 3-5V, both active and passive types. For buzzers that require 12V, see our guide on the Arduino UNO R4 - 12V buzzer by clicking here: Arduino UNO R4 - 12V buzzer tutorial
Pinout
A buzzer typically has two connectors.
How an Active Buzzer Works
How an Passive Buzzer Works
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
How To Program For Buzzer
Using the Arduino UNO R4 library makes playing a melody simple. You don't need to understand how to create a square wave. You only need to use two functions from the library: tone() and noTone().
Arduino UNO R4 Code
Detailed Instructions
Follow these instructions step by step:
- If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
- Connect the piezo buzzer to the Arduino Uno R4 according to the provided diagram.
- Connect the Arduino Uno R4 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
- Copy and paste the provided code into the Arduino IDE.
- To make a file called pitches.h in the Arduino IDE, do one of the following:
- Click the button below the serial monitor icon and select New Tab.
- Alternatively, you can press Ctrl+Shift+N on your keyboard.
- Name the file as pitches.h and click the OK button.
- Copy the code below and paste it into the file named pitches.h.
- Click the Upload button in Arduino IDE to load the code onto Arduino UNO R4.
- Listen to the music.
Modifying Arduino UNO R4 Code
Now, we will change the code to play the song "Jingle Bells".
We just need to modify two arrays: int melody[] and int noteDurations[].
Video Tutorial
Challenge Yourself
- Play your favorite song with a Piezo Buzzer.
- Set up an alarm that goes off if someone gets close to your belongings. **Hint**: See Arduino UNO R4 - Motion Sensor.