Arduino Nano 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 Arduino Nano ESP32
- How to program Arduino Nano ESP32 to control buzzer to generate sound, melody
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
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 Arduino Nano 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 Arduino Nano ESP32
- The wiring diagram between piezo buzzer and arduino nano esp32
This image is created using Fritzing. Click to enlarge image
- The wiring diagram between piezo buzzer module and arduino nano esp32
This image is created using Fritzing. Click to enlarge image
Arduino Nano ESP32 Code
Detailed Instructions
To get started with Arduino Nano ESP32, follow these steps:
- If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
- Wire the components according to the provided diagram.
- Connect the Arduino Nano ESP32 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the Arduino Nano ESP32 board and its corresponding 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 Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
- Listen to the melody
Modifying Arduino Nano ESP32 Code
Lt's modidy the Arduino Nano ESP32 code to play Jingle Bells song.
We only need to change value of two arrays: int melody[] and int noteDurations[].