-
Notifications
You must be signed in to change notification settings - Fork 8
Exercise 04: Control Small Buzzer on GPIO13 (Melody)
Buzzer is a piezoelectrical electronic components, which generate tones when the piezo elements is vibrated based on given voltage supply, different voltage will produce different vibration, thus generating different tone. Since ESP32 has the ability to generate range of voltage using, the built-in PWM controllers, we can apply it on our small buzzer to generate tone.
On Hibiscus Sense, there is 1x small buzzer, labelled as BUZZER
on-board, as circled on the image below.
The operating voltage of the small buzzer is ranging from 2-4V with rated voltage of 3V, the (+ve) terminal
of the buzzer is connected to GPIO13
, while the (-ve) terminal
is connected to GND
, as shown in the schematic below. The 3rd terminal of the buzzer is not connected.
To produce the tone or melody from the buzzer, we need to write PWM functions to control the PWM output signal on GPIO13. In Exercise 2 and Exercise 3 we use ledcWrite(_channel_, _dutycycle_)
function to control the brightness of the LEDs, while to generate the tone, ESP32 has other function known as ledcWriteTone(_channel_, _note_)
function, with 2 arguments:
- channel the number of the PWM channel.
- note the the frequency of note. Example, Note D4 = 147 Hz. Click here for available tones.
In this exercise, all the available note and its frequency to produce the tone, has been declared on a header file, titled tones.h
as here for available tones.
Since we have notice, we need to use ledcWriteTone()
function instead of ledcWrite()
function to generate the PWM output signal for the buzzer to produce the tone, let's write the program as below:
Complete Sketch
// include the preset tones file, which included in local header files named tones.h
#include "tones.h"
void setup() {
// configure PWM controller congfiguration
ledcSetup(0, 5000, 8);
// declare the GPIO number for PWM signal output
ledcAttachPin(13, 0);
}
void loop() {
// ledcWriteTone() function will generate PWM signal based on given tone frequency.
// 1st argument: PWM channel number.
// 2nd argument: Tone frequency.
ledcWriteTone(0, NOTE_D4); // buzzer will sound according to NOTE_D4.
delay(500);
ledcWrite(0, 0); // buzzer has no sound since PWM signal is 0.
delay(500);
}
Detail Sketch Explanations
First, we need to include header file, which has the declaration of all available note.
#include "tones.h"
In void setup()
function, we have to configure the PWM channel using ledcSetup()
function and declare buzzer (+ve) terminal attach to GPIO13 using ledcAttachPin()
function.
ledcSetup(0, 5000, 8);
ledcAttachPin(13, 0);
Inside the void loop()
function, we use ledcWriteTone()
function to generate sound of NOTE_D4
frequency, while ledcWrite()
function to turn OFF the buzzer. Therefore, the execution of these functions repeatedly, producing alarm-like sound.
ledcWriteTone(0, NOTE_D4); // buzzer will sound according to NOTE_D4.
delay(500);
ledcWrite(0, 0); // buzzer has no sound since PWM signal is 0.
delay(500);
Now, we can upload the complete sketch to ESP32. Once done uploading, we'll hear beeping sound.