Contents
- 1 Types of buzzers, or buzzers
- 2 Differences between passive and active buzzer
- 3 Differences between piezo and magnetic buzzers
- 4 Difference between buzzer and buzzer module
- 5 Why do we need a transistor?
- 6 Diagram of a buzzer module
- 7 A drawback of low level activated buzzer modules
- 8 Build your own buzzer module
Sometimes we need our ESPEasy-based project to generate sound alerts, such as warnings, alarms, confirmation of certain actions, or other reasons. In this article, we will see, step by step, how we can add a buzzer to our project with ESPEasy.
Buzzers give people a lot of problems when it comes to getting them to work and this is mainly because there are several types of buzzers and each one has to be used in a different way. The best way to make them work is to know how to use them from the beginning, the type of buzzer you have in hand.
I'm not going to limit myself to just using buzzers with the ESP8266, NodeMCU and Wemos D1 Mini, but I'm also going to cover how to do it with Arduino and ESP32, although it's very similar.
In this case, the examples are going to be done on the CO2 meter I published a few days ago, but can be applied to any other type of project.
Types of buzzers, or buzzers
The first thing we need to know is that not all buzzers are the same.
We can differentiate them mainly by two characteristics:
- Does the buzzer have an integrated oscillator (it is a active or passive buzzer)?
- How are the tones generated (buzzer piezoelectric or electromagnetic)?
As each of these characteristics is independent, this means that we have in total four different combinations:
Piezo | Magnetic | |
---|---|---|
Liabilities | The tone is generated by a piezo buzzer and there is no built-in oscillator. | It has no built-in oscillator and the tone is generated by a magnetic buzzer. |
Active | The tone is generated by a piezoelectric diaphragm and the buzzer has a built-in oscillator. | It has a built-in oscillator and magnetic buzzer. |
In the course of this article we will go into the different types of buzzers in more detail.
Differences between passive and active buzzer
The fundamental difference between an active and a passive buzzer is that the active buzzer has an internal oscillator, whereas the passive buzzer does not. The other differences are derived from this and are summarised in the following table.
Passive Buzzer | Active Buzzer | |
---|---|---|
Oscillator | No internal oscillator | Integrated oscillator |
Feeding | Alternating current | Direct current or square wave |
Terminal length | Same length of terminals | The positive has a longer terminal |
Relative price | Cheap | Caro |
Identification | No sticker on top | With white sticker on top |
Notice, as a good trick to tell them apartThe active buzzers usually come with a label or sticker on the top and the bottom of an active buzzer is usually sealed with resin.
Differences between piezo and magnetic buzzers
Buzzers can be classified depending on technology they use to generate the sound.
The most commonly used buzzers in microcontroller projects are the piezo buzzer, or piezoelectric buzzer, and the magnetic buzzer, or electromagnetic buzzer. There are other technologies, but they are not as common in microcontroller projects.
In the following table I show you the main ones differences between a piezo and a magnetic buzzer:
Piezo buzzer | Magnetic buzzer | |
---|---|---|
Operating voltage | 12V~220V | 1.5V~12V |
Current consumption | < 20mA | > 20mA |
Sound pressure level (SPL) | 85~120 dB | 70~95dB |
Resonant frequency | 2~6 kHz | 1~3 kHz |
Below, I explain, in a little more detail, the most important things about each of them.
How a piezo buzzer works
The piezoelectric buzzer is a type of buzzer based on the properties of piezoelectric crystals (polyester or ceramic), which deform when a stress is applied between their faces and which, acting as an electro-acoustic transducer, is used for sound reproduction.
If a flared cone is attached to one of its faces, it will undergo displacements capable of producing an oscillating pressure within an audible frequency range, i.e. low frequency.
Piezoelectric buzzers are simpleand capable of emitting sounds using very little electrical energy.
Your answer is optimal for the reproduction of high frequenciesare incapable of reproducing low-frequency ranges.
Piezo buzzers are designed for use with high voltages (typically between 5V and 220V) and with current consumptions of less than 20mA. Because of the higher voltages, piezo buzzers typically have a higher sound pressure level (SPL) (between 85db and 120db) than magnetic buzzers.
A piezo buzzer responds to voltage at any frequency, but much more strongly the closer you get to its resonance frequency between 2 kHz and 6 kHz.
How a magnetic buzzer works
Understanding how a magnetic buzzer works is much easier because we are more familiar with them. This is the way the loudspeakers work..
When a voltage is applied to a coil generates a magnetic field which causes a movement by the attraction or repulsion effect it exerts on one or more magnetic fields. magnets of which the buzzer is equipped.
Difference between buzzer and buzzer module
Differentiating between a buzzer and a buzzer module is very important when it comes to their connectionbut it is also very easy to distinguish.
A buzzer is just the buzzer itself and the buzzer module additionally incorporates the transistor and the resistor (and sometimes a protection diode).
The buzzer is a single component whose function is to generate sounds. A buzzer (the individual component), in most cases, needs external components to function. | |
A buzzer module is a board on which we find the buzzer together with the additional external components needed to connect it to a microcontroller. |
Buzzer
If you have a loose buzzer, as a rule, you will not be able to connect it directly to the microcontroller ESP8266, because, although its power consumption is not too high, it is higher than what the microcontroller can provide.
Buzzer module
If you have a buzzer module, the connection between the module and the Arduino, EPS8266 or ESP32 microcontroller board is very simple. Just connect the positive and negative power supply and the control signal to the I/O pin of the microcontroller and you are ready to go.
Passive buzzer module | |
Active buzzer module |
Why do we need a transistor?
When creating a circuit to control a buzzer, either active or passive, with a microcontroller such as Arduino, ESP8266 or ESP32, we must take into account the limits of the current that can be supplied by its pins.
Buzzers, as a rule, consume more current than one pin of a microcontroller is capable of supplying.If the buzzer draws more current than the pin is capable of supplying, we will have to provide a solution.
The peak currents that can be provided by the pins of the microcontrollers we are talking about, are the following:
Microcontroller | Plates | Max current. |
---|---|---|
ATmega328P | Arduino Nano, Arduino Uno, Arduino Pro Mini | 40 mA |
ATmega2560 | Arduino Mega | 20 mA |
ESP8266 | ESP-01, NodeMCU, WeMos D1 Mini | 12 mA |
ESP32 | DevKitC, Adafruit HUZZAH32, Sparkfun ESP32 Thing Plus | 20 mA |
The buzzers piezoelectrichave a current consumption of 20mA and can therefore be powered directly by the Arduino microcontroller and the ESP32 (not the ESP8266, if you look at the table above). But if we want to use magnet buzzers with a current consumption higher than 20 mA, we need to have a power supply different from the I/O pin of the microcontroller.
The solution is to use a transistor and separate the control signal and the power supply line. In my case, I use the 2N2222 NPN as power transistor for supplying the buzzer.
- NPN transistor: because we want to turn on the buzzer, active or passive, by setting the digital pin of the microcontroller HIGH.
- Low-side switch: the transistor is on the low (negative) side of the circuit and the buzzer is connected to the positive (5V or 3.3V) side. If this were not the case, we would not be able to disconnect a load that has a higher voltage than the control voltage.
Many (most) commercial modules use a PNP instead of NPN transistor which has the effect that are triggered by a low signal (when the control signal is removed they sound). You will need to take this into account when writing your rules and adapt them, if necessary.
When you use a buzzer module with low signal activationis particularly important that, after the sound is emitted, the pin that controls the buzzer stay at a high level (pin activated or at 1), otherwise the buzzer would remain activated without emitting sound and would burn out (remember that the buzzers with PNP type transistor, almost all of them, are activated by low level).
The best thing to do, if you have a multimeter, is to make sure, by measuring voltage directly on the buzzer element (not at the module input), that there is no voltage applied.
Diagram of a buzzer module
In the following image you can see the common scheme of commercially available buzzer modules (such as those illustrated in this article).
Buzzer connection to an Arduino Uno
The following images show the wiring between the buzzer and different microcontrollers (in preparation).
Buzzer connection to an ESP8266
In the following images you can see the wiring between the buzzer and the NodeMCU or WeMos D1 Mini. On other ESP8266 based boards the wiring will be almost identical, adapting to the appropriate pins.
WARNING UNTIL I CAN CHANGE THE DIAGRAM: DO NOT CONNECT THE BUZZER TO 5V. CONNECT IT TO 3.3V TO AVOID ANNOYING NOISES (AS EXPLAINED BELOW).)
Wiring between the Buzzer and ESP32 boards
The ESP32 follows the names of the ESP8266. Simply connect it as in the examples above to the pins with the same names.
A drawback of low level activated buzzer modules
Unfortunately (for what I will discuss below), most commercially available buzzer modules are low level activated (you will have a harder time finding one that is high level activated) and this has the following drawback:
A buzzer of the type we are talking about here, emits a louder sound when connected to 5V than when connected to 3.3V. The problem is that with low level activated modules we cannot (or should not) supply them at 5V.If we are going to use them with an ESP8266 or ESP32 (with the consequent loss of sound power) because of the following:
Suppose we connect the buzzer module to 5V.
The problem is that, for the buzzer, the "high level" (silence) would be those 5 volts, but, of course, the ESP8266 or ESP32 do not output 5 volts on their pins, but 3.3 volts.
This means that when we pull a high level on the pin to silence the buzzer, the buzzer does not receive 5V, which is what it expects, but 3.3V. An intermediate voltage that for him it is neither high nor low.
Consequence: the buzzer will probably emit clicks, clacks, and strange sounds because those 3.3V are leading it to an undefined place, where he (in his digital world of 0 and 5 volts) doesn't know how to live.
Solution: Power it at 3.3 volts, albeit at a lower sound level. Most of the time you will want the buzzer for warning purposes, not as an alarm, and the sound level at 3.3 volts is more than enough.
Build your own buzzer module
I, in particular, always prefer to build my own buzzer modules. That way I have it quickly and build them with an NPN transistor, so that they are activated by high level. Much more logical and easier to use.
I connected it to 3.3v and it worked as expected! Excellent solution!
Thank you for sharing.