RGB LED INTERFACING WITH ARDUINO

Photo of author

By Jackson Taylor

Home » Electronics Topics » Arduino » RGB LED INTERFACING WITH ARDUINO

As an Amazon Associate we earn from qualifying purchases.

An RGB LED packs a red, a green and a blue LED into one housing. By changing how bright each color is, you can mix almost any color, which makes it a good first project for learning PWM (pulse width modulation) on an Arduino. This guide covers how the LED works, how to wire it, and a sketch that fades smoothly through many color blends.

The video below shows the project running.

What you need

If you have never driven a plain LED from an Arduino, it helps to first learn how to control a single LED with Arduino. The RGB version is the same idea repeated three times.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

How an RGB LED works

A 4-pin RGB LED has one pin for each color and one shared pin. The shared pin decides which type you have:

  • Common cathode: the shared pin goes to GND. A HIGH signal or a higher PWM value makes that color brighter.
  • Common anode: the shared pin goes to 5 V. The logic is reversed, so a lower PWM value makes the color brighter.

The longest leg is normally the shared pin, but confirm this with the datasheet or by testing, since part markings vary. The sketch in this article assumes a common cathode LED.

Wiring the RGB LED to the Arduino

  1. Place the LED on the breadboard so each leg is in its own row.
  2. Connect a resistor in series with each of the red, green and blue legs.
  3. Connect the other end of each resistor to an Arduino output pin. The sketch below uses pins 10, 11 and 12 for red, green and blue.
  4. Connect the shared (longest) leg to GND for a common cathode LED.
See also
ACCELEROMETER BASED WHEEL CHAIR USING ARDUINO AND H BRIDGE USING RELAY

Never skip the resistors. Without them, each color channel can draw more current than the LED or the Arduino pin is meant to handle.

A note on PWM pins

The sketch uses analogWrite(), which only produces true PWM on pins marked with a tilde (~). On an Arduino Uno these are pins 3, 5, 6, 9, 10 and 11. Pin 12 is not a PWM pin on the Uno, so the blue channel would only switch fully on or off. If you are using an Uno, move the blue wire to pin 9 and change B=12 to B=9 in the code. On boards with PWM on more pins, such as the Mega, pin 12 works as written.

Arduino sketch: fading through colors

This sketch ramps one or two color channels up and down while holding the others at fixed levels, so the LED moves through many different blends.


int R=10,G=11,B=12;
int r=0,g=0,b=0;
void setup()
{
  pinMode(R, OUTPUT);   pinMode(G, OUTPUT); pinMode(B, OUTPUT);
}
void loop()
{
  for(r=0;r<255;r++)
  {
  analogWrite(R, r); analogWrite(G, 30);   analogWrite(B, 112);   
  delay1();
  }
   for(r=0;r<255;r++)
  {
  analogWrite(R, 0); analogWrite(G, r);   analogWrite(B, 112);   
  delay1();
  }
   for(r=0;r<255;r++)
  {
    analogWrite(R, 100); analogWrite(G, 0);   analogWrite(B,r);   
  delay1();
  }
  for(r=0;r<255;r++)
  {
    analogWrite(R, r); analogWrite(G, r);   analogWrite(B, 0);   
  delay1();
  }
  for(r=0;r<255;r++)
  {
    analogWrite(R, r); analogWrite(G,100);   analogWrite(B,r);   
  delay1();
  }
   for(r=255;r>0;r--)
  {
    analogWrite(R, r); analogWrite(G, r);   analogWrite(B, 0);   
  delay1();
  }
  for(r=255;r>0;r--)
  {
    analogWrite(R, r); analogWrite(G,100);   analogWrite(B,r);   
  delay1();
  }
  for(r=255;r>0;r--)
  {
    analogWrite(R, r); analogWrite(G, 30);   analogWrite(B, 112);   
  delay1();
  }
   for(r=255;r>0;r--)
  {
    analogWrite(R, 0); analogWrite(G, r);   analogWrite(B, 112);   
  delay1();
  }
   for(r=255;r>0;r--)
  {
    analogWrite(R, 100); analogWrite(G, 0);   analogWrite(B,r);   
  delay1();
  }
  for(r=255;r>0;r--)
  {
    analogWrite(R, r); analogWrite(G, r);   analogWrite(B, 0);   
  delay1();
  }
  for(r=255;r>0;r--)
  {
    analogWrite(R, r); analogWrite(G,100);   analogWrite(B,r);   
  delay1();
  }
}
void delay1()
{
   delay(10);
}

How the code works

  • Pin variables: R, G and B store the pin numbers for each color. The variable r is a counter that is reused in every loop.
  • setup(): all three pins are set as outputs.
  • analogWrite(pin, value): sets the brightness from 0 (off) to 255 (full). Values in between change the PWM duty cycle, which the eye sees as dimmer or brighter light.
  • The for loops: each loop moves one or two channels step by step while the others stay fixed. The first half counts up and the second half counts down, so the color fades in and out rather than jumping.
  • delay1(): a small helper that waits 10 ms per step. Each loop takes roughly 2.5 seconds. Raise the value for slower fades or lower it for faster ones.
See also
BIPOLAR STEPPER MOTOR INTERFACING WITH PIC

A simpler way to set any color

Once the wiring works, a helper function makes it easier to pick exact colors. This version uses pins 9, 10 and 11, which are all PWM pins on an Uno.


const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;

void setColor(int red, int green, int blue)
{
  analogWrite(RED_PIN, red);
  analogWrite(GREEN_PIN, green);
  analogWrite(BLUE_PIN, blue);
}

void setup()
{
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

void loop()
{
  setColor(255, 0, 0);   // red
  delay(1000);
  setColor(0, 255, 0);   // green
  delay(1000);
  setColor(0, 0, 255);   // blue
  delay(1000);
  setColor(255, 255, 0); // yellow
  delay(1000);
}

For a common anode LED, connect the shared pin to 5 V and pass 255 – value to each analogWrite() call.

Troubleshooting

  • Colors look wrong or inverted: you probably have a common anode LED wired as if it were common cathode.
  • One color never lights: check that the resistor and jumper for that leg are in the correct breadboard row, and that you identified the legs correctly.
  • One color only flips on and off: that channel is on a pin without PWM. Move it to a pin marked with a tilde (~).
  • Colors look uneven: the three LEDs inside have different brightness and forward voltage. Adjusting the resistor values or the PWM numbers can help balance white.

Where to go next

After you have the basics working, try reading a potentiometer to set the color, or add a button to switch modes. For more examples, see our guide on how to control RGB LED lights with Arduino, the related post on RGB LED control with Arduino, and a larger project on smart lighting with RGB LEDs.

FAQ

Do I need a resistor for each color?

Yes. Each color is a separate LED inside the package, and each needs its own current-limiting resistor to protect both the LED and the Arduino pin.

How do I know if my RGB LED is common anode or common cathode?

Check the datasheet or the seller’s listing. You can also test it: connect the longest leg to GND through a resistor-protected setup and see if the colors light when you drive a color pin HIGH. If nothing lights, try the longest leg on 5 V instead.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works
See also
ARDUINO BASED BOOST CONVERTER SIMULATION IN PROTEUS

Why does analogWrite() not fade my LED on some pins?

Only PWM-capable pins support fading. On an Uno, that means pins 3, 5, 6, 9, 10 and 11. On other pins, analogWrite() just switches the output on or off.

How many colors can I make?

Each channel has 256 brightness levels (0 to 255), so the three channels together give about 16.7 million combinations. In practice, you will see fewer distinct colors because the eye cannot tell small steps apart.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works