A buzzer is a simple sound-producing device that can be controlled using a microcontroller. There are two types of buzzers:
In this tutorial, we’ll use a buzzer connected to GPIO 4 of ESP32 DOIT DevKit v1 and make it beep using the Arduino IDE.
⚠️ If using a passive buzzer, we will control the sound frequency using tone()
function.
// Active Buzzer with ESP32 DOIT DevKit v1
// Buzzer at GPIO 4
int buzzerPin = 4;
void setup() {
pinMode(buzzerPin, OUTPUT);
}
void loop() {
digitalWrite(buzzerPin, HIGH); // Turn buzzer ON
delay(1000); // Wait 1 second
digitalWrite(buzzerPin, LOW); // Turn buzzer OFF
delay(1000); // Wait 1 second
}
Define Pin
int buzzerPin = 4;
Assign GPIO 4 for the buzzer.
Setup Function
pinMode(buzzerPin, OUTPUT);
Configure buzzer pin as output.
Loop Function
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(buzzerPin, LOW);
delay(1000);
Problem | Cause | Solution |
---|---|---|
No sound from buzzer | Wrong wiring | Connect + to GPIO 4 and – to GND |
Weak sound | Low current from GPIO | Use a transistor driver circuit |
Noise instead of tone | Passive buzzer used as active | Use tone() for passive buzzer |
ESP32 reset/restart | Buzzer draws too much current | Use external power with transistor |
Active buzzers work with simple HIGH/LOW signals. Passive buzzers need frequency signals for sound.
Yes, with a passive buzzer and Arduino tone library, you can play melodies.
Some buzzers need more current than ESP32 GPIO can supply. Use a transistor + external power.
Volume is fixed in most buzzers, but passive buzzers can change perceived loudness by varying PWM duty cycle.
Yes, as long as they are connected to different GPIOs and programmed correctly.