MisterBotBreak
Published

How to Use an RGB LED

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

BeginnerProtip1 hour21,619
How to Use an RGB LED

Things used in this project

Hardware components

RGB Diffused Common Cathode
RGB Diffused Common Cathode
×1
Arduino UNO
Arduino UNO
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 221 ohm
Resistor 221 ohm
×1
Breadboard (generic)
Breadboard (generic)
×1
RGB Diffused Common Anode
RGB Diffused Common Anode
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Connection of RGB LED with common anode

Connection of RGB LED with common cathode

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);
}

Credits

MisterBotBreak
48 projects • 152 followers
I love electronics and cats :D !
Contact

Comments

Please log in or sign up to comment.