RGB Led example

A common anode RGB LED is three one colour LEDs (one red, one green, and one blue) housed in a single package. A common anode RGB LED is the most popular type but you can also get common cathode types as well, our example is based on one of these.

In the example I used an RGB LED breakout. The LED was wired up as follows

5v – 5v
R – Pin 4
G – Pin 5
B – Pin 6

Here is a picture of the RGB LED breakout that I purchased

rgb led module

rgb led module

Here is a schematic

RGB LED schematic

RGB LED schematic

Code

Cycle through the LED colours

 

int red = 4;
int green = 5;
int blue = 6;
 
// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(red, OUTPUT);
}
 
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(red, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(red, HIGH); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
digitalWrite(green, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(green, HIGH); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
digitalWrite(blue, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(blue, HIGH); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

Switch them all on at the same time

int red = 4;
int green = 5;
int blue = 6;
 
// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}
 
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(red, LOW); // turn the LED on (LOW is the voltage level)
digitalWrite(green, LOW); // turn the LED on (LOW is the voltage level) // wait for a second
digitalWrite(blue, LOW); // turn the LED on (LOW is the voltage level)
delay(1000); // wait for a second
digitalWrite(red, HIGH); // turn the LED off by making the voltage HIGH
digitalWrite(green, HIGH); // turn the LED off by making the voltage HIGH
digitalWrite(blue, HIGH); // turn the LED off by making the voltage HIGH
delay(1000); // wait for a second
}

Links

Here are links to the sensor kit

50 Pcs 5mm Diameter Frosted Head 4 Pins RGB Light LED Diodes – Amazon UK
50 Pcs 5mm Round Head Common Cathode RGB Light LED Emitting Diodes – Amazon US

Related

The PCA9685 is an I²C-bus controlled 16-channel LED controller optimized for Red/Green/Blue/Amber (RGBA)...