How to Use an RGB LED

How to Use an RGB LED

This project will show you how to use an RGB LED.

  • 4,447 views
  • 0 comments
  • 20 respects

Components and supplies

Apps and online services

About this project

Operation

The RGB LEDs are LEDs that are tricolor and can offer a wide range of colors.

The RGB LEDs (RGB for Red, Green, Blue) are available in two versions: common anode or common cathode.

The common cathode versions are simpler to use for beginners because more intuitive. In this configuration, a voltage of 5 volts turns on the LED and a voltage of 0 volts turn it off.

Generally, in a real application, it is preferable to use RGB LEDs with common anode rather than the RGB LEDs with common cathode.

They are less practical to use but these versions have the immense advantage of being able to be controlled by specialized integrated circuits like the TLC5940 which can only absorb the current and not generate it, which makes the use of RGB LEDs impossible with common cathode in this case.

However, RGB LEDs with common anode have 8 colors while RGB LEDs with common cathode have 16.7 MILLION colors.

Connections

With common anode:

Arduino to LED

D11 --> First pin

5V --> Second pin

D10 --> Third pin

D9 --> Fourth pin

With common cathode:

Arduino to LED

D11 --> First pin

GND --> Second pin

D10 --> Third pin

D9 --> Fourth pin

Code

Code of RGB LED with common anode ( 8 colors )Arduino
const byte COLOR_BLACK = 0b000;
const byte COLOR_RED = 0b100;
const byte COLOR_GREEN = 0b010;
const byte COLOR_BLUE = 0b001;
const byte COLOR_MAGENTA = 0b101;
const byte COLOR_CYAN = 0b011;
const byte COLOR_YELLOW = 0b110;
const byte COLOR_WHITE = 0b111;


const byte PIN_LED_R = 9;
const byte PIN_LED_G = 10;
const byte PIN_LED_B = 11;


void setup() {

  pinMode(PIN_LED_R, OUTPUT);
  pinMode(PIN_LED_G, OUTPUT);
  pinMode(PIN_LED_B, OUTPUT);
  displayColor(COLOR_BLACK);
}

 
void loop() {
  
 
  displayColor(COLOR_RED);
  delay(1000);
  
  displayColor(COLOR_GREEN);
  delay(1000);
  
  displayColor(COLOR_BLUE);
  delay(1000);
  
  displayColor(COLOR_MAGENTA);
  delay(1000);
  
  displayColor(COLOR_CYAN);
  delay(1000);
  
  displayColor(COLOR_YELLOW);
  delay(1000);
  
  displayColor(COLOR_WHITE);
  delay(1000);
  
  displayColor(COLOR_BLACK);
  delay(1000);
}


void displayColor(byte color) {

  
  digitalWrite(PIN_LED_R, !bitRead(color, 2));
  digitalWrite(PIN_LED_G, !bitRead(color, 1));
  digitalWrite(PIN_LED_B, !bitRead(color, 0));
}
Code of RGB LED with common cathode ( 16.7 million colors )Arduino
const byte PIN_LED_R = 9;
const byte PIN_LED_G = 10;
const byte PIN_LED_B = 11;


void setup() {

 
  pinMode(PIN_LED_R, OUTPUT);
  pinMode(PIN_LED_G, OUTPUT);
  pinMode(PIN_LED_B, OUTPUT);
  displayColor(0, 0, 0);
}

 
void loop() {
  
 
  displayColor(255, 0, 0);
  delay(1000);
  
  displayColor(0, 255, 0);
  delay(1000);
  
  displayColor(0, 0, 255);
  delay(1000);
  
  displayColor(255, 0, 255);
  delay(1000);
  
  displayColor(0, 255, 255);
  delay(1000);
  
  displayColor(255, 255, 0);
  delay(1000);
  
  displayColor(255, 255, 255);
  delay(1000);
  
  displayColor(0, 0, 0);
  delay(1000);
}


void displayColor(byte r, byte g, byte b) {

  
  analogWrite(PIN_LED_R, ~r);
  analogWrite(PIN_LED_G, ~g);
  analogWrite(PIN_LED_B, ~b);
}

Schematics

Connection of RGB LED with common anode
anode_commune_rgb_uJq02ViGkB.fzz
Connection of RGB LED with common cathode
cathode_commune_rgb_lUFIksgkUo.fzz

Comments

Similar projects you might like

Music Adaptive RGB LED with Arduino and Processing

Project in progress by ounane mohamed elfarouk

  • 2,955 views
  • 8 comments
  • 24 respects

LED Emergency Lights using WS2812 RGB LED Module

Project tutorial by bigboystoys13

  • 10,004 views
  • 5 comments
  • 20 respects

Smooth RGB LED Transitions with Johnny-Five

by Iain

  • 8,410 views
  • 3 comments
  • 28 respects

RGB LED Button Controller

Project showcase by Re

  • 7,947 views
  • 1 comment
  • 12 respects

Multicolor Knight Rider with RGB PL9823 LED + Arduino UNO

Project tutorial by Giovanni Gentile

  • 3,934 views
  • 0 comments
  • 10 respects
Add projectSign up / Login