Skip to main content

Set the LED colours

What this example shows​

Lighting the three RGB LEDs, one at a time and all together. LEDs.ino steps each LED through red, green and blue, then shows the same colours on all three at once.

The example code​

Select Board
LEDs.ino
/**
**************************************************
*
* @file LEDs.ino
* @brief LED test for Soldered Button, LED & Buzzer Board
* Host: ESP32 WROVER-E
*
* Cycles each LED through red, green, blue individually,
* then cycles all LEDs together.
*
* @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);
}

void loop()
{
// Test each LED individually
for (uint8_t i = 0; i < 3; i++)
{
board.setAllLEDs(0, 0, 0);
board.setLED(i, 255, 0, 0); delay(400); // red
board.setLED(i, 0, 255, 0); delay(400); // green
board.setLED(i, 0, 0, 255); delay(400); // blue
}

// All LEDs together
board.setAllLEDs(255, 0, 0); delay(500); // red
board.setAllLEDs( 0, 255, 0); delay(500); // green
board.setAllLEDs( 0, 0, 255); delay(500); // blue
board.setAllLEDs(255, 255, 255); delay(500); // white
board.setAllLEDs( 0, 0, 0); delay(500); // off
}

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, LED 0, then LED 1, then LED 2 shows red, green and blue for 0.4 s each while the other two stay off. Then all three LEDs show red, green, blue and white for 0.5 s each and switch off for 0.5 s. The whole cycle takes about 6 s and repeats.

Functions used​

ButtonLedBuzzerBoard_Soldered(uint8_t address = BLB_DEFAULT_ADDRESS)returns None

Creates 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

TypeNameDescription
uint8_taddressThe board's I2C address, 0x30 to 0x37 as set by the address switch. Defaults to BLB_DEFAULT_ADDRESS (0x30).
begin()returns void

Starts 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 void

Sets all three LEDs to the same colour and sends them to the board. setAllLEDs(0, 0, 0) turns every LED off.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_trRed brightness, 0 (off) to 255 (full).
uint8_tgGreen brightness, 0 to 255.
uint8_tbBlue brightness, 0 to 255.
setLED(uint8_t index, uint8_t r, uint8_t g, uint8_t b)returns void

Sets 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

TypeNameDescription
uint8_tindexWhich LED, 0 to 2.
uint8_trRed brightness, 0 (off) to 255 (full).
uint8_tgGreen brightness, 0 to 255.
uint8_tbBlue brightness, 0 to 255.

Putting it together​

Each colour is three numbers from 0 to 255, one each for red, green and blue, so setLED(i, 255, 0, 0) is full red and setAllLEDs(255, 255, 255) is white. The sketch calls setAllLEDs(0, 0, 0) before each LED's turn, so only one LED is lit at a time; setLED() on its own leaves the other two LEDs as they were.

The library also has setLEDs(), which takes nine values and sets all three LEDs to different colours in one call. This example does not use it.