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

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Relay
1×12V Active Buzzer
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of 12V Active Buzzer

The 12V Active Buzzer can produce a loud sound, which is suitable for the alarming system.

Pinout

ESP8266 NodeMCU 12V Active Buzzer 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 make 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

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and 12V Active Buzzer

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.

ESP8266 Code

The below code repeatedly turns the 12V active buzzer ON in two seconds and OFF in five seconds,

/* * 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); }

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!

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!