Play tones on the buzzer
What this example shows
Playing tones at chosen frequencies on the passive buzzer. Buzzer.ino plays a rising sequence of five tones, then repeats it.
The example code
/**
**************************************************
*
* @file Buzzer.ino
* @brief Buzzer test for Soldered Button, LED & Buzzer Board
* Host: ESP32 WROVER-E
*
* Plays a sweep of frequencies to test the buzzer.
*
* @authors Fran Fodor @ soldered.com
***************************************************/
#include "ButtonLedBuzzerBoard-SOLDERED.h"
ButtonLedBuzzerBoard_Soldered board;
const uint16_t frequencies[] = {500, 1000, 2000, 3000, 4000};
const uint8_t numFreqs = sizeof(frequencies) / sizeof(frequencies[0]);
void setup()
{
Wire.begin(); // if not using Qwiic, specify I2C pins: Wire.begin(21, 22)
board.begin();
delay(2000);
}
void loop()
{
for (uint8_t i = 0; i < numFreqs; i++)
{
board.setBuzzer(frequencies[i]);
delay(400);
board.setBuzzer(0);
delay(200);
}
delay(1000);
}
This example also uses Wire, which is not part of the Soldered-Button-LED-Buzzer-Board library: it comes with your Arduino board package. Wire.begin() joins the I2C bus as a controller. Without Qwiic, the comment in the sketch shows how to pass your own I2C pins, for example Wire.begin(21, 22) on the ESP32 host the example was written for.
Expected result:
There is no serial output. About two seconds after the sketch starts, the buzzer plays 500, 1000, 2000, 3000 and 4000 Hz for 0.4 s each, with 0.2 s of silence between tones. It then stays quiet for 1 s and starts again.
Functions used
ButtonLedBuzzerBoard_Soldered(uint8_t address = BLB_DEFAULT_ADDRESS)returns NoneCreates the board object and remembers its I2C address. All three LEDs start as off in the library's colour buffer. The example uses the default address, 0x30, which the board has with all three address switches OFF.
ReturnsConstructor; no return value.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | address | The board's I2C address, 0x30 to 0x37 as set by the address switch. Defaults to BLB_DEFAULT_ADDRESS (0x30). |
begin()returns voidStarts the I2C bus by calling Wire.begin(). Call it once in setup(), before any other function.
ReturnsNothing.
setBuzzer(uint16_t freq)returns voidSends a tone frequency in Hz to the passive buzzer. The examples pass 0 to stop the tone.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint16_t | freq | Tone frequency in Hz. The examples pass 0 to stop the tone. |
Putting it together
The example assumes a tone continues until the next setBuzzer() call: it waits with delay() while the tone sounds and then calls setBuzzer(0) to stop it. To play a different sequence, change the values in frequencies[]; numFreqs follows the array size on its own.