Skip to content

Buzzer / Piezo · Espressif ESP32 guide

Controlling Passive Piezo Buzzer with Espressif ESP32

Piezo buzzer that requires PWM — play melodies and variable-pitch tones, not just fixed beeps.

Updated November 14, 2026 Arduino C++Also forArduino Uno

Passive Buzzer practical reference

A piezo buzzer that requires a driven PWM signal, allowing variable-pitch tones and simple melody playback rather than a fixed beep.

Key specifications

  • Operating voltage: 3V - 5V DC
  • Control: Requires a driven square wave (PWM or the Arduino tone() function) — pitch equals drive frequency
  • Usable frequency range: ~2kHz - 5kHz for clearly audible tones (piezo response falls off outside this band)
  • Silent when idle: No internal oscillator — a constant HIGH or LOW produces no sound, unlike an active buzzer

Pinout and connection notes

tone() on Arduino drives this directly from a digital pin with no extra hardware; on ESP32 use the ledc PWM API instead since tone() isn't implemented the same way there.

  • VCC / I/O: Signal input on breakout modules with 3 pins — a raw 2-lead piezo just has + and -
  • GND: Ground
  • I/O: On 3-pin modules: the driven PWM/tone signal; on bare 2-lead piezos, drive across the two leads directly

Integration checklist

  1. Confirm the exact part number and breakout-board revision.
  2. Verify supply voltage, current demand, logic levels, and signal direction against the datasheet.
  3. Check controller pin assignments and bus address or interface mode against the example code.
  4. Power up with external loads disconnected, then test signals before full integration.
  5. If readings or communication fail, check power and ground first, then pin mapping, interface settings, and library initialization.

Troubleshooting and reproducibility

Record the board used, wiring, library/version, supply arrangement, and measured behavior. Use the selected board-specific wiring example as the source of truth. Never assume visually similar modules share identical voltage tolerance or pin order.

Passive buzzer drive and protection

A passive piezo buzzer needs an alternating drive signal; a steady HIGH or LOW does not create a continuous tone. Use a square-wave or PWM-capable output and select a frequency within the particular buzzer's audible response. The resonant frequency and sound level vary between parts, so treat broad frequency ranges as starting points and verify the actual component.

Check whether the buzzer is a bare piezo element or a module with additional circuitry. A bare piezo is largely capacitive and is often driven directly for small signals, but a transistor driver may be appropriate for higher sound level or a module with greater current demand. Confirm the GPIO current limit and the buzzer's voltage rating before wiring. If the module includes an indicator LED or driver, follow its pin labels and datasheet rather than assuming a bare two-terminal piezo pinout.

For a quiet idle state, stop the waveform and set the output to a defined inactive level. If the sound is weak or distorted, check frequency, supply voltage, polarity where marked, mounting and whether the buzzer is being driven with sufficient amplitude. Do not connect an unknown buzzer to a GPIO until its type and current requirements are identified.

Wiring & code

Setting up Passive Buzzer with Espressif ESP32

Wiring

Component pinEspressif ESP32 pinNote
I/O (signal)GPIO25
GNDGND
VCCNot connected (optional)
sketch.ino
const int BUZZER_PIN = 25;

// Arduino-ESP32 3.x: tone() drives the pin with the LEDC peripheral.
// (ledcSetup()/ledcAttachPin() from core 2.x were removed in 3.0.)
void setup() {
}

void loop() {
  tone(BUZZER_PIN, 1000); // 1 kHz tone
  delay(500);
  noTone(BUZZER_PIN);     // silence
  delay(500);
}
  • On Arduino-ESP32 3.x, tone() and noTone() work just like on the Uno; underneath, the core drives the pin with the LEDC peripheral.
  • Older tutorials call ledcSetup() and ledcAttachPin(): those are core 2.x functions that were removed in 3.0, so that code no longer compiles. In 3.x the LEDC equivalent is ledcAttach(pin, freq, resolution) followed by ledcWriteTone(pin, freq).
  • Same 2-5kHz sweet spot and "silent without an oscillating signal" caveat as the Arduino Uno.

Keep learning

Related guides & projects