## Introduction: Arduino RGB LED

While "normal leds" are fun, RGB ones are even funnier, imagine using that one led emitting purple color, orange color and many more, wouldn't that be cool? In this tutorial we are going to look into the process of connecting a RGB led. Even though it might seem hard, it is not, believe me. Let's begin.

## Step 1: Parts

I will be using these parts (also shown in picture):

Arduino UNO (about 22\$ or a clone for cheaper)

RGB led (got mine in a starter kit, but about 5, 60 \$ for 100 pcs here)

A couple of jumper wires (got mine for 3\$)

A mounting plate (optional but a couple of dollars)

And finally a breadboard, I am using a half sized one which fits on the mounting plate (about 2\$)

## Step 2: Common Cathode or Common Anode?

Common cathode and anode

The RGB Led has four pins, one for red, one for green, one for blue and then one which goes to ground or 5V depending on if you are using a common cathode or anode led.

How do you find out whether the led is a common cathode or anode?

Basic answer, there is no real answer. You will have to ask the manufacturer or the one you are buying from, they are the only ones who really know for sure. At least I have not found any way to figure this out. EDIT: (From comment) To find out what you've got, use a 5V source with a resistor of 150 to 220 Ohm in series. You can then safely check each color and find out its polarity (CA (common anode or CC (common cathode)), by simply testing each pin.

Connecting each one

There is a difference when connecting both types of leds. When you are connecting the common cathode one, do connect the longest leg, to ground and when connecting a common anode one, connect the longest leg to +5V. More about connecting the ones in the next step.

So what is the difference?

The difference is that with a common cathode, you supply 5V from your arduino output pins to light up the leds and with the common anode LEDs, the anode(longest leg) goes to +5V and the cathodes are pulled to ground (through current limiting resistors)

I am pretty new to these so please tell me if I am wrong in the comments and I will correct it. Thanks! Now let's go more into detail in connecting. Follow along :)

## Step 3: Connections

As you see in the picture below, only a few of the pins on the arduino are PWM (Pulse Width Modulation). We will use three of these to our advantage to create different square waves which will the determine how bright each value should be.

Connect the RGB led according to the pictures or as below, remember to put 220 Ohm resitors through the Red, green respectively blue values:

GND -> Arduino GND (Arduino 5V if you are using a common anode one)

Red RGB pin (the one pin left of the ground pin) -> Pin 11

Blue RGB pin ( the one closest to the ground pin which is not the red one) -> Pin 10

Green RGB pin (the last one) -> Pin 9

Remember that you can connect this however you want but do remember to choose a PWM pin (pins 3,5,6,9,10,11) and to change this accordingly in the code. Next up, code.

## Step 4: The Code

I have commented most of the code for you to understand. Remember if you are using a common anode led, uncomment the line higher up in the code (as comments state). And please, feel free to modify the code however you want and do share your creations in the comments selection. Enjoy!

```//Program by Samuel. Enjoy!//If you are using a common anode RGB led, remove the slashes at the next line (the comment)
//#define COMMON_ANODE//Defining pinouts
int redPin = 11; //The pin that red is connected through
int bluePin = 10; //The pin that blue is connected through
int greenPin = 9; //The pin that green is connected throughvoid setup() {  //Setting these pins as outputs
pinMode(redPin, OUTPUT); //Setting red as output
pinMode(greenPin, OUTPUT); //Setting green as output
pinMode(bluePin, OUTPUT); //Setting blue as output}void loop() {  setColor(255, 0, 0); //Red color
delay(1000);
setColor(0, 255, 0); //Green color
delay(1000);
setColor(0, 0, 255); //Blue color
delay(1000);}void setColor(int redValue, int greenValue, int blueValue) { //Defining function setColor, taking three arguments#ifdef COMMON_ANODE //Checking if this is defined
red = 255 - redValue;
green = 255 - greenValue;
blue = 255 - blueValue;
#endif //Ending check  analogWrite(redPin, redValue); //Setting value red
analogWrite(greenPin, greenValue); //Setting value green
analogWrite(bluePin, blueValue); //Setting value blue}```

## Step 5: Finished

There we go, hope this tutorial helped you and learned you something new. If there is anything wrong please tell me, either by the comments or by a private message. Have a great day!

Samuel