ibp-project-2

RGB LED Control with ESP32 - Innovator Box Plus Project 2

RGB LED Control with ESP32

Learn how to control RGB LEDs using PWM with your Innovator Box Plus and ESP32

Objective

In this project, you'll learn how to control an RGB LED using Pulse Width Modulation (PWM) with the ESP32 microcontroller. You'll be able to create smooth color transitions and understand how RGB color mixing works.

Components Needed

  • ESP32 Development Board (included in Innovator Box Plus)
  • RGB LED (common cathode)
  • 3 x 220 ohm resistors
  • Breadboard
  • Jumper wires
  • USB cable

Step 1: Understanding RGB LED and PWM

An RGB LED is essentially three LEDs in one package - a red, green, and blue LED. By controlling the brightness of each individual LED, we can create millions of different colors.

Pulse Width Modulation (PWM) is a technique used to control the average power delivered to a load by rapidly switching between on and off states. The proportion of time that the signal is on (called the duty cycle) determines the average power delivered.

Note: The ESP32 has 16 PWM channels that can be configured to generate signals with different properties.

RGB Color Mixing

Here's how RGB color mixing works:

  • Red + Green = Yellow
  • Red + Blue = Magenta
  • Green + Blue = Cyan
  • Red + Green + Blue = White

By varying the intensity of each color (using PWM), we can create virtually any color in the visible spectrum.

Step 2: Assembling the Circuit

Follow these steps to build the circuit on your breadboard:

  1. Insert the RGB LED into the breadboard. Make sure the longest leg (common cathode) is connected to GND.
  2. Connect the red LED pin through a 220 ohm resistor to GPIO pin 25 on the ESP32.
  3. Connect the green LED pin through a 220 ohm resistor to GPIO pin 26 on the ESP32.
  4. Connect the blue LED pin through a 220 ohm resistor to GPIO pin 27 on the ESP32.
  5. Connect the common cathode pin (longest leg) to GND on the ESP32.

Tip: If you're using a common anode RGB LED, you'll need to connect the longest pin to 3.3V instead of GND, and invert the PWM logic in the code.

Circuit Diagram

RGB LED Circuit Diagram
RGB LED Circuit Connection with ESP32

Step 3: Writing the Code

Open the Arduino IDE and create a new sketch. Copy and paste the following code:

// RGB LED Control with ESP32
// For Innovator Box Plus Project 2

// Define pins for RGB LED
#define RED_PIN 25
#define GREEN_PIN 26
#define BLUE_PIN 27

// PWM properties
#define PWM_FREQUENCY 5000
#define PWM_RESOLUTION 8
#define RED_CHANNEL 0
#define GREEN_CHANNEL 1
#define BLUE_CHANNEL 2

void setup() {
  // Configure LED PWM channels
  ledcSetup(RED_CHANNEL, PWM_FREQUENCY, PWM_RESOLUTION);
  ledcSetup(GREEN_CHANNEL, PWM_FREQUENCY, PWM_RESOLUTION);
  ledcSetup(BLUE_CHANNEL, PWM_FREQUENCY, PWM_RESOLUTION);
  
  // Attach the PWM channels to the GPIO pins
  ledcAttachPin(RED_PIN, RED_CHANNEL);
  ledcAttachPin(GREEN_PIN, GREEN_CHANNEL);
  ledcAttachPin(BLUE_PIN, BLUE_CHANNEL);
  
  Serial.begin(115200);
  Serial.println("RGB LED Control with ESP32");
}

void loop() {
  // Fade from red to green
  for(int i = 0; i <= 255; i++) {
    setColor(255-i, i, 0);
    delay(10);
  }
  
  // Fade from green to blue
  for(int i = 0; i <= 255; i++) {
    setColor(0, 255-i, i);
    delay(10);
  }
  
  // Fade from blue to red
  for(int i = 0; i <= 255; i++) {
    setColor(i, 0, 255-i);
    delay(10);
  }
}

// Function to set RGB color
void setColor(int red, int green, int blue) {
  ledcWrite(RED_CHANNEL, red);
  ledcWrite(GREEN_CHANNEL, green);
  ledcWrite(BLUE_CHANNEL, blue);
  
  Serial.print("RGB: ");
  Serial.print(red);
  Serial.print(", ");
  Serial.print(green);
  Serial.print(", ");
  Serial.println(blue);
}

Code Explanation

The code does the following:

  1. Defines GPIO pins for the red, green, and blue LED connections
  2. Sets up PWM properties including frequency, resolution, and channels
  3. Configures the PWM channels and attaches them to the GPIO pins
  4. Creates a loop that smoothly transitions between red, green, and blue colors
  5. Uses a helper function called setColor() to update the RGB values

Step 4: Uploading the Code

Follow these steps to upload the code to your ESP32:

  1. Connect your ESP32 to your computer using a USB cable
  2. Select the correct board from Tools > Board menu (ESP32 Dev Module)
  3. Select the correct port from Tools > Port menu
  4. Click the Upload button (right arrow icon)

Warning: If you encounter any upload errors, make sure you have installed the ESP32 board package in the Arduino IDE. Also, some ESP32 boards require holding the BOOT button while starting the upload process.

After uploading, you should see the RGB LED smoothly cycle through different colors.

Experimenting with the Project

Creating Custom Colors

Try modifying the code to create specific colors. Here are some RGB values for common colors:

Color Red value Green value Blue value
Red 255 0 0
Green 0 255 0
Blue 0 0 255
Yellow 255 255 0
Purple 128 0 128
Cyan 0 255 255
White 255 255 255

Creating a Color Fading Pattern

Try implementing a different color fading pattern. For example, you could create a rainbow effect or pulse the LED with varying brightness.

Troubleshooting

LED Not Lighting Up

  • Check if the LED is connected in the correct orientation. The longest leg should be connected to GND (common cathode).
  • Verify that the resistors are properly connected to limit current to the LED.
  • Double-check the GPIO pin connections.

Colors Are Incorrect

  • You might be using a common anode RGB LED instead of a common cathode. In that case, you need to invert the PWM values (use 255-value instead of value).
  • Check if the RGB pins are connected to the correct GPIO pins as defined in the code.

PWM Not Working

  • Make sure you're using pins that support PWM functionality on the ESP32.
  • Check if there are any conflicts with other libraries or hardware that might be using the same PWM channels.

Innovator Box Plus Tips

Tip: The Innovator Box Plus includes multiple RGB LEDs that you can use for this project. Try connecting multiple LEDs to create more complex lighting effects!

Your Innovator Box Plus kit includes several other components that you can integrate with this project:

  • Potentiometers: You can use these to manually control the RGB values
  • Photoresistors: Create a light-responsive RGB LED
  • Buttons: Use them to switch between different color modes

Next Steps

Now that you've mastered RGB LED control, you can expand on this project in several ways:

  1. Create a web server on the ESP32 that allows you to control the RGB LED color from a web browser
  2. Add sensors to change the LED color based on environmental conditions
  3. Create a multi-LED setup for more complex lighting effects
  4. Move on to Project 3: Temperature Sensor to learn about integrating sensors with your ESP32