Home LED projects RGB Led example

RGB Led example

by shedboy71

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

 

[codesyntax lang=”cpp”]

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
}

[/codesyntax]

Switch them all on at the same time

[codesyntax lang=”cpp”]

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
}

[/codesyntax]

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

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More