Introduction: Using RGB LED With Arduino

Picture of Using RGB LED With Arduino

Hi guys this is my first instructable and I will be showing you how to control RGB (Red Green and Blue) LEDs. its not that hard to do but the materials you will need are;

-An Arduino Board

-RGB LED

-jumper wires

-breadboard(optional)recommended

-And a computer of course to send code to your Arduino.

Step 1: Step: 1

Picture of Step: 1

First you need connect the LED to the Arduino. The longest pin on the led is ground, second longest is blue, third longest is red then the last pin is green. The Arduino sends an output to a pin which lights the LED.

Step 2: Step: 2

Picture of Step: 2

in this step im going to use the colour code in the picture above. Ok first the orange wire is ground and goes to the Arduino GND pin. The yellow to Digital pin(PWN) 10, Black to 9 and white to 8. Make sure that you have done it in this order because the coding is this way.

-orange wire=Ground

-yellow wire=Green

-Black wire=Blue

-White wire=Red

Step 3: Step:3

Time for the code! this code is fairly simple and can easily be changed.
Now using your computer open up the Arduino software. Then connect your Arduino to your computer. Next copy and paste all the text between the [ ]. Then upload the program to your Arduino and try it out.

There will be 3 different types of code. 1=Fade 2=Blink;

1.[

#define GREEN 10
#define BLUE 9
#define RED 8
#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 );
}
}

]

2.[

int ledcolor = 0;
int a = 1000; //this sets how long the stays one color for
int red = 8; //this sets the red led pin
int green = 10; //this sets the green led pin
int blue = 9; //this sets the blue led pin

void setup() { //this sets the output pins

pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}

void loop() {
int ledcolor = random(7); //this randomly selects a number between 0 and 6

switch (ledcolor) {
case 0: //if ledcolor equals 0 then the led will turn red
analogWrite(red, 204);
delay(a);
analogWrite(red, 0);
break;
case 1: //if ledcolor equals 1 then the led will turn green
digitalWrite(green, HIGH);
delay(a);
digitalWrite(green, LOW);
break;
case 2: //if ledcolor equals 2 then the led will turn blue
digitalWrite(blue, HIGH);
delay(a);
digitalWrite(blue, LOW);
break;
case 3: //if ledcolor equals 3 then the led will turn yellow
analogWrite(red, 160);
digitalWrite(green, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(green, LOW);
break;
case 4: //if ledcolor equals 4 then the led will turn cyan
analogWrite(red, 168);
digitalWrite(blue, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(blue, LOW);
break;
case 5: //if ledcolor equals 5 then the led will turn magenta
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
delay(a);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
break;
case 6: //if ledcolor equals 6 then the led will turn white
analogWrite(red, 100);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
break;
}

}

]

Don't put [] those in the Arduino code other wise it wont work and you shouldn't need resistors because of the Arduino.

If you have any questions, comments or suggestions for other Arduino examples please feel free to leave a comment and I will write back as soon as i can.

Comments