Home LearningBasics Arduino and multi-color LED example

Arduino and multi-color LED example

by shedboy71

An RGB led is a combination of a Red, Green and Blue LED's in one package, they can be common cathode or common anode types

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

rgb led

rgb led

Here is a rough schematic, this is a common anode type. This means that to switch on the LED you would make the arduino pin output go low and to switch off the LED you would make the arduino pin output go high.

A common cathode type is the opposite

 

RGB LED

RGB LED

 

 

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

Share

You may also like