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