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 the 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 the 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 an 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 a 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.

Circuit

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 below. A resistor can be placed in series with the positive leg of the buzzer to protect the board from loading too 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 a 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 below 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 downloading 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.

const unsigned char Passive_buzzer = 2;
const unsigned char Active_buzzer = 14;
const unsigned char built_in_led = 16;

void setup () {
pinMode (Passive_buzzer,OUTPUT) ;
pinMode (Active_buzzer,OUTPUT) ;
pinMode (built_in_led,OUTPUT) ;
}

void loop () {
digitalWrite(built_in_led,LOW) ; //Turn on the built in led
digitalWrite(Active_buzzer,HIGH) ; //Turn on active buzzer
delay (1000);
 
digitalWrite(built_in_led,HIGH) ; //Turn off the built in led
digitalWrite(Active_buzzer,LOW) ; //Turn off active buzzer
delay (1000); 

tone(Passive_buzzer, 523) ; //DO note 523 Hz
delay (1000); 
tone(Passive_buzzer, 587) ; //RE note ...
delay (1000); 
tone(Passive_buzzer, 659) ; //MI note ...
delay (1000); 
tone(Passive_buzzer, 783) ; //FA note ...
delay (1000); 
tone(Passive_buzzer, 880) ; //SOL note ...
delay (1000); 
tone(Passive_buzzer, 987) ; //LA note ...
delay (1000); 
tone(Passive_buzzer, 1046) ; // SI note ...
delay (1000); 
noTone(Passive_buzzer) ; //Turn off the pin attached to the tone()
}

9 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/

  5. Thank you for every other informative website. Where else may I get that type of info written in such a perfect approach? I’ve a mission that I’m simply now working on, and I’ve been on the look out for such info.

Leave a Reply

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