Arduino projects: RGB LED Arduino

Everyone loves LEDs, they are cheap, simple to use and you can easily learn all Arduino tricks with them. But here some an even cooler LED type, this one is different from the ordinary LED we have been using here. It’s called the RGB LED.

What is the RGB LED?

The RGB LED is a single LED that contains 3 LEDs: red, green and blue one.

It can shine in over 16 million colors and is as easy to use as normal LEDs.

We have two main types of RGB LEDs: the common cathode and the common anode.

If you have the common cathode, you should connect the longest leg of the LED to ground (GND) and the rest 3 legs to 3 of the Arduino pins. It shines at HIGH states of the pins.

If you have the common anode you have to connect the longest leg to VCC (5V) and the rest of them to the Arduino pins. It shines at LOW states of the pins.

Do not forget to put a resistor in series with each of the 3 pins between the Arduino and the LEDs pins

Parts needed

Here are two diagrams for connection purposes. The first one is for the common anode (I used it) and the second one is for the common cathode.

Whichever you use, make sure to connect the LED properly. The code does not change.

We need to connect 4 cables and 3 resistors. Here is how it looks like:

Now, we can upload the first program to the Arduino. The following code will light up the red, green and blue LED in a sequence. Not so fancy huh!

void setup() {
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);

}

void loop() {
for(int r = 0; r < 255; r++){
analogWrite(9, r);
delay(10);
}
for(int g = 0; g < 255; g++){
analogWrite(10, g);
delay(10);
}
for(int b = 0; b < 255; b++){
analogWrite(11, b);
delay(10);
}

}

The previous code was used for the sake of showing you how it looks and works.

Now, we can build something more sophisticated.

Here, we uses a function random.

This function calls random([min_value], [max_value]); That make you get a random number within the range, min_value (the smallest value we can get is 0 and max value is 255 because those are the max value we can write to the analogWrite() function).

We have the following 3 values for each color and we randomly generate numbers for those colors.

When current_color_value is equal to random_color_value, we get a new random value.

float now_r = 0, now_g = 0, now_b = 0;
float ran_r, ran_g, ran_b;

void setup() {

  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);

}

void loop() {
  //random value is what we want to achieve
  //if current r value is bigger than random r value we have to decrement the value
  if(now_r > ran_r){
    now_r--;
    //if value is smaller we have to increment the value
  }else if(now_r < ran_r){
    now_r++;
  }else{
    //we bove values are qeual we have to set new random value
    ran_r = random(0, 255);
  }

  if(now_g > ran_g){
    now_g--;
  }else if(now_g < ran_g){
    now_g++;
  }else{
    ran_g = random(0, 255);
  }

  if(now_b > ran_b){
    now_b--;
  }else if(now_b < ran_b){
    now_b++;
  }else{
    ran_b = random(0, 255);
  }

  //here we are writing those values to pins with analogwrite because we use PWM
  analogWrite(11, now_r);
  analogWrite(10,now_g);
  analogWrite(9, now_b);
  //0.01 second of delay between each iteration, you can change it to make it faster or slower
  delay(10);
}

The LED itself is just really bright and we are not able to notice it is color changing. We should somehow diffuse its color to make it look better.

You most probably have this type of foam somewhere in your house, it usually come in boxes to protect their content when shipped. It is just perfect for this. Just make a small hole for the LED to go in and we are set to go.

I hope you have just built your first RGB LED Arduino project. Just take a look on how cool this is.

 

tchouken2

Related Posts