Skip to content

Buzzer / Piezo · ESP32 guide

Controlling Active Buzzer Module with ESP32

A self-oscillating sounder that produces a fixed tone when correctly powered — learn how to identify, select and use active buzzer modules safely.

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

What is the Active Buzzer?

An active buzzer is the simplest way to make a project beep. Apply its rated voltage and it sounds; remove the voltage and it stops. There is no frequency to generate and no library to install, because the part contains its own oscillator circuit that drives the sound element at a fixed tone, usually around 2.3 kHz. That makes it ideal for alarms, button feedback, timers and "something went wrong" warnings.

The word active refers to that built-in drive circuit. A passive buzzer looks almost identical but has no oscillator: it is just a sound element, and your microcontroller has to switch it on and off thousands of times a second (for example with tone()) to make a sound. Passive buzzers can play melodies; active buzzers play one note, loudly and with almost no code.

Specifications

Type
Magnetic or piezo sounder with a built-in oscillator
Rated voltage
5 V (3 V and 12 V versions also exist)
Operating voltage
About 4–8 V for typical 5 V parts (some are rated 3–7 V)
Current
Up to 30 mA at rated voltage
Sound output
85 dB or more at 10 cm
Tone
Fixed, about 2.3 kHz ± 300 Hz (varies by part)
Size
12 mm diameter, about 9 mm tall
Polarity
Yes: longer lead or + marking is positive
Operating temperature
About −20 to +70 °C

Typical figures for 12 mm 5 V active buzzers from manufacturer datasheets (for example the LTE12-05 and PUI Audio AI-1223-TWT-5V). Check the markings and listing for your part.

Pinout

PinNameDescription
+PositiveLonger lead, or the leg next to the + mark on top. Connect to the supply or the driver.
−NegativeShorter lead. Connect to ground or to the transistor's collector.

Wiring & code

Setting up Active Buzzer with ESP32

The ESP32 runs at 3.3 V and its pins are meant for logic signals, not loads. A typical 5 V active buzzer is quiet or silent at 3.3 V and can draw more current than a pin should supply. In this guide you drive the buzzer from the board's 5 V pin through a small NPN transistor, so the ESP32 only provides a tiny control current. You will also choose a pin that stays quiet during boot, and use a non-blocking beeper that plays patterns while your Wi-Fi, sensors and display code keep running.

What you need

  • ESP32 DevKit (38- or 30-pin) and USB cable

    Quantity
    1
    Note
    Any classic ESP32 board
  • Active buzzer, 5 V

    Quantity
    1
    Note
    Bare buzzer or a 3-pin module
  • NPN transistor (2N2222, BC547, 2N3904 or S8050)

    Quantity
    1
    Note
    For a bare buzzer or a 5 V-only module
  • 1 kΩ resistor

    Quantity
    1
    Note
    Base resistor
  • 1N4148 diode

    Quantity
    1
    Note
    Optional, recommended for magnetic buzzers
  • Breadboard and jumper wires

    Quantity
    -

Wiring: buzzer and NPN driver

Circuit pointConnect toNote
Buzzer +ESP32 5V (VIN)USB 5 V; the buzzer gets its full rated voltage
Buzzer −Transistor collector
Transistor emitterESP32 GND
Transistor baseGPIO25 through 1 kΩAbout 2.6 mA from the pin
1N4148 diodeAcross the buzzerStripe (cathode) to 5V; optional

Warning

Never feed 5 V into an ESP32 GPIO. The buzzer sits on the 5 V side of the transistor; the ESP32 pin only connects to the base resistor. If you use a three-pin module powered from 5 V, check that its signal input is not pulled up to 5 V; if unsure, power the module from 3V3 or use the transistor circuit above.

Why GPIO25?

Several ESP32 pins are strapping pins (GPIO0, 2, 5, 12 and 15) that the chip reads at reset to decide how to boot, and some pins briefly toggle or output signals during start-up. A buzzer on one of those can chirp every time the board resets, or in the case of GPIO12, stop it booting at all. GPIO25, 26, 27, 32 and 33 are calm at boot and make good choices for a buzzer.

  1. 1Install the ESP32 board package: in Arduino IDE 2.x open Boards Manager, search for esp32 by Espressif Systems and install it.
  2. 2Choose Tools → Board → ESP32 Dev Module and select the board's port.
  3. 3Paste the sketch below and click Upload. If uploading stalls at "Connecting...", hold the BOOT button until it starts.
  4. 4Open the Serial Monitor at 115200 baud and press the board's EN (reset) button.
  5. 5You should hear two chirps. Type s, l or a and press Enter.
sketch.ino
// Active buzzer on an ESP32 with an NPN low-side driver - SolderHub
// A non-blocking beeper: patterns play while loop() keeps running.

const int  BUZZER_PIN = 25;      // boot-safe output pin
const bool ACTIVE_LOW = false;   // true for modules that sound when the pin is LOW

void setBuzzer(bool on) {
  digitalWrite(BUZZER_PIN, (on != ACTIVE_LOW) ? HIGH : LOW);
}

// --- Non-blocking beeper state ---
uint16_t beepOnMs = 0, beepOffMs = 0;
uint8_t  beepsLeft = 0;
bool     beepIsOn  = false;
uint32_t lastChange = 0;

void beepStart(uint16_t onMs, uint16_t offMs, uint8_t count) {
  beepOnMs = onMs;
  beepOffMs = offMs;
  beepsLeft = count;
  beepIsOn = true;
  lastChange = millis();
  setBuzzer(true);
}

void beepUpdate() {
  if (beepsLeft == 0) return;
  uint32_t now = millis();

  if (beepIsOn && now - lastChange >= beepOnMs) {
    setBuzzer(false);
    beepIsOn = false;
    lastChange = now;
    beepsLeft--;                         // one beep finished
  } else if (!beepIsOn && beepsLeft > 0 && now - lastChange >= beepOffMs) {
    setBuzzer(true);
    beepIsOn = true;
    lastChange = now;
  }
}

void setup() {
  pinMode(BUZZER_PIN, OUTPUT);
  setBuzzer(false);

  Serial.begin(115200);
  beepStart(100, 100, 2);                // two "ready" chirps
  Serial.println("Buzzer ready. Type s = short, l = long, a = alarm");
}

void loop() {
  beepUpdate();                          // call this as often as possible

  if (Serial.available()) {
    char c = Serial.read();
    if (c == 's') { beepStart(80, 80, 1);    Serial.println("short beep"); }
    if (c == 'l') { beepStart(600, 100, 1);  Serial.println("long beep"); }
    if (c == 'a') { beepStart(150, 150, 10); Serial.println("alarm"); }
  }

  // Your other code runs here: Wi-Fi, sensors, display updates...
}

How the code works

  • setBuzzer(bool on) converts "on" or "off" into the right pin level. With the NPN driver, HIGH switches the transistor on. ACTIVE_LOW flips this for PNP-based modules.
  • beepStart(onMs, offMs, count) only starts a pattern: it stores the timings, turns the buzzer on and notes the time with millis().
  • beepUpdate() runs on every pass through loop(). It checks how long the buzzer has been in its current state and switches it when the time is up, counting down the remaining beeps. Nothing ever waits, so a 10-beep alarm does not stop the ESP32 from serving a web page or reading a sensor at the same time.
  • The Serial commands start a new pattern at any time; a new command simply replaces the one that is playing.

Expected output

Two quick chirps after reset, and in the Serial Monitor:

Output
Buzzer ready. Type s = short, l = long, a = alarm

Each letter plays its pattern and prints a confirmation line. If the buzzer is silent, measure the 5V pin (it should read about 5 V on USB power) and check the transistor's pin order.

Troubleshooting

SymptomLikely causeFix
Silent, but Serial worksTransistor pins wrong, buzzer reversed, or a passive buzzerCheck E, B, C for your transistor; swap the buzzer leads; test the buzzer on 5 V
Very quietBuzzer powered from 3V3Move buzzer + to the 5V (VIN) pin
Chirps on every resetBuzzer on a strapping or boot-time pinUse GPIO25, 26, 27, 32 or 33
Board will not boot with the buzzer connectedBuzzer circuit on GPIO12 or GPIO0Move it to GPIO25
Beeps nonstop, stops when it should beepActive-LOW moduleSet ACTIVE_LOW = true
Patterns cut short or stutterdelay() added elsewhere in loop()Remove long delays; keep beepUpdate() running often

Projects to build next

  • Wi-Fi doorbell: a button press beeps locally and sends a phone notification.
  • Freezer alarm: a DS18B20 sensor that sounds the alarm pattern if the temperature rises.
  • Deep-sleep motion alert: wake on a PIR sensor, beep, report over Wi-Fi, sleep again.
  • Web-controlled buzzer: a small web page with buttons for each pattern, using the non-blocking beeper so the server stays responsive.

Keep learning

Related guides & projects