Wiring guide

How to wire a buzzer to an ESP32

To wire a buzzer to an ESP32, connect 1 to GPIO 13 (D13) and 2 to GND. A small passive piezo runs straight from a 3.3 V pin with its other leg on ground; it is a little quieter than at 5 V. The sketch below uses no library beyond what the board includes. It plays a four-note phrase and repeats it, printing Playing once and Again after each phrase.

  • 3.3 V logic
  • Library: none
  • 2 wires
ESP32 · buzzerlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Pin to pin

Buzzer to ESP32 wiring table

Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.

Buzzer pinESP32 pinWire colorWhy
Pin 1GPIO 13 (D13)ambertone() puts a square wave at the note’s frequency on this pin.
Pin 2GNDblueGround. A passive piezo has no polarity.
Voltage

Power and logic levels for buzzer and ESP32

Logic level

The ESP32’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.

This pair

A small passive piezo runs straight from a 3.3 V pin; it is quieter than at 5 V. On an ESP32 DevKit tone() makes the square wave in hardware (LEDC), so the core is free while a note plays. A magnetic buzzer draws more than a pin should give, so drive one through a transistor.

Pins to leave alone

GPIO 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.

The sketch

The working code for the ESP32

Library: none, the sketch makes the note with tone(). This is the sketch the circuit above runs.

Tones on ESP32 GPIO 13 (D13)
const int BUZZER_PIN = 13;   // GPIO 13 (D13)

// C, E, G, C: an arpeggio, a quarter second a note.
const int NOTES[] = {523, 659, 784, 1047};

void setup() {
  Serial.begin(115200);
  Serial.println("Playing");
}

void loop() {
  for (int i = 0; i < 4; i++) {
    tone(BUZZER_PIN, NOTES[i], 200);   // returns at once; the note plays on
    delay(250);
  }
  noTone(BUZZER_PIN);
  Serial.println("Again");
  delay(500);
}
Output

What you should see

The buzzer plays a short four-note phrase and then plays it again. The serial monitor, at 115200 baud, prints Playing at the start and Again after each phrase.

A buzzer that clicks once but never sounds a note is an active buzzer: it ignores tone() and needs only a HIGH level.

The serial monitor prints this, recorded from the circuit above running for two seconds:

Playing
Again
Pitfalls

Common mistakes wiring a buzzer to an ESP32

An active buzzer

An active buzzer has its own oscillator and beeps at one pitch whenever it has power, so tone() cannot change its note. For melodies you need a passive one; the two look the same, but the active kind usually has a sticker on top.

No delay between notes

tone() does not wait for the note to finish. Call it twice in a row and only the second note plays. Wait at least as long as the note, as the sketch does.

Putting the buzzer on an input-only pin

GPIO 34, 35, 36 and 39 on the ESP32 have no output driver at all. pinMode() to OUTPUT on one of them does nothing and the line never moves, which looks exactly like a dead part. GPIO 4 (D4) is a safe choice; so are GPIO 13, 14, 16 to 19, 23, 25 to 27 and 32 to 33.

Build your own buzzer project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.