Earn Maker Points on Every Order! Learn More!

RGB LED Tutorial | Arduino

Back to Blog
RGB LED Tutorial | Arduino - RGB LED Tutorial | Arduino - RGB LED Tutorial | Arduino - RGB LED Tutorial | Arduino

RGB LED Tutorial | Arduino

An RGB LED is an integrated LED with all three primary colors in it, RED, GREEN and BLUE. Using an RGB LED we can make all other colors by mixing a proper amount of these three colors. The amount of voltage applied to each pin specifies the amount of colors it emits. This is called additive mixing.

There are two types of mixing we are using in the real world. They are

  • Additive Mixing (RGB)
  • Subtractive Mixing (CMYK)

Additive Mixing:

Additive Mixing sums up the amount of color from each RGB color to make secondary colors. Eg. Red+Green = Yellow. In additive mixing the Red, Green and Blue are the Primary Colors. This mixing method is mostly used in TV Monitors, CRT, LCD and other video displays.

AdditiveColor

Subtractive Mixing:

Subtractive Mixing is most widely used in Color Printers, Painting, etc. The Cyan, Magenta, Yellow are the Primary colors to make the secondary colors.  It subtracts two colors to get a secondary color. Eg. Yellow-Magenta = Red.

If you notice that in printers you can able to see an extra Black Color which is a key color. It is actually to save the ink, instead of combining all three colors (CMY) to make black color, we can simply use the single black ink.

CMYK_subtractive_color_mixing

 

The RGB LED works on the principle of additive mixing. Do not confuse them with common anode and cathode variations. These are just for circuit and project requirements.

Circuit Diagram:

The circuit diagram for this example is given below. As we are using a common anode LED, we connected the anode terminal to the 5V Terminal via a 220 Ohm Resistor.

To write a specific voltage to the LED pins, we need to use the PWM Signals. Depending on the duty cycles the voltage at the output varies. So we used 3,5 and 6th pin of Arduino UNO that are PWM pins.

Red Pin = Pin 3, Green Pin = Pin 5, Blue Pin = 6.

RGBLED_Arduino_bb

Code:

The below code lights each color LED one by one for 1 second continuously. We have created a custom function called writeColor to write the PWM Values for each pin. 255 is the maximum value here. If you reduce or modify it the resultant color will light up. As it is a common Anode LED the value 0 lights up the LED.

Example: To light the green LED we should write

Red = 255;

Green = 0;

Blue = 255;

You can modify these values and see the result on the LED.

/*This example uses Common Anode LED*/
/*
* Author: Sharath 
* Website: http://www.factoryforward.com
*
* For Common Anode LED(The common pin connected to +ve).
* Supply a low value signal to a color pin glows that LED color.
* Eg. Red = 0(Low), Green = 255(High), Blue = 255(High) = Red LED
* 
* For Common cathode, connect the common pin to -ve.
* Supply a High value to a color pin glows that LED Color.
* Eg. Red = 255(High), Green = 0(Low), Blue = 0(Low) = Red LED
*/
int redPin= 11; //PWM Pins
int greenPin = 10; //PWM Pins
int bluePin = 9; //PWM Pins
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
}
void loop() {
writeColor(0, 255, 255); // Red
Serial.println("Red");
delay(1000);
writeColor(255, 0, 255); // Green
Serial.println("Green");
delay(1000);
writeColor(255, 255, 0); // Blue
Serial.println("Blue");
delay(1000);
}

//Custom function to write color values to LED

void writeColor(int redVal, int greenVal, int blueVal)
{
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
}

 

Share this post

Leave a Reply

Back to Blog