# BITE SIZE ARDUINO – RGB LED

A RGB LED is a LED that can change the colour of the light it produces depending on which of the LEDs’ Connectors have current flowing through them. The LED has 4 connectors, one connector for red, one for green, one for blue and then finally an anode or a cathode, depending if the RGB is a common anode or cathode LED.

So what is the difference between common anode and common cathode?

Well a RGB LED is actually a combination of 3 LEDs, a red LED, a green LED and a blue LED. All LEDs have 2 connectors, an anode and a cathode. So depending how these LEDs are connected together determines if they share an anode or a cathode, thus common anode RGB LED or common cathode RGB LED. The Anode\Cathode leg can be identified as it is the longest leg on the LED. Below are 2 diagrams that illustrates the difference discussed.

Common Cathode:

Common Anode:

How these 2 different RGB LEDs are connected to a circuit also differs, let us first have a look at a circuit that contains a common cathode RGB LED:

Here is the code used with this circuit:

```int redPin = 9;
int greenPin = 10;
int bluePin = 11;

void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}

void loop()
{
setLEDColour(255, 0, 0);  // red
delay(2000);
setLEDColour(0, 255, 0);  // green
delay(2000);
setLEDColour(0, 0, 255);  // blue
delay(2000);
setLEDColour(255, 255, 0);  // yellow
delay(2000);
setLEDColour(80, 0, 80);  // purple
delay(2000);
}

void setLEDColour(int red, int green, int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
```

Now let us have a look at a circuit that contains a common anode RGB LED:

Code used with this circuit:

```int redPin = 11;
int greenPin = 10;
int bluePin = 9;

void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}

void loop()
{
setLEDColour(255, 0, 0);  // red
delay(2000);
setLEDColour(0, 255, 0);  // green
delay(2000);
setLEDColour(0, 0, 255);  // blue
delay(2000);
setLEDColour(255, 255, 0);  // yellow
delay(2000);
setLEDColour(80, 0, 80);  // purple
delay(2000);
}

void setLEDColour(int red, int green, int blue)
{
red = 255 - red;
green = 255 - green;
blue = 255 - blue;

analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
```

Although the circuits and code differ between the 2 types of RGB LEDs, the end results are exactly the same.

Try changing the values passed into the setLEDColour function to see what different colours can be created.

## One thought on “BITE SIZE ARDUINO – RGB LED”

1. Hmm. Just put one of these together with a potentiometer yesterday, but with only 3 colors. Looks like I’ll have to go back and add some more colors before the weekends over. (Just starting to learn this stuff)

Liked by 1 person