ESP8266 - Buzzer
This tutorial instructs you how to program ESP8266 to control a 12V active buzzer to produce a loud sound. If you want to control 5V active/passive buzzer, please check out this ESP8266 Piezo Buzzer tutorial
There are two ways to drive the 12V buzzer. A relay is simple, but it clicks every time it switches. A MOSFET module does the same job with no sound, and it can switch very fast. This is good for alarm beeps. We will try both ways in this tutorial.
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of 12V Active Buzzer
The 12V Active Buzzer can produce a loud sound, which is suitable for the alarming system.
Pinout

12V Active Buzzer usually has two pins:
- Negative (-) pin (black): needs to be connected to GND of DC power supply
- Positive (+) pin (red): needs to be connected to 12V of DC power supply
How to Control 12V Active Buzzer
If 12V active buzzer is powered by 12V power supply, it makes sound. To control a 12V active buzzer, we need to use a relay in between ESP8266 and 12V active buzzer. ESP8266 can control the 12V active buzzer via the relay. If you do not know about relay (pinout, how it works, how to program ...), learn about relay in the ESP8266 - Relay tutorial
The MOSFET module is the second choice. The pins, the inside and the code of the module are explained in the ESP8266 - MOSFET Module tutorial.
Relay vs MOSFET Module for a 12V Buzzer
| Relay | MOSFET Module | |
|---|---|---|
| What it can do | turn the buzzer on or off | turn the buzzer on or off |
| Noise when switching | a click every time, mixed with the beep | no sound, you hear only the beep |
| How fast it switches | slow, a few milliseconds | very fast |
| Fast beeps | the relay clicks loudly and wears out fast | no problem |
| Life | the contacts wear out after many switches | nothing moves, so it lasts long |
| Type of power | AC or DC | DC only |
| Buzzer power supply | any voltage the relay is rated for | about 6V or more |
| Isolation from the ESP8266 | yes, between coil and contacts | yes, by the PC817 optocoupler |
| Good for | slow ON/OFF, like ON for a few seconds | alarm patterns with many short beeps |
If the buzzer beeps slowly, the relay is fine. If you want an alarm with many short beeps, the MOSFET module is the better pick.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
If you do not use a breadboard, wire it like this:

This image is created using Fritzing. Click to enlarge image
ESP8266 Code
The below code repeatedly turns the 12V active buzzer ON in two seconds and OFF in five seconds,
Detailed Instructions
To get started with ESP8266 on Arduino IDE, follow these steps:
- Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
- Wire the components as shown in the diagram.
- Connect the ESP8266 board to your computer using a USB cable.
- Open Arduino IDE on your computer.
- Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
- Connect ESP8266 to PC via USB cable
- Open Arduino IDE, select the right board and port
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to ESP8266
- See the 12V active buzzer's state
Code Explanation
Read the line-by-line explanation in comment lines of code!
How to Make Alarm Beeps with MOSFET Module
Now let's replace the relay with a MOSFET module. The buzzer works the same, but there is no click, so it can beep fast like a real alarm.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.
| From | To |
|---|---|
| ESP8266 D5 (GPIO14) | MOSFET module PWM |
| ESP8266 G (GND) | MOSFET module GND |
| 12V adapter + | MOSFET module + |
| Buzzer red wire (+) | MOSFET module + (same terminal as the adapter +) |
| Buzzer black wire (−) | MOSFET module LOAD |
| 12V adapter − | MOSFET module − |
A 3.3V ESP8266 pin is enough, because it only pushes a few milliamps through the LED of the PC817 optocoupler.
Most 12V active buzzers have a small circuit inside and do not need a flyback diode. If your buzzer is a magnetic type (it has a coil inside), add a 1N4007 or 1N5819 diode across it. The side with the stripe (cathode) goes to the buzzer +, the other side goes to the buzzer −.
ESP8266 Code
What the buzzer does: it beeps 10 times fast, then makes one long beep, then stays quiet for 3 seconds. Then it starts again.
An active buzzer makes its own sound. Only turn it fully ON or fully OFF with digitalWrite(). Do not use analogWrite() (PWM) to change the volume, because the sound will be weak or strange.
Detailed Instructions
- Do the same setup as in the Quick Steps steps of the relay part above.
- Wire the buzzer and the MOSFET module as shown in the diagram. Keep the 12V adapter unplugged.
- Upload the code above to the ESP8266.
- Plug in the 12V adapter.
- Open the Serial Monitor at 9600 baud.
- Listen to the buzzer: you hear the fast beeps with no relay click.
※ NOTE THAT:
The MOSFET module works with DC only, and the buzzer power supply must be about 6V or more. For a 5V buzzer, see the ESP8266 Piezo Buzzer tutorial. For an AC buzzer or siren, use the relay.