ESP8266 NodeMCU – Make some noise with buzzers


This tutorial will show you how to make noise with both an active and a passive buzzer. If you need an introduction on how to start with NodeMCU board, you can check the tutorial “ESP8266 NodeMCU – Blinking a LED”.
There are two main types of buzzers for hobby use. Although they are both buzzers, they work differently. Here you will learn why and how to control them.

Parts Required

To build the global setup you must have the following parts:

Note: You do not need to have a NodeMCU board nor an ESP8266 to follow this tutorial. I am using this board because it is very famous and also because, i will post future tutorials using wifi communications. In order to use other boards, you just have to change the pin number.

Passive Buzzer

A passive buzzer (aka piezoelectric buzzer) can make different tones, but the device that controls it has to provide an output with an oscillating electronic signal at a desired frequency. The supplied frequency will determine the tone. Supplying just a fixed voltage will generate no sound, except perhaps a slight “tick” at the point when the power source is connected or disconnected from the buzzer. When mounting in the breadboard, the longer leg is the positive one.
The polarity must be respected for sound to be generated. To distinguish between Active and Passive, always check the product reference or waste your time trying.

Active Buzzer

An active buzzer has additional circuit on it, which makes it easier to use but limits the buzzer to only one type of sound. An active buzzer only requires DC power source for a beep to be generated. When mounting in the breadboard, the longer leg is the positive one. The polarity must be respected for sound to be generated. To distinguish between Active and Passive, always check the product reference, or waste your time trying.


Connect the passive to GPIO pin 2 (D4) and the active to GPIO pin 14 (D5) as shown above. Everything will be powered from your PC using micro-usb to usb cable. The buzzers can use an external source of power, but for that we would need more parts. In this configuration, they will be powered directly from the GPIO pins. All the current for the buzzers will be drawn from the board, and since each GPIO delivers only no more than a few mA, the sound can be low. A resistor can be placed in series with the positive leg of the buzzer to protect the board from loading to much current. Let’s try it.

Note: Remember that these buzzers are rated to 5V, and the GPIO only delivers 3.3V

Start Coding

This code will turn on first the active buzzer, then the passive and then loop. The active buzzer is connected to a pin set as digital output. The passive is also connected to a pin set as digital output and PWM compatible. To generate sound in the passive buzzer, we will use the arduino “tone()” function that generates a PWM output. For more information about this function check arduino tone(). Copy the code bellow to your Arduino IDE project and save it. Connect your ESP8266 NodeMCU to your PC using micro-usb to usb cable and upload the code. Have fun!

Note: Unplug the buzzers before download the sketch, or place a capacitor in series with the buzzer to prevent a short-circuit while the ESP8266 is resetting. This happens because the buzzer internal resistance is low.

8 thoughts on “ESP8266 NodeMCU – Make some noise with buzzers

  1. Thank you for that informative and clearly written article. I wanted to know about using active and passive buzzers with an ESP8266, and that is exactly what you explained to me.

  2. This is the perfect site for anybody who wishes to find out about this topic.

    You understand a whole lot its almost hard to argue with you (not that I personally would want to…HaHa).
    You definitely put a new spin on a topic which has been written about for
    ages. Wonderful stuff, just great!

  3. I’ve been browsing online more than 4 hours today, yet I never
    found any interesting article like yours. It is pretty worth enough
    for me. In my opinion, if all web owners and bloggers made good content
    as you did, the net will be much more useful than ever before.

  4. just in case anyone wants the correct fa, sol, la si…

    tone(Passive_buzzer, 698) ; //FA note …
    delay (1000);
    tone(Passive_buzzer, 783) ; //SOL note …
    delay (1000);
    tone(Passive_buzzer, 880) ; //LA note …
    delay (1000);
    tone(Passive_buzzer, 987) ; // SI note ..

    …says a lot for the replies above 8/

Leave a Reply

Your email address will not be published. Required fields are marked *