5mm 4-Pin RGB LED (Common Anode) Tutorial: Pinout, Resistor Sizing, Arduino/ESP32 Wiring, PWM Color Mixing, and Practical Projects

Beginner Tutorial Views: 653

This tutorial is a detailed, practical guide to using the 5mm 4-Pin RGB Tri-color LED (Common Anode) (Leobot Product #121) for color indicators, status lights, mood lighting, and DIY UI feedback. You’ll learn the exact wiring logic for common anode (it’s inverted compared to common cathode), how to choose current-limiting resistors safely, how to drive it from Arduino/ESP32 using PWM to mix colors, and when you must use transistor/MOSFET drivers for higher brightness or multiple LEDs.

5mm 4-Pin RGB LED (Common Anode) Tutorial: Pinout, Resistor Sizing, Arduino/ESP32 Wiring, PWM Color Mixing, and Practical Projects

This tutorial is a detailed, practical guide to using the 5mm 4-Pin RGB Tri-color LED (Common Anode) (Leobot Product #121) for color indicators, status lights, mood lighting, and DIY UI feedback. You’ll learn the exact wiring logic for common anode (it’s inverted compared to common cathode), how to choose current-limiting resistors safely, how to drive it from Arduino/ESP32 using PWM to mix colors, and when you must use transistor/MOSFET drivers for higher brightness or multiple LEDs.

Tutorial Beginner–Intermediate LEDs RGB Common Anode Arduino ESP32 PWM Electronics Basics Projects
What this component does: A single 5mm LED package containing three LEDs (Red, Green, Blue). By controlling the brightness of each channel, you can mix thousands of colors.
Critical wiring rule: This is common anode. The shared pin goes to +V, and you drive each color by pulling its pin LOW through a resistor (current sinks into the MCU/transistor). So PWM logic is “inverted” compared to common-cathode RGB LEDs.

1) What “common anode” means (and why it changes your code)

In a common anode RGB LED, all three LED anodes are tied together internally and brought out to one pin. That common pin connects to +V (usually 5V or 3.3V). Each color channel has its own cathode pin (R, G, B). To light a channel, you pull that channel pin toward GND through a resistor.

Common anode logic

  • Channel ON: output LOW (sink current)
  • Channel OFF: output HIGH (no current)
  • Brightness control: use PWM, but inverted (0 = full on, 255 = off on Arduino PWM)
Why common anode exists: Many circuits prefer “sinking” current (pulling to GND) because some MCUs and drivers handle sinking better than sourcing.

2) Pinout identification (finding the common pin and R/G/B pins)

RGB 5mm LEDs often have a similar physical layout, but don’t assume the pin order without checking. Here are reliable ways to identify pins:

Method A: Identify the longest lead

  • On many common-anode RGB LEDs, the longest leg is the common anode (+).
  • Not guaranteed across all batches, so confirm with a multimeter if possible.

Method B: Use a multimeter diode-test mode

  • Put the multimeter in diode-test mode.
  • For common anode: place the red probe on the common pin, and touch the black probe to other pins. You should see different colors glow faintly (depending on your meter).

Method C: Quick test with a resistor and supply

  • Connect suspected common pin to +V.
  • Touch each other pin to GND through a resistor (e.g., 330O–1kO) and see which color lights.
Never connect LED pins directly to VCC/GND without resistors: each color channel needs its own current limiting resistor.

3) Resistor sizing (safe current limiting per channel)

Each color channel is a separate LED and needs its own resistor. Use the basic LED resistor equation:


R = (V_supply - V_forward) / I

Approximate forward voltages (typical ballpark; varies by LED):

  • Red: ~1.8V–2.2V
  • Green: ~2.8V–3.3V
  • Blue: ~2.8V–3.3V

Practical “safe starting” resistor values

  • Arduino 5V: 330O per channel is a common safe start for indicator brightness
  • ESP32 3.3V: 220O–330O per channel is a common start (brightness depends on LED)
Color balance: The human eye perceives green as much brighter than red/blue at the same current. You may use different resistor values per channel (e.g., larger resistor for green) to get nicer “white”.

4) Wiring to Arduino (direct drive) and current limits

For a single RGB LED at low currents (a few mA per channel), you can drive it directly from Arduino pins with resistors. Wiring:


+5V -------------- Common Anode (CA)

Red pin  ---[Rr]--- Arduino PWM pin (sink current)
Green pin -[Rg]--- Arduino PWM pin (sink current)
Blue pin  -[Rb]--- Arduino PWM pin (sink current)

Arduino GND shared as normal

Current limits reminder

  • Don’t exceed your MCU pin current limits (and total MCU current).
  • If you want higher brightness or multiple RGB LEDs, use transistors/MOSFETs (see section 8).

5) Wiring to ESP32 (3.3V logic) and PWM channels

ESP32 uses 3.3V GPIO. Many RGB LEDs still light fine, but brightness may be lower than at 5V. The wiring concept is the same: common anode to +3.3V, each channel pin to a GPIO through a resistor.

ESP32 PWM: ESP32 PWM is flexible (LEDC). You can choose PWM frequency and resolution. For visible LEDs, frequencies like 500Hz–5kHz are common.

6) PWM color mixing: how to get any color

Color mixing means setting each channel brightness (0–255 on Arduino, for example). But remember: common anode is inverted — 0 means full ON, 255 means OFF if you are using analogWrite directly.

Inversion rule


PWM_out = 255 - brightness

7) Gamma correction (why your colors look “wrong” without it)

Human brightness perception is non-linear. If you linearly increase PWM duty, the LED won’t “look” linear. Gamma correction fixes this by mapping brightness through a curve (often gamma ~2.0–2.4).

When gamma matters: smooth fades, pretty color transitions, and anything “UI-like”. For simple status indicators, you can skip it.

8) When to use transistors/MOSFETs (multiple LEDs / higher brightness)

If you want more brightness or multiple LEDs, don’t dump all that current through the MCU pins. Instead, use three NPN transistors or three N-channel MOSFETs as low-side switches.

Why drivers help

  • Protects MCU pins and avoids brownouts/resets
  • Allows higher LED currents and multiple RGB LEDs
  • Cleaner PWM behavior at higher currents

9) Arduino example code: set colors + smooth fade (common anode)

This code shows:

  • How to write colors correctly for common anode (inverted PWM)
  • A simple “rainbow-ish” fade sequence

/*
  5mm RGB LED (Common Anode) (#121) - Arduino PWM Color Mixing

  Wiring:
    Common Anode (CA) -> +5V
    R pin -> resistor -> PIN_R
    G pin -> resistor -> PIN_G
    B pin -> resistor -> PIN_B

  NOTE: Common anode is INVERTED:
    brightness 0   = OFF
    brightness 255 = FULL ON
  We'll handle inversion in setColor().
*/

const int PIN_R = 9;   // PWM
const int PIN_G = 10;  // PWM
const int PIN_B = 11;  // PWM

int clamp255(int x) { return (x < 0) ? 0 : (x > 255) ? 255 : x; }

void setColor(int r, int g, int b) {
  r = clamp255(r);
  g = clamp255(g);
  b = clamp255(b);

  // Invert for common anode:
  analogWrite(PIN_R, 255 - r);
  analogWrite(PIN_G, 255 - g);
  analogWrite(PIN_B, 255 - b);
}

void setup() {
  pinMode(PIN_R, OUTPUT);
  pinMode(PIN_G, OUTPUT);
  pinMode(PIN_B, OUTPUT);

  setColor(0, 0, 0);
}

void loop() {
  // Fade Red up/down
  for (int i = 0; i <= 255; i += 5) { setColor(i, 0, 0); delay(10); }
  for (int i = 255; i >= 0; i -= 5) { setColor(i, 0, 0); delay(10); }

  // Fade Green up/down
  for (int i = 0; i <= 255; i += 5) { setColor(0, i, 0); delay(10); }
  for (int i = 255; i >= 0; i -= 5) { setColor(0, i, 0); delay(10); }

  // Fade Blue up/down
  for (int i = 0; i <= 255; i += 5) { setColor(0, 0, i); delay(10); }
  for (int i = 255; i >= 0; i -= 5) { setColor(0, 0, i); delay(10); }

  // White (mix)
  setColor(180, 180, 180);
  delay(400);

  // Off
  setColor(0, 0, 0);
  delay(200);
}
    

10) Projects: status indicator, RGB mood light, sensor feedback, alarms

Project A: Status LED for your device

  • Green = OK
  • Yellow = warning
  • Red = fault
  • Blue = “pairing mode”

Project B: Sensor feedback (temperature / distance / gas)

  • Map sensor reading to a color gradient (blue?green?red)
  • Great for quick “at a glance” feedback

Project C: Simple mood light / desk indicator

  • Use slow fades and gamma correction for smooth visuals
  • Power from USB or a 5V supply (use drivers if you want more brightness)

Project D: Alarm beacon

  • Flash red on error
  • Alternate red/blue for attention
  • Pulsing patterns are more noticeable than constant light

11) Troubleshooting

Nothing lights up

  • Cause: common anode not connected to +V. Fix: connect CA to 3.3V/5V.
  • Cause: pins misidentified. Fix: test pins with diode mode or resistor test.
  • Cause: no resistors. Fix: add resistors on each channel.

Colors are “wrong”

  • Cause: R/G/B pins swapped. Fix: swap wiring in hardware or remap pins in code.
  • Cause: common anode inversion not handled. Fix: invert PWM (255 - value).

LED is very dim

  • Cause: resistor values too high. Fix: reduce resistors (within safe current).
  • Cause: ESP32 3.3V drive. Fix: accept lower brightness or use transistor drivers and 5V (with correct design).

MCU resets when LED turns on (multiple LEDs)

  • Cause: too much current from GPIO pins or weak supply. Fix: use transistor/MOSFET drivers and a better 5V supply.

12) Quick checklist


5mm RGB LED (Common Anode) (#121) Checklist
-------------------------------------------
 Identify the common anode pin and connect it to +V
 Use a resistor on EACH color channel (R, G, B)
 Remember logic is inverted (LOW / PWM low = ON)
 Use PWM for color mixing; invert values in code (255 - brightness)
 For multiple LEDs or high brightness: use transistor/MOSFET drivers
 Calibrate resistor values per channel if you want nicer “white” balance
    

Products that this may apply to