ESP32 - Piezo Buzzer
This tutorial instructs you learn how to use the buzzer with ESP32, In detail, we will learn:
- How to connect the buzzer to ESP32
- How to program ESP32 to control buzzer to generate sound, melody
Hardware Used In This Tutorial
1 | Ć | ESP-WROOM-32 Dev Module | |
1 | Ć | USB Cable Type-C | |
1 | Ć | Piezo Buzzer | |
1 | Ć | Breadboard | |
1 | Ć | Jumper Wires | |
1 | Ć | (Optional) DC Power Jack | |
1 | Ć | (Recommended) Screw Terminal Expansion Board for ESP32 |
Or you can buy the following sensor kits:
1 | Ć | DIYables Sensor Kit (30 sensors/displays) | |
1 | Ć | DIYables Sensor Kit (18 sensors/displays) |
Introduction to Piezo Buzzer
The Piezo Buzzer is an electronic component that can make sound, beep or even play music.
Available in the market is a versatile 3V-24V active buzzer that can function as both a 3-5V active buzzer and a high-voltage buzzer (12V and above).
- When directly connected to an Arduino pin, this buzzer produces a standard sound, making it ideal for applications such as keypad sound indicators.
- When connected to a high-voltage source through a relay, it emits a loud sound, making it well-suited for warning signals.
Piezo Buzzer Pinout
Piezo Buzzer usually includes two pins:
- Positive (+) pin: The ESP32 generate the control signal to this pin (directly, or indirectly via relay)
- Negative (-) pin: connect this pin to GND (0V)
How Piezo Buzzer Works
Wiring Diagram between Piezo Buzzer and ESP32
This image is created using Fritzing. Click to enlarge image
If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.
ESP32 Code
Quick Instructions
- If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
- Do the wiring as above image.
- Connect the ESP32 board to your PC via a micro USB cable
- Open Arduino IDE on your PC.
- Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
- Copy the below code and paste to Arduino IDE
- Create the pitches.h file On Arduino IDE by:
- Either click on the button just below the serial monitor icon and choose New Tab, or use Ctrl+Shift+N keys.
- Give file's name pitches.h and click OK button
- Copy the below code and paste it to the created pitches.h file.
- Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
- Listen to the melody
Modifying ESP32 Code
Lt's modidy the ESP32 code to play Jingle Bells song.
We only need to change value of two arrays: int melody[] and int noteDurations[].
Video Tutorial
Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.