Arduino - RGB LED

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
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

Arduino 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 Arduino pins that connects to R, G, and B pins. For example:
const int PIN_RED = 5; const int PIN_GREEN = 6; const int PIN_BLUE = 7;
  • Configure these Arduino 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);

Arduino - 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 ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rgb-led */ 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 code by creating a function:

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rgb-led */ 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 Arduino.
    • 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 .....

※ OUR MESSAGES