This repository contains Arduino code for controlling the built-in RGB LED on the ESP32-C6 microcontroller. It provides a simple and flexible way to manage LED colors with brightness control.
The built-in RGB LED on the ESP32-C6 is connected to GPIO 8
. This is already configured in the code, but it's crucial to know if you're working with other components or modifying the pin configuration.
- Easy control of the built-in RGB LED on GPIO 8
- Predefined color constants for common colors
- Brightness control (0-100%)
- Simple function to set any RGB color
- Easily expandable to include more colors and patterns
- Examples folder with playful patterns (breathing, rainbow, sparkle)
- Arduino IDE
- ESP32 board support for Arduino IDE
- Adafruit NeoPixel library
- Install the Arduino IDE from arduino.cc
- Add ESP32 board support to Arduino IDE:
- Open Arduino IDE
- Go to File > Preferences
- Add the following URL to "Additional Boards Manager URLs":
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
- Go to Tools > Board > Boards Manager
- Search for "esp32" and install the latest version
- Install the Adafruit NeoPixel library:
- Go to Sketch > Include Library > Manage Libraries
- Search for "Adafruit NeoPixel"
- Install the latest version
- Download the
esp32_c6_rgb_led_control.ino
file - Open the
.ino
file in Arduino IDE - Select your ESP32-C6 board from Tools > Board menu
- Select the appropriate port from Tools > Port menu
- Click the Upload button to flash the code to your ESP32-C6
Here's the basic code structure:
#include <Adafruit_NeoPixel.h>
constexpr uint8_t LED_PIN = 8;
constexpr uint8_t NUM_LEDS = 1;
Adafruit_NeoPixel rgbLed(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
struct RGB {
uint8_t r, g, b;
};
constexpr RGB COLOR_OFF = {0, 0, 0};
// ...Feel free to add more colors here...
constexpr RGB CUSTOM_COLOR = {255, 0, 255};
void setColor(const RGB& color, uint8_t brightness = 100) {
uint16_t scale = (uint16_t)brightness * 255 / 100;
uint8_t r = (uint8_t)(((uint16_t)color.r * scale) / 255);
uint8_t g = (uint8_t)(((uint16_t)color.g * scale) / 255);
uint8_t b = (uint8_t)(((uint16_t)color.b * scale) / 255);
rgbLed.setPixelColor(0, rgbLed.Color(r, g, b));
rgbLed.show();
}
void setup() {
rgbLed.begin();
rgbLed.show();
}
void loop() {
setColor(CUSTOM_COLOR, 50); // 50% brightness
delay(1000);
setColor(COLOR_OFF);
delay(1000);
}
- The
LED_PIN
is set to8
for the built-in LED. Do not change this unless you're using an external LED. - Add new color definitions in the
RGB
struct format - Adjust brightness by passing a value (0-100) to setColor()
- Create new light patterns by combining
setColor()
andblinkColor()
functions - Modify the
BLINK_DURATION
to change the speed of the blinking pattern - Customize the brightness level in
blinkColor()
by modifying the hardcoded 50% value
Contributions to improve the code or add new features are welcome. Please feel free to submit a pull request or open an issue.