Subscribe to Make Magazine Today!

Make: Projects

Use a Common Anode RGB LED

This is a quick tutorial on how to fade between colors with an Arduino and Common Anode RGB LED.

Use a Common Anode RGB LED

How to fade between colors with the Common Anode RGB LED, included in Maker Shed’s Ultimate Arduino Microcontroller Pack.


Step #1: Plug in the RGB LED

Use a Common Anode RGB LEDUse a Common Anode RGB LEDUse a Common Anode RGB LEDUse a Common Anode RGB LED
  • Grab all the parts, and begin by plugging the Common Anode RGB LED into your breadboard.
  • Orient it in the breadboard with the red leg on the left, and power leg second from the left.
  • That's power (long) leg to F42 on the breadboard, with the red leg right next to it in F43.

Step #2: Begin the wiring

Use a Common Anode RGB LEDUse a Common Anode RGB LEDUse a Common Anode RGB LED
  • Now wire up the positive (longest) lead of the LED to the 5V header on the Arduino.
  • You can use a jumper from J42 to the Arduino.
  • Connect the three resistors, one from each of the three other LED pins to another spot on the transparent breadboard.
  • Place one 330 Ω resistor in G43 and G49.
  • Place another 330 Ω resistor in H41 and H35.
  • Place the last 330 Ω resistor in G40 and G34.

Step #3: Connect the RGB pins to the Arduino

Use a Common Anode RGB LED
  • Next, connect the three pins of the LED to the Arduino.
  • Red goes to PWM Pin 6, from breadboard pin J49.
  • Green goes to PWM Pin 5, from breadboard pin J35.
  • Blue goes to PWM Pin 3, from breadboard pin J34.
  • Now you can grab the code from GitHub and upload it to your Arduino. Have fun!
  • Milan Keser

    Found this just after I finished something very similar. Thought I’d post the code here as an add-on. The following will slowly fade the common anode RGB LED from random color to random color. I think it makes a nice night light or decoration…

    /* RBG LED Fader. I used a common anode RGB LED, which means
    the common pin should be connected to +5V, and red, green, and
    blue go to PWM digital outputs. LOW=LED off, HIGH=LED on. I
    used a 180 ohm resistor on red, and 100’s on green and blue.

    int redPin = 9;
    int greenPin=10;
    int bluePin=11;
    int oldRed=255; // Start out with red off…
    int oldGreen=255; // and green…
    int oldBlue=255; // and blue.

    void setup() {
    pinMode(redPin,OUTPUT); // Set pin modes. Not sure this is necessary
    analogWrite(redPin,oldRed); // Initial RGB settings.
    Serial.begin(9600); // Open serial for debug info.

    void loop() {
    int newRed=random(0,255); // Choose new values for red,
    int newGreen=random(0,255); // green,
    int newBlue=random(0,255); // and blue.
    int redMod=0; // The ‘mod’ variables will hold
    int blueMod=0; // the adjustments we will make
    int greenMod=0; // to move from old to new color.
    float r1,r2,b1,b2,g1,g2; // To store intermediate calculations.

    Serial.print(“(“); // This block sends the next
    Serial.print(newRed); // color to Serial for
    Serial.print(“, “); // debugging/info.
    Serial.print(“, “);

    r2=newRed-oldRed; // First, find the difference
    g2=newGreen-oldGreen; // between the new color values
    b2=newBlue-oldBlue; // and the old.

    for(int i=0;i<=100;i+=1) { // The i loop will move from
    r1=float(i)/100; // our old color to our new
    g1=float(i)/100; // color. In 100 steps. The
    b1=float(i)/100; // math didn't work on int's.

    redMod=r1*r2; // The "current" color is always
    greenMod=g1*g2; // old+mod.

    newRed=oldRed+redMod; // Calculate those new color
    newGreen=oldGreen+greenMod; // values.

    analogWrite(redPin,newRed); // Send the new colors to
    analogWrite(greenPin,newGreen); // the LED.

    delay(20); // Controls the fade rate.

    oldRed=newRed; // Store the new (current)
    oldGreen=newGreen; // colors as the (now) old
    oldBlue=newBlue; // ones.

    delay(5000); // Linger a bit on each color.

    • Milan Keser

      I’m sorry, there’s something wrong with the quotation marks after copying and pasting them to the site. If you edit them, replacing them with fresh ” marks, it will compile. (Or, it did for me.) You can tell you have it right when the “(” in line 33 (and the quoted segments of the following 6 lines) turn blue instead of staying grey in the arduino editor.

      As an alternative, just delete all 7 of the Serial.print and Serial.println lines, plus the Serial.begin() above. They were really only there for debugging/watching the color fade process. They do not affect the LED behavior at all.

      It’s like cutting and pasting turns the ” characters in my code into something that looks like double quotes to humans, but something else entirely to the compiler.

      tl;dr; replace the double quotation marks in the seven Serial.print lines with fresh ” marks that you type youself, or else delete those seven lines.

  • Travis Szewczyk

    can someone please help me figure out how to transfer this from the arduino to a stripboard please. very new to this and circuits in general so any help would be great

  • Trey Roady

    Great project, but what I’m really wondering is how you take the concept into powering something slightly larger. The LED is nice, but the amount of light is far from impressive.

    How would you upgrade it to a globe light that’s visible in a standard-lit room?