keepcalculator

06 Numbers and Powers

Arduino RGB LED Wiring and Color Mixing Guide

Card No. 06·15492 · Updated · Standard formula, shown below

Control a red-green-blue (RGB) LED with an Arduino by wiring its three color channels to separate output pins, then adjusting their brightness in code. You’ll learn how to identify common-anode and common-cathode LEDs, protect each channel with a resistor, and mix red, green, and blue light into other colors.

What You’ll Need

  • An Arduino-compatible board and USB cable
  • A 4-pin RGB LED (check its datasheet or packaging for common-anode or common-cathode type)
  • Three 220–330 Ω resistors, one per color channel
  • Breadboard and jumper wires
  • A computer with the Arduino IDE or another compatible editor

Use a resistor for each color channel; one resistor shared by the LED’s common leg will not limit each channel reliably when colors are mixed. A 220–330 Ω resistor is a practical starting range for many 5 V Arduino setups, but confirm the LED’s forward-current and voltage ratings and your board’s pin limits. Never connect an LED directly between a GPIO pin and a supply without current limiting.

Step-by-Step

  1. Identify the LED type and pins. A 4-pin RGB LED has one common pin and separate red, green, and blue pins. The common pin is often the longest, but this is not guaranteed. Use the datasheet or a multimeter’s diode-test function to confirm the pinout and whether it is common-anode or common-cathode. Do not rely on leg length alone.
  2. Choose three PWM-capable pins. For many Arduino Uno-style boards, pins 3, 5, 6, 9, 10, and 11 support PWM output. Select three of these, such as 9, 10, and 11. Other boards may use different pins or PWM behavior, so check the board’s pinout. PWM lets the board simulate brightness by rapidly switching a pin on and off.
  3. Wire a common-cathode LED. Insert the LED into the breadboard so its legs are in separate rows. Connect the common-cathode leg to GND. Connect each color leg to its own resistor, then connect the other end of the red, green, and blue resistors to pins 9, 10, and 11, respectively. The resistor may be placed on either side of its LED channel, provided it is in series.
  4. Wire a common-anode LED instead, if that is your type. Connect the common-anode leg to the board’s 5 V supply. Connect each separate color leg through its own resistor to a PWM pin. This type is active-low: a pin value of 0 gives maximum channel brightness, and 255 turns that channel off. Do not connect the common pin to 5 V if the LED is common-cathode.
  5. Check the circuit before powering it. Verify the common leg, color pinout, three separate resistors, and ground or 5 V connection. Ensure there are no loose wires or shorts between supply and ground. Disconnect USB power while changing the wiring.
  6. Upload a test sketch. In the Arduino IDE, select the correct board and port, then upload this example. It is written for common-cathode wiring; change COMMON_ANODE to true for a common-anode LED.
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
const bool COMMON_ANODE = false;

void setColor(int red, int green, int blue) {
  if (COMMON_ANODE) {
    red = 255 - red;
    green = 255 - green;
    blue = 255 - blue;
  }
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, 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, 80, 0);    // orange-like
  delay(1000);
  setColor(0, 0, 0);       // off
  delay(1000);
}
  1. Try color mixing. The three numbers passed to setColor(red, green, blue) range from 0 to 255. A value of 0 means that channel is off; 255 means full commanded brightness for common-cathode wiring. Red plus green can look yellow, red plus blue magenta, and green plus blue cyan. All three at high values appear white or near-white, depending on the LED.
  2. Adjust by eye. Change one channel at a time and upload again, or use a serial input or sensor later. RGB values are not a guarantee of a particular perceived color: LED components differ in brightness, and human vision does not perceive each channel equally. PWM values control duty cycle, not a calibrated color standard.

Common Mistakes

  • Using the wrong common type: Common-anode and common-cathode circuits have opposite polarity. Confirm the part and set the sketch’s flag correctly.
  • Skipping channel resistors: Use one suitable series resistor for red, one for green, and one for blue. Without them, excessive current may damage the LED or board.
  • Choosing non-PWM pins: On an Uno-style board, analogWrite() brightness control works on PWM-marked pins, not every digital pin. A non-PWM pin may behave only as on or off.
  • Assuming the pin order: RGB LED leg arrangements vary. If colors appear swapped, verify the datasheet and change the pin assignments in the sketch.
  • Forgetting the common connection: A missing ground on common-cathode wiring or missing supply on common-anode wiring leaves the circuit without a complete current path.

Tips

Start with one channel at a time, then combine them. If you want softer colors, reduce values rather than changing the wiring. For example, setColor(40, 0, 120) mixes dim red with stronger blue for a purple shade. Keep channel values within 0–255; values outside that range are not valid brightness settings.

For repeatable results, write down combinations that look good in your room and with your particular LED. Ambient light, diffusers, viewing angle, and the LED’s component balance all affect appearance. If the LED is too bright for an indicator, lower the PWM values; do not remove the resistors.

Troubleshooting

  • LED stays dark: Check USB power, the selected board and port, the common-pin connection, and whether the LED is reversed. Confirm that the code uses the same pins as the wiring. Test one channel with full brightness.
  • Only one color works: Check the other two LED legs, resistors, jumper wires, and pin numbers. A mistaken pinout or a damaged channel can cause this. Swap the channel’s wire to a known working PWM pin to help isolate the issue.
  • Colors are inverted or always on: Confirm whether the LED is common-anode. Set COMMON_ANODE to true for that type; the helper function reverses the PWM values.
  • Colors do not match the comments: Red, green, and blue legs may be in a different order than assumed. Confirm the pinout and update redPin, greenPin, and bluePin.
  • LED flickers or the board resets: Look for loose connections and shorts, and make sure each channel has its own resistor. Avoid drawing more current than the board’s pins or supply can provide; consult the board and LED specifications before using a different supply or adding more LEDs.

Frequently Asked Questions

Can I use any digital pin?
For adjustable brightness with analogWrite(), use PWM-capable pins supported by your board. A regular digital pin can generally switch a channel on or off, but not provide the same PWM brightness control.

Why does common-anode need reversed values?
Its common leg is connected to the positive supply. Pulling a color pin low allows current to flow, so low output means bright and high output means off.

Can I use one resistor for all three colors?
No. Use a separate current-limiting resistor in series with each color channel so current is limited appropriately as different combinations light up.

How do I make white?
Start with all three channels on, such as setColor(255, 255, 255) for common-cathode wiring, then adjust the values to balance the result. The white may not look neutral because RGB LEDs vary.

Will this work with a 3.3 V board?
The wiring concept is similar, but check the board’s output voltage and pin-current limits and the LED’s specifications. Choose resistors suitable for that circuit; do not assume values or connections from a 5 V board apply unchanged.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →