Logo

ESP32 Simple RGB LED Controller

Introduction to ESP32 Simple RGB LED Controller

In this project, you will learn how to control an RGB LED using the ESP32. An RGB LED can produce various colors by combining the Red, Green, and Blue channels. You can control the brightness of each color individually to create a wide range of colors. This can be achieved through PWM (Pulse Width Modulation) and can be controlled either through a web interface or physical buttons connected to the ESP32.

Components Required

The following components are needed to build the ESP32 RGB LED controller system: ESP32 development board, common cathode RGB LED, 220Ω resistors (3 pieces), jumper wires, and a breadboard.

  • Connect the common cathode RGB LED to the ESP32. The Red, Green, and Blue pins are connected to three GPIO pins of the ESP32.
  • Use PWM to control the brightness of the RGB LED on each of the color channels (Red, Green, Blue).
  • Optionally, create a simple web interface to control the color of the LED remotely.
  • Upload the code to the ESP32 and test the color changes on the RGB LED.

Formula: RGB LED = (Red, Green, Blue) → Color combination based on PWM values

Components List

  • 1 × ESP32 Development Board
  • 1 × Common Cathode RGB LED
  • 3 × 220Ω Resistors
  • Jumper wires
  • Breadboard

Pin Configuration

  • GPIO 12 (Red): The Red pin of the RGB LED is connected to GPIO 12 of the ESP32.
  • GPIO 13 (Green): The Green pin of the RGB LED is connected to GPIO 13 of the ESP32.
  • GPIO 14 (Blue): The Blue pin of the RGB LED is connected to GPIO 14 of the ESP32.

Make sure to use appropriate resistors (220Ω) for each color channel of the RGB LED to prevent overloading the GPIO pins of the ESP32.

Wiring and Connections

  • -> GPIO 12 (Red)
  • -> GPIO 13 (Green)
  • -> GPIO 14 (Blue)

Code for ESP32 Simple RGB LED Controller

1#define RED_PIN 12
2#define GREEN_PIN 13
3#define BLUE_PIN 14
4
5void setup() {
6  pinMode(RED_PIN, OUTPUT);
7  pinMode(GREEN_PIN, OUTPUT);
8  pinMode(BLUE_PIN, OUTPUT);
9}
10
11void loop() {
12  // Set the LED to Red
13  analogWrite(RED_PIN, 255);  // Max brightness for Red
14  analogWrite(GREEN_PIN, 0);  // Turn off Green
15  analogWrite(BLUE_PIN, 0);  // Turn off Blue
16  delay(1000);  // Wait for 1 second
17
18  // Set the LED to Green
19  analogWrite(RED_PIN, 0);  // Turn off Red
20  analogWrite(GREEN_PIN, 255);  // Max brightness for Green
21  analogWrite(BLUE_PIN, 0);  // Turn off Blue
22  delay(1000);  // Wait for 1 second
23
24  // Set the LED to Blue
25  analogWrite(RED_PIN, 0);  // Turn off Red
26  analogWrite(GREEN_PIN, 0);  // Turn off Green
27  analogWrite(BLUE_PIN, 255);  // Max brightness for Blue
28  delay(1000);  // Wait for 1 second
29}

Code Explanation

  • #define RED_PIN 12: This defines GPIO 12 as the pin for the Red color channel of the RGB LED.
  • #define GREEN_PIN 13: This defines GPIO 13 as the pin for the Green color channel of the RGB LED.
  • #define BLUE_PIN 14: This defines GPIO 14 as the pin for the Blue color channel of the RGB LED.
  • pinMode(RED_PIN, OUTPUT);: This sets the Red pin (GPIO 12) as an output, allowing it to control the LED's brightness.
  • pinMode(GREEN_PIN, OUTPUT);: This sets the Green pin (GPIO 13) as an output.
  • pinMode(BLUE_PIN, OUTPUT);: This sets the Blue pin (GPIO 14) as an output.
  • analogWrite(RED_PIN, 255);: This line sets the Red channel of the RGB LED to full brightness (255).
  • analogWrite(GREEN_PIN, 0);: This line turns off the Green channel by setting it to 0.
  • analogWrite(BLUE_PIN, 0);: This line turns off the Blue channel by setting it to 0.
  • delay(1000);: This introduces a delay of 1 second before changing the LED color.
  • analogWrite(RED_PIN, 0);: This line turns off the Red channel by setting it to 0.
  • analogWrite(GREEN_PIN, 255);: This line sets the Green channel to full brightness (255).
  • analogWrite(BLUE_PIN, 0);: This line turns off the Blue channel by setting it to 0.
  • analogWrite(BLUE_PIN, 255);: This line sets the Blue channel to full brightness (255).

Applications

  • Decorative lighting systems.
  • RGB indicators for various applications like temperature or status indication.
  • Home automation and smart lighting systems.

Conclusion

The ESP32 Simple RGB LED Controller project demonstrates how to use Pulse Width Modulation (PWM) to control the brightness of an RGB LED. This system can be expanded to include more colors or integrate with a web interface for remote control. It's a great foundation for creating visually interactive systems and lighting effects.