esp32 c3 rgb led example

ESP32 C3 RGB LED Example: Illuminate Your Projects

By Laura Miller
AD

Looking to illuminate your ESP32-C3 project with vibrant colors? Controlling an RGB LED with this microcontroller is not only fun but also practical, enabling you to add visual feedback or create unique lighting effects. Here's a comprehensive guide on how to connect and manage an RGB LED with your ESP32-C3, packed with examples and practical tips.

First, let's understand the ESP32-C3's capabilities in handling RGB LEDs. The ESP32-C3 is equipped with GPIO pins that can output PWM (Pulse Width Modulation) signals, essential for controlling the LED's color and brightness. Before you dive into the code, ensure you have the latest ESP32-C3 development board, an RGB LED (common cathode or common anode), and proper resistors to prevent overloading.

Setting Up the Circuit

Connecting an RGB LED to your ESP32-C3 involves a few simple steps. For a common cathode LED, connect the-longer cathodes (GND pins) to the ESP32-C3's GND, and the shorter anodes (VCC pins) to different GPIO pins. Don't forget to add resistors (around 220-330 ohms) for each color channel - red, green, and blue - to protect your LED and microcontroller from excessive current.

AD
10 ESP32 Skills Every Maker Should Learn
10 ESP32 Skills Every Maker Should Learn
Controlling RGB Lights From ESP32 Web Server
Controlling RGB Lights From ESP32 Web Server
💡 ESP32 Web Server – Control an LED from Your Browser!
💡 ESP32 Web Server – Control an LED from Your Browser!

For a common anode configuration, the process remains similar; however, you'll connect the longer anodes to your ESP32-C3's GPIO pins, and all cathodes will share a GND.

Understanding RGB LED Control

RGB LEDs work by varying the intensity of each color (red, green, blue). The ESP32-C3 generates PWM signals to control the LED's color. Each color channel uses a different GPIO pin, allowing independent control of red, green, and blue colors.

Modulo ESP32 Devkit V1 - Led RGB WS2812B - www.adrirobot.it
Modulo ESP32 Devkit V1 - Led RGB WS2812B - www.adrirobot.it
WLED ON XIAO ESP32
WLED ON XIAO ESP32
Come Collegare Strisce LED ANALOGICHE RGB con ESP32 e WLED – Guida Completa
Come Collegare Strisce LED ANALOGICHE RGB con ESP32 e WLED – Guida Completa

PWM signals produce a smooth, variable brightness effect by rapidly turning the LED on and off. The duty cycle (the portion of time the signal is high, or 'on') determines the perceived brightness. When the duty cycle is 100%, the LED appears fully lit, while a lower duty cycle reduces brightness.

ESP32-C3 PWM Configuration

The ESP32-C3's PWM generation is straightforward with the built-in `pwm_set_duty` and `pwm_set_freq` functions in the `driver/gpio.h` library. To control an RGB LED, you'll need to initialize three PWM channels, each corresponding to one of the colors.

AD

You'll also need to ensure that the GPIO pins you're using support PWM output. The ESP32-C3 offers PWM functionality on several GPIO pins, making RGB LED control versatile and accessible. Don't forget to set each pin's function to PWM using the `gpio_set_pwm_mode` function.

ESP32 Pinout Guide: GPIO, Buttons & Ports Explained
ESP32 Pinout Guide: GPIO, Buttons & Ports Explained
ESP32 Simulator - LED chaser project 🎉😍-2022
ESP32 Simulator - LED chaser project 🎉😍-2022
ESP32 GPIO with ESP-IDF with LED Blinking example
ESP32 GPIO with ESP-IDF with LED Blinking example
How to Easily Control Addressable LEDs with an ESP32 or ESP8266 | WLED Project
How to Easily Control Addressable LEDs with an ESP32 or ESP8266 | WLED Project
ESP32 RGB LED Controller Web Server - Remote Color Picker
ESP32 RGB LED Controller Web Server - Remote Color Picker
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes

Example: Blinking RGB LED

Let's create a simple example that makes the RGB LED slowly change color, demonstrating each primary color and a mix of them, too. This example uses the `delay` function to control the speed and color transitions.

Here's a simple Arduino sketch for your ESP32-C3 to get you started:

AD
```cpp #define LED_RED 2 #define LED_GREEN 3 #define LED_BLUE 4 void setup() { // Initialize RGB LED pins as output and PWM mode pinMode(LED_RED, OUTPUT); pinMode(LED_GREEN, OUTPUT); pinMode(LED_BLUE, OUTPUT); ledcSetup(LED_RED, 5000, 8); ledcSetup(LED_GREEN, 5000, 8); ledcSetup(LED_BLUE, 5000, 8); ledcAttachPin(LED_RED, LED_RED); ledcAttachPin(LED_GREEN, LED_GREEN); ledcAttachPin(LED_BLUE, LED_BLUE); } void loop() { // Red ledcWrite(LED_RED, 255); delay(500); // Green ledcWrite(LED_GREEN, 255); delay(500); // Blue ledcWrite(LED_BLUE, 255); delay(500); // Cyan ledcWrite(LED_BLUE, 255); ledcWrite(LED_GREEN, 255); delay(500); // Magenta ledcWrite(LED_BLUE, 255); ledcWrite(LED_RED, 255); delay(500); // Yellow ledcWrite(LED_RED, 255); ledcWrite(LED_GREEN, 255); delay(500); // White ledcWrite(LED_RED, 255); ledcWrite(LED_GREEN, 255); ledcWrite(LED_BLUE, 255); delay(500); // Off ledcWrite(LED_RED, 0); ledcWrite(LED_GREEN, 0); ledcWrite(LED_BLUE, 0); delay(500); } ```

Playing around with different delay times and values can create various lighting patterns and color transitions, adding a dynamic element to your project.

ESP32 C3 Supermini Pinout
ESP32 C3 Supermini Pinout
Usefulelectronics/esp32-homekit-lightbulb
Usefulelectronics/esp32-homekit-lightbulb
ESP32 - LED Matrix | ESP32 Tutorial
ESP32 - LED Matrix | ESP32 Tutorial
ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials
ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials
ESP32-C3 Super Mini and an ILI9488 SPI TFT display
ESP32-C3 Super Mini and an ILI9488 SPI TFT display
WS2812B Music Reactive LED Using ESP32 & WLED
WS2812B Music Reactive LED Using ESP32 & WLED
Build your own 5-in-1 RGB + CCT LED Strip Controller with ESP32 D1 Mini and WLED.
Build your own 5-in-1 RGB + CCT LED Strip Controller with ESP32 D1 Mini and WLED.
5 Must-Try ESP32 Projects for Beginners (2025 Edition)
5 Must-Try ESP32 Projects for Beginners (2025 Edition)
an electronic device with two eyes on it's face and wires attached to the board
an electronic device with two eyes on it's face and wires attached to the board
ESP NeoPixel - Share Project - PCBWay
ESP NeoPixel - Share Project - PCBWay
How to Use ESP32 to Control LED With Blynk Via WiFi
How to Use ESP32 to Control LED With Blynk Via WiFi
ESP32-C3 Super Mini Pinout & Specs 📌⚡
ESP32-C3 Super Mini Pinout & Specs 📌⚡
AD