ESP8266: Controlling a buzzer

Introduction

The objective of this post is to explain how to control a passive buzzer module using an ESP8266. We will program the ESP8266 using the Arduino core.

Hardware

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 previous 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 board, please take in consideration that the pin numbers indicated on the board don’t correspond to the pins in the ESP8266.

ESP8266 buzzer connection 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 it will emit. 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.

Software

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 Arduino core 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 it will buzz for 1 second, then stay off for 1 second, and then repeat, for a bip pattern. Additionally, as illustrated in the diagram on 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);
   delay(timeOn);
   noTone(buzzPin);
   delay(timeOff);
}

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.

Related Posts

References

[1] https://cdn-shop.adafruit.com/datasheets/ESP8266_Specifications_English.pdf

14 thoughts on “ESP8266: Controlling a buzzer”

  1. Guido Gambardella

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

    1. 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.

    2. 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.

  2. Guido Gambardella

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

    1. 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.

    2. 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.

  3. hi, im ttrying to make a device using esp8622. The aim is, to press a button and activate the buzzer. so i will need 2 things, button will send the signal and otheer end will receive the singal and beep/buzz/soind. but im stuck,and now i dont know how to make it. so do you have any idea on how to make it work?

  4. hi, im ttrying to make a device using esp8622. The aim is, to press a button and activate the buzzer. so i will need 2 things, button will send the signal and otheer end will receive the singal and beep/buzz/soind. but im stuck,and now i dont know how to make it. so do you have any idea on how to make it work?

  5. Hi, you talk about “the latest esp library” but googling for it (or esp8266 + tone) I can only find a lot of people saying that they are incompatible!

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading