How to Use an RGB LED

How to Use an RGB LED

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

  • 8,218 views
  • 4 comments
  • 25 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.

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

Smooth RGB LED Transitions with Johnny-Five

by Iain

  • 14,486 views
  • 3 comments
  • 36 respects

LED Emergency Lights using WS2812 RGB LED Module

Project tutorial by bigboystoys13

  • 15,533 views
  • 7 comments
  • 34 respects

RGB LED Button Controller

Project showcase by Re

  • 13,541 views
  • 2 comments
  • 17 respects

Not Your Typical RGB LED

by Alexander Vaughn

  • 3,551 views
  • 1 comment
  • 11 respects

Controlling an RGB LED with Color Detector Camera

Project tutorial by Zowail

  • 2,559 views
  • 0 comments
  • 1 respect

Multicolor Knight Rider with RGB PL9823 LED + Arduino UNO

Project tutorial by Giovanni Gentile

  • 6,840 views
  • 0 comments
  • 16 respects
Add projectSign up / Login