Contents
Sometimes we need our ESPEasy-based project to generate sound alerts, for 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.
In this case, let's do the example with the CO2 meter I published a few days ago, but the same can be done with any other type of project..
Because ESPEasy allows you to connect to our project many different accessories without having to program or complicate our lives. with firmware modifications or adaptations, adding a buzzer is trivial.
Connection of the buzzer module
In this case, we are going to use a buzzer module, one of those that come on a small board with its transistor and resistor.
Connecting the module is extremely easy, we only need to make three connections:
- Positive wire, from the Vin pin of the NodeMCU to the pin marked as positive on the buzzer module (normally marked VCC).
- Negative wire, from the GND pin of the NodeMCU to the pin marked as positive on the buzzer module.
- Data cable, from a NodeMCU Input/Output pin to the buzzer signal input pin (usually marked I/O).
In this example we are going to use the D6 output of the NodeMCU, although we could use another one.
Use and control of the buzzer with ESPEasy
The first thing to know is that to use a buzzer with ESPEasy we do not need to create a device. The control is done directly through the commands that ESPEasy has available:
Protocol | Syntax | Additional information |
---|---|---|
HTTP | http:///control?cmd=
| Sends commands via HTTP protocol. |
MQTT | /cmd with payload:
| Sends commands via MQTT protocol. |
Serial (TTL) |
| Send commands using the serial port (RX/TX). Simply type the
|
UDP | SendTo,,
| Sends commands from one ESP Easy unit to another. Configures the ESP Easy controller's peer-to-peer UDP server first. |
Rules |
| Internally within ESP Easy. Simply enter the in an event block or conditional block. |
We have two possibilitieswhen sending commands to ESPEasy to sound our buzzer. The command tone and the command rtttl.
Playing a sound
http:///control?cmd=tone,14,1300,200
Plays a 1300 Hz tone for 200 ms on the GPIO-14 pin.
Command (GPIO/Value) | Additional information |
---|---|
tone,,,. GPIO: 12 ... 16 Tone: 20 ... 13000 Hz Duration: 100 ... 15000 msec | Try to use GPIO 12...16 as these are usually not used by the ESO internal functions. The recommended tone range is 20 Hz ... 13 kHz. It is possible to generate tones up to 40 kHz but they are inaudible to humans. Frequencies above 30 kHz are not stable and may block the ESP. The duration is given in milliseconds. Note: Tones with a duration of less than 50 msec temporarily block the ESP. Longer duration tones are created by timer, which may cause changes in duration. |
rtttl,,. GPIO: 12 ... 16 Value: d=,o=,b=,,o=,b=, | Try to use GPIO 12...16 as these are not normally used by the ESO's internal functions. N.B. the tones temporarily block the ESP while sounding. Value can be defined as . For example: rtttl,14,test:d=8,o=5,b=180,c6,b,c6,p,g,g- More RTTTL Ringtone Downloads |
Play a melody
http:///control?cmd=rtttl,14:d=4,o=5,b=112:8a,8a,a,8a,8a,a,8a,8c6,8f.,16g,2a,8a-,8a-,8a-.,16a-,8a-,8a,8a.,16a,8a,8g,8g,8a,g,c6
This plays a tune via RTTTL on the GPIO-14 pin.
RTTTL is a musical notation designed to create ringtones for old mobile phones. RTTTL libraries exist, so it will not be difficult to find your favourite music or, if you have musical notions, to create your own.
RTTTL (Ringtone Transfer Language [Text]) was developed by Nokia to play more "easy-to-listen-to" melodies as ringtones for mobile phones. The melodies for all features are created using three different commands:
d - duration
o - octave
b - tempo (rhythm)
After that, the actual pitch is created using standard notes (a, b, c, d, e, f and g), before the note the duration is set, after the note the octave is set:
.
To compose a melody, notes are separated by a comma.
Examples of using the buzzer with ESPEasy
Here are some simple examples of how to use the buzzer with the different protocols.
The command we have to send is similar in all cases:
To play a note: tone,14,1300,200
Para reproducir una melodía: rtttl,14:d=4,o=5,b=112:8a,8a,a,8a,8a,a,8a,8c6,8f.,16g,2a,8a-,8a-,8a-.,16a-,8a-,8a,8a.,16a,8a,8g,8g,8a,g,c6
Use of the buzzer via HTTP
http:///control?cmd=tone,14,1300,200
http:///control?cmd=rtttl,14:d=4,o=5,b=112:8a,8a,a,8a,8a,a,8a,8c6,8f.,16g,2a,8a-,8a-,8a-.,16a-,8a-,8a,8a.,16a,8a,8g,8g,8a,g,c6
Use of the buzzer via MQTT
Use of the buzzer by rules (ESPEasy Rules)
To play a melody at ESPEasy start-up:
On System#Boot do rtttl,14:d=10,o=6,b=180,c,e,g endon
Integration with CO2 meter
We are going to use what we have learned to create a system of warnings, or alarms, for our CO2 meter.
By means of rules, the meter will play a warning and/or alarm sound when certain CO2 concentrations are reached.
To do so, we will make use of the ESPEasy rules.
Activating the ESPEasy rules engine
The first thing to do is to activate the ESPEasy rules engineThe default, when we install it, will be disabled.
To do this, go to the tab "Tools(Tools) and click on the button " ".Advanced"(Advanced):
The ESPEasy advanced configuration options window will open.
There we have to activate the options "Rules"(Rules) and "Old Engine"(Old Engine):
Writing our rules
Finally, we have to write our rules, in the "Rules" tab. This tab will not appear until we have enabled the rules engine, as we have seen in the previous point:
In the rule editor that appears in ESPEasy, we will write the following rules (I recommend copying and pasting, to avoid errors and leave comments - lines starting with // - which ESPEasy will ignore).
You will need to replace CO2#PPM with your chosen device name and value (in the "Devices" tab):
// We set a minimum time for the alarm to repeat, so that the alarm is repeated,
// is not continuously beeping if the CO2 concentration value is exceeded.
// Create a timer to run every second
On System#Boot do Let,1,60 // Minimum time, in seconds, to repeat the alarm Let,2,1500 // CO2 concentration to trigger alarm loopTimerSet,8,1 // Set a timer to run every second EndOn
// This timer runs every second
// Decrements the time remaining to activate the alarm by one second.
// It will set a timer again
On Rules#Timer=8 Do Let,1,[var#1]-1 EndOn
// Each time the PPM value of the CO2 device changes:
// We check that the CO2 value is higher than the concentration at
// from which we want the alarm to go off and the time has passed
// minimum time we want before the alarm sounds.
// If the PPM concentration is higher and the minimum time has elapsed:
// We sound the buzzer connected to the GPIO-12 pin at 1000 Hz.
// for 60 milliseconds.
// We set the minimum time we want the alarm to be repeated.
on CO2#PPM do if [CO2#PPM] > [var#2] and [var#1] < 1 tone,12,1000,60 Let,1,60 // Time to repeat alarm again endif endon
Buy the buzzer
I recommend you buy the passive buzzer moduleThis allows you to play the tones you want (beeps or melodies). For example, this one:
- Passive buzzer, needs input oscillation frequency at I/O ports, and the buzzer will sound
- Module with transistor S8550
- Operating voltage: 3.3 V ~ 5 V
- Fixed bolt holes for easy installation
- Module interface description (3 wires)
If you prefer to buy it on AliExpress, cheaper, even if it takes a little longer to reach you, you can buy the buzzer at this link.
Be careful not to get confused and buy a module active buzzerif that's not what you want. With an active buzzer You will only be able to play the factory default tone and will not be able to play melodies.. It is not easy to differentiate at first glance because the board is the same as that of a passive buzzer module.
Don't get confused and buy a buzzer module that doesn't have the necessary components (transistor and resistor, at least).
The NodeMCU does not have enough output current for it.
It has to be a passive buzzer module (a component board, as in the connection example or similar).
High or low level buzzer
Logic tells us that when we give voltage to the signal pin of the buzzer module, it should sound and when we remove it, it should stop sounding (high level control), but this is not always the case. There are buzzer modules (many) that work by low level.The signal pins are silent, i.e. they ring when there is no voltage on the signal pin and are silent when there is voltage on the signal pin.
Checking this is very easy. Just go to connect GND and VCC (without connecting the signal pin) and if it buzzes non-stop, the module is active low (and if you connect the buzzer module signal pin to VCC it should be muted).
This should be borne in mind, since in many cases the rules will have to be adapted for it to work properly.
Thank you very much, as soon as I have some time I'll get to work.
There is nothing better than doing something concrete and useful to learn, as well as, of course, researching on our own, not everything has to be given to us chewed up, it also takes an effort on our part.
Greetings.
I'm doing something wrong because the new buzzer keeps beeping.
Even if you delete the rules it still beeps, the only way to stop it (apart from removing the power supply of course) is to disconnect the D6 cable.
I think it is a passive buzzer, i.e. the right one, because it doesn't sound when powered alone, only when connected to D6.
I will keep testing.
Greetings.
Hello Zulu.
Let's see what might be happening...
The first thing is to see if and how the buzzer works. We are going to use the NodeMCU only as a 5V power supply (if you have another 5V DC power supply, you can use it).
To begin with, do the following two tests:
Test 1: With the NodeMCU off (USB cable removed), connect the Positive and Negative of the buzzer to Vin and GND of the NodeMCU (the 5V output). Connect the signal cable from the buzzer to GND. Turn on the NodeMCU (connect the USB) Does the buzzer sound, click or beep?
Test 2:With the NodeMCU off (USB cable removed), connect the Positive and Negative of the buzzer to Vin and GND of the NodeMCU (the 5V output). Connect the signal cable from the buzzer to Vin. Turn on the NodeMCU (connect the USB) Does the buzzer sound, click or beep?
The two tests are the same, the only difference is that the buzzer control wire is connected to positive (Vin) or GND (negative). With this we will find out if it is a buzzer activated by positive or negative control.
Knowing whether it just clicks or beeps will tell us whether it is a passive (click) or active (beep) buzzer.
If nothing sounds at all, something is wrong.
Do not apply voltage to the buzzer for more than a couple of seconds (especially if it clicks). A passive buzzer cannot operate on direct current and will burn out.
If you have trouble hearing if it clicks when the USB is connected, you can connect and disconnect the positive wire from the buzzer to Vin several times. This will probably make it easier.
Depending on the results of these tests we continue to...
Hi Mariete, first of all I apologise for my delay in replying but I have not had a chance to reply until now.
The buzzer beeps continuously when the signal cable is connected to GND.
I have seen the manual to add a strip of LEDs to the meter, it seems to me a great idea and easy to do following your magnificent explanations that leave no room for doubt.
I also have to print the box for the whole invention.
Thank you very much for everything.
Best regards.
In other words, it is active. Not valid.
I have a few buzzers on the way, I hope they are passive, because although I ordered them passive, at first glance they are indistinguishable from each other.
You could use the buzzer, even if it is active. But it will only beep, you can't make tones.
You will also need to take into account that your buzzer will beeps when NO voltage is received on the signal cable.. You will have to change "Pin mode GPIO-X" in the Hardware tab and set it to "Output High" (this is so that when it starts up it will output voltage on that pin and so the buzzer will not beep, although it will probably make some noise while it starts up). Then you will have to adjust the rules to turn off the voltage output on that pin when you want the buzzer to beep and turn it back up again to make it beep.
The truth is that the buzzer issue is a mess because most of the sellers indicate very badly what they are selling. I make the buzzer modules myself and that way I get rid of problems (I have a video recorded. One of these days I'll put it up).
Anyway, I will be completing this article with instructions on how to use different types of buzzers (for example this one).
Great article as always Mario.
One question, do you have a link to a buzzer you recommend?
Thank you
Hello José.
Thank you very much. I'm glad you find it useful.
I have added to the article an option to buy the passive buzzer module on Amazon and another one on AliExpress.
Thanks Mario, I'll get on it too, glad to learn with everyone!
Hello Mariete,
Just today I bought the buzzer and as soon as I got it I started looking for how to connect it. To my surprise, you can actually use any loudspeaker (I used a Renfe headset) with RTTTL: https://www.letscontrolit.com/wiki/index.php?title=Buzzer_(RTTTL)
My assembly:
https://prnt.sc/10kbyz0
To test it, just enter the command in Tools ("13" is the GPIO I'm using, the D7 of the nodecmu):
rtttl,13:d=4,o=5,b=180,8f,8f,8f,2a#.,2f.6,8d#6,8d6,8c6,2a#.6,f.6,8d#6,8d6,8c6,2a#.6,f.6,8d#6,8d6,8d#6,2c6
Or via http (changing the # for )
http://ESPEASYIP/control?cmd=rtttl,13:d=4,o=5,b=180,8f,8f,8f,2a%23.,2f.6,8d6,8d6,8c6,2a.6,f.6,8d6,8d6,8c6,2a.6,f.6,8d6,8d6,8d6,2c6
Note that I have read elsewhere that it is recommended to put a resistor between the nodecmu and the speaker (red wire), although I understand that this is for speakers that require more power.
May the force be with you!