ESP8266: Controlling a buzzer

The objective of this post is to explain how to control a passive buzzer module using an ESP8266.


For this tutorial, we assume the use of a passive buzzer, integrated in a board that allows us to directly control it using an IO pin from a microcontroller. This kind of device, shown in figure 1, can be bought at eBay for less than 1 euro.

YL-44 passive buzzer module

Figure 1 – Passive buzzer module.

As shown in the figure, there is one pin to provide power to the device, and another to actually control it. This happens to allow the current to be supplied by a source other than the IO pin that is controlling the buzzer.

Important: Don’t directly connect a digital pin of the ESP8266 to a buzzer without using a transistor or any other method to supply the current to the device. The GPIOs of the ESP8266 can only supply 12 mA [1] and the buzzer will draw considerably more, which can damage the microcontroller.

So, we connect the ESP8266 to the buzzer module as shown in figure 2. If you are using a NodeMCU, please take in consideration that the pin numbers indicated on the board don’t correspond to the pins in the ESP8266.

ESP8266 buzzer diagram

Figure 2 – Connection diagram between the ESP8266 and the buzzer module.

Since we use a passive buzzer, we need to generate a square wave to control the sound. The frequency of the square wave produced in the IO pin will be the frequency of the sound. So, controlling the buzzer will be slightly more complicated, since it won’t be on/off like a LED, but we will have more freedom, since we will be able to change the frequency.



The code for this tutorial is very simple, since we will use the tone function, which performs most of the work for us. Please make sure you are using the latest version of the ESP8266 libraries to guarantee that the tone function is available.

This function takes 2 arguments: the first one corresponds to the pin where the square wave that controls the buzzer will be generated and the second corresponds to the frequency to use.

Additionally, we can pass a third argument that specifies how much time we will be outputting the square wave. In our example, we will not use this third parameter because we will control the duration of the sound using a delay function. So, if we don’t pass the third argument, the square wave will be maintained until we call the noTone function on the same pin.

We will assume that our buzzer will play at a 1000 Hz frequency and will buzz for 1 second, then stay off for 1 second, and then repeat, for a bip pattern. Additionally, as illustrated in the hardware section, we consider the use of pin 2 to output the square wave that will control the buzzer.

To make the code easy to change, we first declare 4 global variables and assign them the corresponding values.

int frequency=1000; //Specified in Hz
int buzzPin=2; 
int timeOn=1000; //specified in milliseconds
int timeOff=1000; //specified in millisecods

For this simple example we can jump the setup function and put the following code in our loop:

void loop(){

tone(buzzPin, frequency);


So, we activate the 1000 Hz square wave in pin 2, by calling the tone function, and the buzzer starts playing. We wait for 1 second, then we stop the buzzer with the noTone function. Then we wait 1 more second, and repeat the process.




This entry was posted in Electronics, ESP8266, Microcontrollers and tagged , , , , , . Bookmark the permalink.

5 Responses to ESP8266: Controlling a buzzer

  1. Pranab Salian says:

    How loud does this get?

    Liked by 1 person

  2. Guido Gambardella says:

    Who told you that a piezoelectric buzzer will draw that much current? Have you measured it?


    • antepher says:

      No one told me, I’ve experimented in the past with other types of buzzer and it was drawing more that 12 mA.

      It may depend on the model used, but it is typically a good ideia to put some buffer in the middle between the GPIO and the device you are trying to control, unless you know exactly the maximum amount of current it will draw. Note that 12 mA is not that much.


    • antepher says:

      Just for curiosity I’ve measured again the current drawn by this particular buzzer device (I’ve done it in the past but I didn’t remember the values) and its current draw was around 60 mA, with a frequency of 5000 Hz and a duty cycle of approximately 20 %.

      So, this is much more than the values the ESP8266 can supply in a digital pin without getting damaged.

      Again, there are many devices in the market and other buzzers may draw less current. But the ones I’ve been using always draw considerable amounts of current and are not suitable to be directly driven by microncontroller GPIOs such as this.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s