Example Code for Arduino-RGB LED

Last revision 2026/02/05

The article offers a comprehensive example code for using an onboard RGB LED with Arduino's FastLED library, detailing hardware setup and programming instructions for creating color sequences.

Hardware Preparation

Other Preparation Work

  1. Firebeetle 2 ESP32-E has an onboard WS2812 RGB LED connected to the pin IO5/D8, which is not broken out from the board, so IO5/D8 is exclusively reserved for the RGB LED, located as shown in the figure below:
    DFR0654-RGB LED

  2. FastLED is a powerful and easy-to-use Arduino library for controlling LED strips such as WS2810 and LPD8806. Currently, it is widely recognized as one of the most popular LED control libraries for Arduino developers. You can install FastLED by referring to the method for installing the SDK of FireBeetle 2 ESP32-E before, as shown below, click "Install".
    DFR0654-FastLED

Next, we'll learn how to light up the RGB LED without external hardware connections.

Sample Code

Program function: Light up the on-board RGB LED and make it show red, green, blue and a randomly-mixed color in sequence repeatedly.

#include <FastLED.h>
#define NUM_LEDS 1     //Number of RGB LED beads
#define DATA_PIN D8    //The pin for controlling RGB LED
#define LED_TYPE NEOPIXEL    //RGB LED strip type
CRGB leds[NUM_LEDS];    //Instantiate RGB LED

void setup() { 
    FastLED.addLeds<LED_TYPE, DATA_PIN>(leds, NUM_LEDS);     //Initialize RGB LED
}

void loop() { 
  leds[0] = CRGB::Red;     //LED shows red light
  FastLED.show();
  delay(1000);
  leds[0] = CRGB::Green;    //LED shows green light
  FastLED.show();
  delay(1000);
  leds[0] = CRGB::Blue;     // LED shows blue light
  FastLED.show();
  delay(1000);
  leds[0] = CRGB(random(0,255),random(0,255),random(0,255));    // LED shows a randomly mixed color
  FastLED.show();
  delay(1000);
}

Result

After the program is successfully uploaded, the on-board RGB LED switches between red, green, blue and randomly mixed colors every second.

Was this article helpful?

TOP