In this tutorial, we will learn how to use the SunFounder ESP32 microcontroller to play musical notes using a passive buzzer and a transistor. The project aims to demonstrate how to generate sound with specific frequencies by controlling the buzzer through the ESP32's GPIO pins.
We will be connecting a passive buzzer to the ESP32, which will allow us to produce different musical notes based on the frequency we provide. This setup includes using a transistor to switch the buzzer on and off at high speeds, creating sound waves that we can hear. For further clarification on the project, be sure to check out the video (in video at 00:30).
ESP32-13-buzzer-schematic
Hardware Explained
The main components used in this project include the SunFounder ESP32 microcontroller, a passive buzzer, and an NPN transistor (2N8050). The ESP32 is a powerful development board with built-in Wi-Fi and Bluetooth capabilities, making it versatile for various IoT applications. In this case, it will generate signals to control the buzzer.
The passive buzzer operates by generating sound when an alternating voltage is applied to it. Unlike active buzzers, which have a built-in oscillator, passive buzzers require an external signal to produce sound. The transistor acts as a switch, allowing the ESP32 to control the buzzer without directly powering it, ensuring that the current rating of the microcontroller is not exceeded.
S87050_transistor
Datasheet Details
Manufacturer
SunFounder
Part number
Passive Buzzer
Operating Voltage
3.3 V - 5 V
Resonant Frequency
2 kHz - 4 kHz
Current Consumption
≤ 30 mA
Sound Pressure Level
≥ 70 dB
Package
Through-hole
Notes / variants
None
Ensure correct polarity when connecting the buzzer.
Use a current-limiting resistor for the transistor base.
Connect the buzzer to a power supply compatible with its voltage rating.
Beware of exceeding the maximum current rating of the transistor.
Keep the wiring neat to avoid short circuits.
Test the circuit before uploading code to avoid component damage.
Use a breadboard for easy component connections.
Make sure to debounce inputs if using buttons.
Consider adding a heat sink for the transistor if used at high current.
Wiring Instructions
ESP32-13-buzzer-wriing
To wire the components, start by connecting the positive terminal of the passive buzzer to the 3.3V pin on the ESP32. The negative terminal of the buzzer should connect to the collector of the NPN transistor. The emitter pin of the transistor should be wired to the ground (GND) of the ESP32.
Next, connect a 1 kΩ resistor from the base of the transistor to GPIO pin 14 on the ESP32. This resistor limits the current flowing into the base of the transistor. Finally, ensure that all ground connections are tied together to provide a common reference for the circuit.
Code Examples & Walkthrough
const int buzzerPin = 14; // the buzzer pin
void setup()
{
pinMode(buzzerPin, OUTPUT); // Set as output
}
In the code, we define a constant variable buzzerPin which is set to 14, indicating the GPIO pin connected to the base of the transistor. In the setup() function, we set this pin as an OUTPUT, allowing the ESP32 to control the buzzer.
void loop()
{
for (int i = 0; i < 50; i++) // Loop 50 times and play a short tone each time
{
digitalWrite(buzzerPin, HIGH); // Set to HIGH to make the buzzer sound
delay(3); // Wait for 3 milliseconds
digitalWrite(buzzerPin, LOW); // LOW to turn off the buzzer
delay(3); //
}
delay(1000); // Wait for 1s before starting the next loop
}
The loop() function contains a for-loop that runs 50 times. Each iteration turns the buzzer on and off quickly, producing a tone. The delays of 3 milliseconds control how long the buzzer is activated and deactivated, while a 1-second delay at the end of the loop provides a pause before repeating.
Demonstration / What to Expect
Once the wiring is complete and the code is uploaded, you should hear a series of short tones produced by the buzzer. If the buzzer is not sounding, check your connections, especially the transistor's orientation and the resistor value. Ensure that the correct pin is used in the code and that the ESP32 is powered properly.
During the demonstration, if you experience issues, it may be due to incorrect wiring or using the wrong pin for the buzzer. Check the wiring against the instructions and ensure that the power supply is adequate (in video at 08:15).
Video Timestamps
00:00 Start
1:52 Introduction to the project
4:11 Wiring Explained
8:11 Selecting ESP32 board and COM port on Arduino IDE