ESP8266 - LED RGB

This tutorial instructs you how to use ESP8266 to control RGB LED. In detail, we will learn:

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×RGB LED
3×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of RGB LED

The RGB LED can produce any color by combining the three primary colors of red, green, and blue. It is composed of three distinct LEDs( red, green, and blue), but all contained in a single housing.

The RGB LED Pinout

An RGB LED has four pins:

  • The common (cathode-) pin needs to be linked to GND (0V)
  • The R (red) pin is used to regulate red
  • The G (green) pin is used to regulate green
  • The B (blue) pin is used to regulate blue
RGB LED pinout

To make an RGB LED work with ESP8266, we gotta add current-limiting resistors. That can make the wiring a bit complicated. But hey, there's a solution! We can use an RGB LED module that already has resistors built-in. Easy peasy!

RGB LED Module also 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 Module Pinout

※ NOTE THAT:

The common pin that can be either a cathode or anode can vary depending on the type of RGB LED. This tutorial will use a common cathode pin.

How it works

In the realm of physics, three color values make up a color: Red (R), Green (G), and Blue (B). Each value ranges from 0 to 255. The combination of three values produces a total of 256 x 256 x 256 colors.

We can set the RGB LED to any color we desire by using ESP8266 to generate PWM signals (ranging from 0 to 255) to the R, G, and B pins. The duty cycle of PWM signals sent to the R, G and B pins are in proportion to the color values of Red, Green and Blue respectively.

Wiring Diagram

  • Wiring diagram between ESP8266 and RGB LED
The wiring diagram between ESP8266 NodeMCU and RGB LED

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

  • Wiring diagram between ESP8266 and RGB LED module
The wiring diagram between ESP8266 NodeMCU and RGB LED module

This image is created using Fritzing. Click to enlarge image

How To Control RGB LED

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

  • First, determine which color you want to display and get its color code. Tips:
  • Then, convert the color code to R, G, B values using the tool from w3school. Take note of these values. In this case: R = 0, G = 151, B = 157
RGB LED color picker
  • Specify the ESP8266 pins that link to the R, G, and B pins. For instance:
const int PIN_RED = 5; const int PIN_GREEN = 6; const int PIN_BLUE = 7;
  • Set the following ESP8266 pins to output mode:
pinMode(PIN_RED, OUTPUT); pinMode(PIN_GREEN, OUTPUT); pinMode(PIN_BLUE, OUTPUT);
  • Control LED to emit the color of #00979D, which is composed of Red = 0, Green = 151, and Blue = 157.
analogWrite(PIN_RED, 0); analogWrite(PIN_GREEN, 151); analogWrite(PIN_BLUE, 157);

ESP8266 - RGB LED Example Code

The ESP8266 code below changes the color of the LED in a sequence of:

  • #00C9CC (Red = 0, Green = 201, Blue = 204)
  • #F7788A (Red = 247, Green = 120, Blue = 138)
  • #34A853 (Red = 52, Green = 168, Blue = 83)
/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-led-rgb */ const int PIN_RED = 5; const int PIN_GREEN = 6; const int PIN_BLUE = 7; 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 ESP8266 code by creating a function:

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-led-rgb */ const int PIN_RED = 5; const int PIN_GREEN = 6; const int PIN_BLUE = 7; 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 ESP8266.
  • Invert the R, G and B values in analogWrite() function, i.e. use 255 - R, 255 - G, and 255 - B, respectively

A sequence of RGB LEDs connected together forms an RGB LED Strip. LED strips can be divided into addressable LED strips and non-addressable LED strips. We will create tutorials for both types of LED strips.

※ NOTE THAT:

Do not use a single resistor in the common pin of an RGB LED instead of three resistors in the other pins.

This is because, although in theory it is ok to use a single resistor in the common pin, in reality this is not the case. The LEDs in an RGB package are not identical, meaning that their resistors are different. As a result, the current is distributed unequally to each LED, causing the brightness to be unequal, which can lead to the destruction of one or more of the LEDs, and eventually the other LEDs.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!