Color the lights using RGB LED

Updated: May 5, 2020




Know it

RGB LED is simply 3 LEDs in 1 LED. The 3 LEDs are Red, Green, Blue. Since these three colors are the additive primary color, you can control each brightness of each color individually to get any color that you want!


There are two kinds of RGB LEDs.

  1. Common pin for 5V (Common Anode)

  2. Common pin for GND (Common Cathode) Which is the one you can find in your Voltaat Ultimate Arduino kit!



Wire it

If you know how to connect one LED to the Arduino, then you can easily do this circuit! Just do the same circuit, but three times like the bellow image. For that you need these times:

  • Breadboard and Jumper wires

  • 3x 220Ω Resistors

  • RGB LED (Common Cathode)

  • Arduino board and USB connector



















Connections:

  • Arduino pin GND LED longest leg

  • Arduino pin 11 Resistor (220Ω) LED Red pin

  • Arduino pin 10 Resistor (220Ω) LED Green pin

  • Arduino pin 9 Resistor (220Ω) LED Blue pin


Code it

This is a simple example sketch that will change the color of the RGB LED after every second. Changing the values of Red, Green And Blue will produce different color lights.


Try it out! Copy the code below:

/*   
Changing colors using RGB LEDs

Tutorial link: https://www.learn.voltaat.com/post/rgb-blink-colors

This is a simple example sketch that will change the color of the RGB LED after every second.
  
Changing the values in analogWrite will make different colors lights.
 
Use the color wheel to know the values that can make the color that you want.
  
Components Needed:
  1. RGB Led Common Cathode 5mm ...x1
  2. 220ohm Resistor ...x3

Connections:
   - Arduino pin GND → LED longest leg
   - Arduino pin 11 → Resistor (220Ω) → LED Red pin
   - Arduino pin 10 → Resistor (220Ω) → LED Green pin 
   - Arduino pin 9 → Resistor (220Ω) → LED Blue pin
*/

int redPin= 11; // Red pin is connected to 11
int greenPin= 10; // green pin is connected to 10
int bluePin= 9; // blue pin LED is connected to 9

// This routine runs only once upon reset
void setup()
{
  pinMode(redPin, OUTPUT); // Initialize 11 pin as output
  pinMode(greenPin, OUTPUT); // Initialize 10 pin as output
  pinMode(bluePin, OUTPUT); // Initialize 9 pin as output
}

// This routine loops forever
void loop()
{
 //show the color on the LED as Below
 analogWrite(redPin, 255);  
 analogWrite(greenPin, 0); 
 analogWrite(bluePin, 0);  
 
 delay(1000); // delay of 1 sec
  
 //Change LED color as Below
 analogWrite(redPin, 255);   
 analogWrite(greenPin, 255);    
 analogWrite(bluePin, 0);  
 
 delay(1000);// delay of 1 sec       

 //Change LED color as Below
 analogWrite(redPin, 255);   
 analogWrite(greenPin, 0);    
 analogWrite(bluePin, 255);  
  
 delay(1000);// delay of 1 sec 
  
 //Change LED color as Below
 analogWrite(redPin, 0);   
 analogWrite(greenPin, 255);    
 analogWrite(bluePin, 255);  
 
 delay(1000);// delay of 1 sec  
}


Learn

Arduino Starter kit

Contact us

Doha, Qatar

Company

© 2020, Voltaat Store for Trading