Projects / Status light with a common-anode RGB LED

Status light with a common-anode RGB LED

Beginner2 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO12.8 mA

    Find the common pin

    Longest leg, check with the diode test.

  2. NANO12.8 mA

    Wire the common pin

    To the Nano 5V pin.

  3. NANO

    Wire the colours

    Each through 220 ohm to D9, D10, D11.

  4. NANO

    Wire the button

    D2 to GND.

  5. NANO

    Upload the sketch

    The lamp starts off.

  6. NANO

    Press the button

    Steps through eight colours.

  7. NANO

    Test the inverted logic

    Change 255 minus value to value and watch the colours flip.

  8. NANO

    Change the colour list

    Add your own mixes.

In short

How do you build a status light with a common-anode RGB LED?

Learn the difference between common-cathode and common-anode LEDs by showing eight colours on one lamp, stepped with a button. You need Arduino Nano, RGB LED 5 mm common anode, Resistor 220 Ω × 3, Tactile push button 6 mm, Digital multimeter, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 120 minutes.

Level
Beginner
Time
About 120 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
RGB LED common pin (longest leg)Nano 5VCommon anode goes to the positive supply
RGB red leg220 ohm to Nano D9Red channel, PWM pin
RGB green leg220 ohm to Nano D10Green channel, PWM pin
RGB blue leg220 ohm to Nano D11Blue channel, PWM pin
Button leg 1Nano D2Colour step input
Button leg 2Nano GNDPressed pulls D2 low

Code

An RGB LED has three LEDs in one package, and they share one pin. In a common-cathode part the shared pin goes to ground and each colour is switched on with a high signal. In a common-anode part, like this one, the shared pin goes to the positive supply and each colour lights when its pin is pulled low. That means the PWM values must be inverted: 255 means off and 0 means fully on, and the sketch handles this in a single function so the rest of the code can use normal colour values. The longest leg is the common pin, and you can check it with the meter on its diode test by touching the common leg and each other leg: one colour lights faintly each time. Each colour has its own 220 ohm resistor, because the three LEDs need different currents: red drops less voltage than green and blue, so it takes slightly more current. At 5 volts, red draws about 14 milliamps and the others about 9, well within the Nano pin limit. A button steps through eight colours, from off to white, and each one is also printed on the serial monitor.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const int PIN_R = 9, PIN_G = 10, PIN_B = 11, BTN = 2;

// Common anode: the LED lights when the pin goes LOW, so every value is inverted.
void setRGB(byte r, byte g, byte b) {
  analogWrite(PIN_R, 255 - r);
  analogWrite(PIN_G, 255 - g);
  analogWrite(PIN_B, 255 - b);
}

const byte COLOURS[8][3] = {
  {0, 0, 0},         // off
  {255, 0, 0},       // red
  {0, 255, 0},       // green
  {0, 0, 255},       // blue
  {255, 160, 0},     // amber
  {0, 255, 255},     // cyan
  {255, 0, 255},     // magenta
  {255, 255, 255}    // white
};
const char *NAMES[8] = {"off", "red", "green", "blue", "amber", "cyan", "magenta", "white"};

int index = 0;
bool wasPressed = false;
unsigned long lastChange = 0;

void show() {
  setRGB(COLOURS[index][0], COLOURS[index][1], COLOURS[index][2]);
  Serial.println(NAMES[index]);
}

void setup() {
  pinMode(BTN, INPUT_PULLUP);
  Serial.begin(9600);
  show();
}

void loop() {
  bool pressed = (digitalRead(BTN) == LOW);
  if (pressed && !wasPressed && millis() - lastChange > 40) {
    lastChange = millis();
    index = (index + 1) % 8;
    show();
  }
  wasPressed = pressed;
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

off

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Power onLED off
First pressRed
Second to seventh pressGreen, blue, amber, cyan, magenta, white
Eighth pressBack to off
Serial monitorThe colour name prints on each press

If it does not work

Colours are inverted

You have a common-cathode part, wire the common leg to GND and remove the inversion.

Only one colour works

Check each resistor reaches its pin.

Too dim

Check the 220 ohm values and that the common leg is on 5V.

White looks pink

Red is stronger, lower the red value a little.

Button double-steps

Increase the 40 millisecond debounce.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the status light with a common-anode RGB LED?

Arduino Nano, RGB LED 5 mm common anode, Resistor 220 Ω × 3, Tactile push button 6 mm, Digital multimeter, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the status light with a common-anode RGB LED take to build?

About 120 minutes for a beginner maker, in 8 steps.

How do I know the status light with a common-anode RGB LED works?

Power on: LED off. First press: Red. Second to seventh press: Green, blue, amber, cyan, magenta, white.

Colours are inverted: what should I check?

You have a common-cathode part, wire the common leg to GND and remove the inversion.

Only one colour works: what should I check?

Check each resistor reaches its pin.

Too dim: what should I check?

Check the 220 ohm values and that the common leg is on 5V.

White looks pink: what should I check?

Red is stronger, lower the red value a little.