A 12V active buzzer is loud enough for an actual alarm, but it needs more voltage than the ESP8266 can supply directly, so you need something in between to switch the power. This tutorial covers two ways to do that: a relay, which is simple but clicks every time it switches, and a MOSFET module, which switches silently and fast enough for quick alarm beeps. Good starting point if you're building anything that needs to make noise on demand.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP8266 - Buzzer
What you need
- ESP8266 NodeMCU ESP-12F
- ESP8266 D1 Mini NodeMCU ESP-12F
- ESP8266 NodeMCU ESP-12E (Uno-form)
- Micro USB Cable
- ESP8266 NodeMCU ESP-12F (SMD-form)
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- Relay
- 12V Active Buzzer
- DC Power Jack
- Jumper Wires
- Breadboard
- Screw Terminal Expansion Board for ESP8266
- DIYables Sensor Kit (18 sensors/displays)
Wiring
The buzzer has two wires: black negative goes to GND of the 12V supply, red positive goes to 12V. You don't connect the buzzer directly to the ESP8266 since it needs 12V and the ESP8266 only outputs 3.3V logic. Instead, the ESP8266 drives a relay (or MOSFET module) on pin D7, and that component switches the 12V line to the buzzer on and off. For the MOSFET version, a 3.3V ESP8266 pin is enough to drive it, since it only needs to push a few milliamps through the LED of the PC817 optocoupler inside the module. If your buzzer is a magnetic type with a coil inside, add a flyback diode (1N4007 or 1N5819) across it, stripe side to buzzer plus.
How it works
An active buzzer has its own sound-generating circuit built in, so all it needs from you is 12V power, fully on or fully off. A relay is basically a switch controlled by the ESP8266 that physically connects and disconnects the 12V line, which is why it makes a click each time. A MOSFET module does the same switching electronically with no moving parts, so it's silent and can flip on and off much faster, which matters if you want a rapid alarm pattern instead of a single slow beep.
Code
The relay version is as simple as it gets: set D7 as OUTPUT, then alternate digitalWrite(RELAY_PIN, HIGH) and LOW with delays, turning the buzzer on for 2 seconds and off for 5. The MOSFET version builds a more alarm-like pattern: 10 fast beeps, then one long beep, then 3 seconds of silence, repeating. In both cases you only ever use digitalWrite() to fully turn the buzzer on or off. Don't use analogWrite() (PWM) to try to control volume, since the buzzer generates its own tone and PWM will just make it sound weak or strange.
esp8266-buzzer.ino
/*
* This ESP8266 NodeMCU code was developed by newbiely.com
*
* This ESP8266 NodeMCU code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/esp8266/esp8266-buzzer
*/
#define RELAY_PIN D7 // The ESP8266 pin that controls the buzzer via relay
// The setup function runs once on reset or power-up
void setup() {
// initialize digital pin D7 as an output.
pinMode(RELAY_PIN, OUTPUT);
}
// The loop function repeats indefinitely
void loop() {
digitalWrite(RELAY_PIN, HIGH); // turn on buzzer 2 seconds
delay(2000);
digitalWrite(RELAY_PIN, LOW); // turn off buzzer 5 seconds
delay(5000);
}
Things that go wrong
If nothing happens, double check the 12V adapter is actually plugged in and the buzzer's polarity is correct, red to 12V and black to GND. If you're using the relay and expect fast alarm-style beeping, you'll hear the relay struggling to click that quickly, or it may not keep up at all, that's the cue to switch to the MOSFET module instead. If a magnetic-coil buzzer is acting unreliably or affecting other components, check that the flyback diode is wired with the correct orientation across it.
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/esp8266/esp8266-buzzer

Top comments (0)