ESP32 - RGB LED

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×RGB LED
3×220 ohm resistor
1×Breadboard
4×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About RGB LED

The RGB LED can emit any colors by mixing the 3 basic colors red, green and blue. Actually, it consists of 3 separate LEDs red, green and blue packed together in a single case.

Pinout

RGB LED includes four pins:

  • Common (Cathode-) pin needs to be connected to GND (0V)
  • R (red) pin is used to control red
  • G (green) pin is used to control green
  • B (blue) pin is used to control blue
  • RGB LED Pinout

※ NOTE THAT:

The common pin can be cathode or anode, depending of the RGB LED type. This tutorial uses a common cathode one.

How it works

In the nature of physics, a color is composed of three color values: Red (R), Grean (G) and Blue (B). Each color value ranges from 0 to 255.

The mix of three values creates 256 x 256 x 256 colors in total.

If we provide PWM signals (with duty cycle from 0 to 255) to R, G, B pins, we can makes RGB LED displays any color we want.

The duty cycle of PWM signals to R, G and B pins correspond to color values of Red (R), Grean (G) and Blue (B)

Wiring Diagram

ESP32 RGB LED wiring diagram

Image is developed using Fritzing. Click to enlarge image

How To Control RGB LED

Let's lern step-by-step how to control the GRB LED to any color, for example #00979D:

  • Determine which color you want to display, get its color code. Tips:
  • Convert color code to R, G, B values using the tool from w3school. Take note these values. in this case: R = 0, G = 151, B = 157
  • RGB LED color picker
  • Define ESP32 pins that connects to R, G, and B pins. For example:
const int PIN_RED = 23; const int PIN_GREEN = 22; const int PIN_BLUE = 21;
  • Configure these ESP32 pins to the output mode
pinMode(PIN_RED, OUTPUT); pinMode(PIN_GREEN, OUTPUT); pinMode(PIN_BLUE, OUTPUT);
  • Control LED to emit that color (#00979D → R = 0, G = 151, B = 157)
analogWrite(PIN_RED, 0); analogWrite(PIN_GREEN, 151); analogWrite(PIN_BLUE, 157);

ESP32 - RGB LED Example Code

The below code changes color of LED among following colors in sequence:

  • #00C9CC (R = 0, G = 201, B = 204)
  • #F7788A (R = 247, G = 120, B = 138)
  • #34A853 (R = 52, G = 168, B = 83)
/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-rgb-led */ const int PIN_RED = 23; const int PIN_GREEN = 22; const int PIN_BLUE = 21; void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_GREEN, OUTPUT); pinMode(PIN_BLUE, OUTPUT); } void loop() { // color code #00C9CC (R = 0, G = 201, B = 204) analogWrite(PIN_RED, 0); analogWrite(PIN_GREEN, 201); analogWrite(PIN_BLUE, 204); delay(1000); // keep the color 1 second // color code #F7788A (R = 247, G = 120, B = 138) analogWrite(PIN_RED, 247); analogWrite(PIN_GREEN, 120); analogWrite(PIN_BLUE, 138); delay(1000); // keep the color 1 second // color code #34A853 (R = 52, G = 168, B = 83) analogWrite(PIN_RED, 52); analogWrite(PIN_GREEN, 168); analogWrite(PIN_BLUE, 83); delay(1000); // keep the color 1 second }

When using many colors, we could shorten the code by creating a function:

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-rgb-led */ const int PIN_RED = 23; const int PIN_GREEN = 22; const int PIN_BLUE = 21; void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_GREEN, OUTPUT); pinMode(PIN_BLUE, OUTPUT); } void loop() { // color code #00C9CC (R = 0, G = 201, B = 204) setColor(0, 201, 204); delay(1000); // keep the color 1 second // color code #F7788A (R = 247, G = 120, B = 138) setColor(247, 120, 138); delay(1000); // keep the color 1 second // color code #34A853 (R = 52, G = 168, B = 83) setColor(52, 168, 83); delay(1000); // keep the color 1 second } void setColor(int R, int G, int B) { analogWrite(PIN_RED, R); analogWrite(PIN_GREEN, G); analogWrite(PIN_BLUE, B); }

Addtional Knowledge

  • For RGB LED with common Anode, you need to:
    • Connect the common pin to 3.3V of ESP32.
    • Change R, G and B values in analogWrite() function to 255 - R, 255 - G, and 255 - B, respectively
  • A sequences of RCB LED connected together creates the RGB LED Strip. LED strip can be categorized in to the addressable LED strip and non-addressable LED Strip. We are going to make tutorials for each types of LED strip.

※ NOTE THAT:

Avoid using a single resistor in the common pin of RGB LED instead of three resistors in the other pins.

As we know, three LEDs in a single RGB package are in parallel. In ideal conditions, It is ok to use a single resistor in the common pin. However, in practice, do not use it. That is because the real world LED doesn't have the same characteristics. Three LEDs in the RGB package are NOT identical ⇒ Resistors of LEDs are different ⇒ The current is distributed unequally to each LED ⇒ Brightness is not the same and this may destroy a LED, and then destroy the other LEDs.

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere