Categories

Arduino and multi-color LED example

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

RGB LED

RGB LED

 

 

Code

Cycle through the LED colours

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

Share

Comments are closed.