RGB Example

RGB Example

The RGB is a Led that can produce three different light signals: Red(R),Green(G), Blue(B). 
It is formed from four pins and it can have two different configurations: common cathode and common anode. 
RGB 
In this example is used a RGB common Cathode and it will realize a sketch to switch on the led Red,Green and Blue alternately.

Hardware Required

  • Arduino Board
  • common cathode RGB
  • Wires
  • Breadboard
  • 3 resistors of 220 Ω

Circuit

RGB example circuit 
Connect the first pin (R) from flat side to a resistor of 220 Ω and to Pin 2 of the board, connect the second (cathode), 
usually is longer than others pins, to ground (GND) of the board, connect the third pin (G) to a resistor of 220 Ω and to Pin 3 of 
the board and finally connect the fourth pin (B) of RGB to a resistor of 220 Ω and to Pin 4 of the board.

Schematic

RGB Example schematic

Code

The code defines with red, green, blue the RGB pins. 
It is created a function called Led_on to switch on the singles Leds. 
In the setup red, blue and green are defined as OUTPUT pins. 
In the loop is called the function Led_on and every second is changed the parameter supplied to the function, so it changes the led on. 
NOTE: 
When you use a RGB common cathode if you want to switch on the led you must write HIGH, if you want to switch off you must write LOW, 
instead when you use a RGB common anode you must write in opposite mode.

 
 
/*RGB Common Cathode */
const int red=2;
const int green=3;
const int blue=4;
 
void Led_on(int n){
switch(n){
case 0: digitalWrite(red, LOW);//rgb off
            digitalWrite(blue, LOW);
            digitalWrite(green, LOW);
        break;
case 1: digitalWrite(red, HIGH);//red on
            digitalWrite(blue, LOW);
            digitalWrite(green, LOW);
        break;
case 2: digitalWrite(red, LOW);//green on
              digitalWrite(blue, LOW);
              digitalWrite(green, HIGH);
        break;
case 3: digitalWrite(red, LOW);//blue on
            digitalWrite(blue, HIGH);
            digitalWrite(green, LOW);
        break;
}
}
void setup() {
pinMode(red, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
}
void loop(){
for(int i=0;i<=3; i++){
Led_on(i);
delay(1000);}
}