Chapter 5 RGB LED

In this chapter, we will learn how to control a RGB LED. It can emit different colors of light. Next, we will use RGB LED to make a multicolored light.

Project 5.1 Random Color Light

In this project, we will make a multicolored LED. And we can control RGB LED to switch different colors automatically.

Component List

ESP32-WROVER x1

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Breadboard x1

Chapter01_02

RGBLED x1

Chapter05_00

Resistor 220Ω x1

Chapter01_04

Jumper M/M x2

Chapter01_05

Circuit

Schematic diagram

Chapter05_03

Hardware connection

If you need any support, please feel free to contact us via: support@freenove.com

Chapter05_04

Sketch

We need to create three PWM channels and use random duty cycle to make random RGB LED color.

Sketch_05.1_ColorfulLight

../../../_images/Chapter05_10.png

With the code downloaded to ESP32-WROVER, RGB LED begins to display random colors.

../../../_images/Chapter05_06.png

If you need any support, please feel free to contact us via: support@freenove.com

The following is the program code:

 1/**********************************************************************
 2  Filename    : ColorfulLight
 3  Description : Use RGBLED to show random color.
 4  Auther      : www.freenove.com
 5  Modification: 2024/06/18
 6**********************************************************************/
 7const byte ledPins[] = {4, 2, 15};    //define red, green, blue led pins
 8const byte chns[] = {0, 1, 2};        //define the pwm channels
 9int red, green, blue;
10void setup() {
11  for (int i = 0; i < 3; i++) {   //setup the pwm channels,1KHz,8bit
12    ledcAttachChannel(ledPins[i], 1000, 8, chns[i]);
13  }
14}
15
16void loop() {
17  red = random(0, 256);
18  green = random(0, 256);
19  blue = random(0, 256);
20  setColor(red, green, blue);
21  delay(200);
22}
23
24void setColor(byte r, byte g, byte b) {
25  ledcWrite(ledPins[0], 255 - r); //Common anode LED, low level to turn on the led.
26  ledcWrite(ledPins[1], 255 - g);
27  ledcWrite(ledPins[2], 255 - b);
28}

Define the PWM channel and associate it with the pin connected to RGB LED, and define the variable to hold the color value and initialize it in setup().

1const byte ledPins[] = {4, 2, 15};    //define red, green, blue led pins
2const byte chns[] = {0, 1, 2};        //define the pwm channels
3int red, green, blue;
4void setup() {
5  for (int i = 0; i < 3; i++) {   //setup the pwm channels,1KHz,8bit
6    ledcAttachChannel(ledPins[i], 1000, 8, chns[i]);
7  }
8}

In setColor(), this function controls the output color of RGB LED by the given color value. Because the circuit uses a common anode, the LED lights up when the GPIO outputs low power. Therefore, in PWM, low level is the active level, so 255 minus the given value is necessary.

1void setColor(byte r, byte g, byte b) {
2  ledcWrite(ledPins[0], 255 - r); //Common anode LED, low level to turn on the led.
3  ledcWrite(ledPins[1], 255 - g);
4  ledcWrite(ledPins[2], 255 - b);
5}

In loop(), get three random Numbers and set them as color values.

1red = random(0, 256);
2green = random(0, 256);
3blue = random(0, 256);
4setColor(red, green, blue);
5delay(200);

The related function of software PWM can be described as follows:

long random(min, max);

This function will return a random number(min — max-1).

Project 5.2 Gradient Color Light

In the previous project, we have mastered the usage of RGB LED, but the random display of colors is rather stiff. This project will realize a fashionable light with soft color changes.

Component list and the circuit are exactly the same as the random color light.

Using a color model, the color changes from 0 to 255 as shown below.

../../../_images/Chapter05_07.png

In this code, the color model will be implemented and RGB LED will change colors along the model.

Sketch_05.2_SoftColorfulLight

The following is the program code:

 1/**********************************************************************
 2  Filename    : SoftColorfulLight
 3  Description : Colorful light with gradually changing color.
 4  Auther      : www.freenove.com
 5  Modification: 2024/06/18
 6**********************************************************************/
 7const byte ledPins[] = {4, 2, 15};    //define led pins
 8const byte chns[] = {0, 1, 2};        //define the pwm channels
 9
10void setup() {
11  for (int i = 0; i < 3; i++) {   //setup the pwm channels
12    ledcAttachChannel(ledPins[i], 1000, 8, chns[i]);
13  }
14}
15
16void loop() {
17  for (int i = 0; i < 256; i++) {
18    setColor(wheel(i));
19    delay(20);
20  }
21}
22
23void setColor(long rgb) {
24  ledcWrite(ledPins[0], 255 - (rgb >> 16) & 0xFF);
25  ledcWrite(ledPins[1], 255 - (rgb >> 8) & 0xFF);
26  ledcWrite(ledPins[2], 255 - (rgb >> 0) & 0xFF);
27}
28
29long wheel(int pos) {
30  long WheelPos = pos % 0xff;
31  if (WheelPos < 85) {
32    return ((255 - WheelPos * 3) << 16) | ((WheelPos * 3) << 8);
33  } else if (WheelPos < 170) {
34    WheelPos -= 85;
35    return (((255 - WheelPos * 3) << 8) | (WheelPos * 3));
36  } else {
37    WheelPos -= 170;
38    return ((WheelPos * 3) << 16 | (255 - WheelPos * 3));
39  }
40}

In setColor(), a variable represents the value of RGB, and a hexadecimal representation of color is a common representation, such as 0xAABBCC, where AA represents the red value, BB represents the green value, and CC represents the blue value. The use of a variable can make the transmission of parameters more convenient, in the split, only a simple operation can take out the value of each color channel

1void setColor(long rgb) {
2  ledcWrite(ledPins[0], 255 - (rgb >> 16) & 0xFF);
3  ledcWrite(ledPins[1], 255 - (rgb >> 8) & 0xFF);
4  ledcWrite(ledPins[2], 255 - (rgb >> 0) & 0xFF);
5}

The wheel() function is the color selection method for the color model introduced earlier. The pos parameter ranges from 0 to 255 and outputs a color value in hexadecimal.