Buttons, LEDs and buzzer together
What this example shows
Using the buttons to drive the LEDs and the buzzer on the same board. Each button lights one LED in its own colour while you hold it, and the buzzer plays the tone of the highest-numbered button you hold.
The example code
/**
**************************************************
*
* @file ButtonInteraction.ino
* @brief Button interaction example for Soldered Button, LED & Buzzer Board
* Host: ESP32 WROVER-E
*
* Press a button to light its LED and sound the buzzer:
* BTN1 -> LED1 red, 1000 Hz
* BTN2 -> LED2 green, 2000 Hz
* BTN3 -> LED3 blue, 3000 Hz
*
* Multiple buttons can be pressed simultaneously.
* Buzzer plays the highest pressed button's frequency.
*
* @authors Fran Fodor @ soldered.com
***************************************************/
#include "ButtonLedBuzzerBoard-SOLDERED.h"
ButtonLedBuzzerBoard_Soldered board;
void setup()
{
Wire.begin(); // if not using Qwiic, specify I2C pins: Wire.begin(21, 22)
board.begin();
delay(2000);
board.setAllLEDs(0, 0, 0);
board.setBuzzer(0);
}
void loop()
{
uint8_t buttons = board.readButtons();
bool btn1 = buttons & 0x01;
bool btn2 = buttons & 0x02;
bool btn3 = buttons & 0x04;
board.setLED(0, btn3 ? 255 : 0, 0, 0);
board.setLED(1, 0, btn2 ? 255 : 0, 0);
board.setLED(2, 0, 0, btn1 ? 255 : 0);
if (btn3) board.setBuzzer(3000);
else if (btn2) board.setBuzzer(2000);
else if (btn1) board.setBuzzer(1000);
else board.setBuzzer(0);
// remove delay for smooth tone
delay(20);
}
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. While you hold a button:
| Button | LED | Colour | Tone |
|---|---|---|---|
| BTN1 | LED 2 | Blue | 1000 Hz |
| BTN2 | LED 1 | Green | 2000 Hz |
| BTN3 | LED 0 | Red | 3000 Hz |
Hold several buttons and each of their LEDs lights, while the buzzer plays the tone of the highest-numbered button. Release them all and the LEDs and the buzzer go off.
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.
setAllLEDs(uint8_t r, uint8_t g, uint8_t b)returns voidSets all three LEDs to the same colour and sends them to the board. setAllLEDs(0, 0, 0) turns every LED off.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | r | Red brightness, 0 (off) to 255 (full). |
uint8_t | g | Green brightness, 0 to 255. |
uint8_t | b | Blue brightness, 0 to 255. |
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. |
readButtons()returns uint8_tReads the board's button register and returns the state of all three buttons in one byte. Bit 0 is BTN1, bit 1 is BTN2 and bit 2 is BTN3; a bit is 1 while its button is pressed. If the board does not answer, the function returns 0, which looks the same as no button pressed.
ReturnsThe button bits: 0x01 for BTN1, 0x02 for BTN2, 0x04 for BTN3, combined when several are pressed.
setLED(uint8_t index, uint8_t r, uint8_t g, uint8_t b)returns voidSets the colour of one LED. The library keeps the colours of all three LEDs and sends all of them to the board each time, so the other two LEDs keep their colours. An index of 3 or more is ignored.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | index | Which LED, 0 to 2. |
uint8_t | r | Red brightness, 0 (off) to 255 (full). |
uint8_t | g | Green brightness, 0 to 255. |
uint8_t | b | Blue brightness, 0 to 255. |
Putting it together
Every 20 ms the sketch reads all three buttons with one readButtons() call and splits the byte with the masks 0x01, 0x02 and 0x04. It then sets each LED to full brightness in its colour, or to off, so the LEDs always follow the buttons. The if chain checks BTN3 first, which is why the highest-numbered pressed button sets the tone.
The comment at the top of the sketch pairs BTN1 with red and BTN3 with blue. The code swaps those two colours, and the table above follows the code.