Home Chapter 8 Maxuino/Arduino RGB leds

Site Search

GTranslate

Chinese (Simplified) French German Italian Portuguese Russian Spanish
Maxuino/Arduino RGB leds

 

"If you want to find some secrets of the universe think in terms of energy, frequency, and vibration" Nicola Tesla

 

RGB LEDs are exciting devices where you can mix red, green and blue light in a single package to create an infinite number of colors. They are quite beautiful and can add some real pizzaz to a work.

Building the circuit

Parts

Arduino UNO
RGB LED and
3 x 220 Ohm resistors

©2014 Arduino

Using the Arduino IDE to control an RGB LED.

 

#define GREEN 3
#define BLUE 5
#define RED 6
#define delayTime 20

void setup() {

pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(RED, OUTPUT);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
digitalWrite(RED, HIGH);
}

int redVal;
int blueVal;
int greenVal;

void loop() {

int redVal = 255;
int blueVal = 0;
int greenVal = 0;
for( int i = 0 ; i < 255 ; i += 1 ){
greenVal += 1;
redVal -= 1;
analogWrite( GREEN, 255 - greenVal );
analogWrite( RED, 255 - redVal );

delay( delayTime );
}

redVal = 0;
blueVal = 0;
greenVal = 255;
for( int i = 0 ; i < 255 ; i += 1 ){
blueVal += 1;
greenVal -= 1;
analogWrite( BLUE, 255 - blueVal );
analogWrite( GREEN, 255 - greenVal );

delay( delayTime );
}

redVal = 0;
blueVal = 255;
greenVal = 0;
for( int i = 0 ; i < 255 ; i += 1 ){
redVal += 1;
blueVal -= 1;
analogWrite( RED, 255 - redVal );
analogWrite( BLUE, 255 - blueVal );

delay( delayTime );
}
}


Notice that the v+ line for the RGB led should be indicated on the package. Also, notice that the PWM pins can be the only ones used for the patch below.

 

©2014 Ken Rinaldo

As you wire up your RGB LEDs keep in mind that some may require a common ground vs a common V+ rail to function