Common Cathode vs. Anode RGB LED

I have some code that I found online (Update: Original post can be found at the old forum http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1230560883/2, it was written by Studio Bricolage) however I can’t figure out how to transform the code to work with a Common-Anode RGB LED using pull down resistors. Does anyone have any ideas of where I can begin to get this RGB LED to work? Hints and ideas are appreciated at this point since I’ve been banging my head on a well for a couple of weeks.

Here’s the code.

``````//
// random tri-color fading LED *common cathode*
//

int value;
int w;
int x;
int y;
int gradation = 255;
float light_value;
long value_RED;
long value_GREEN;
long value_BLUE;
int ledpin1 = 9;                           // red LED connected to pwm pin 9 via 1K ohm resistor
int ledpin2 = 10;                          // blue LED connected to pwm pin 10 via 1K ohm resistor
int ledpin3 = 11;                          // green LED connected to pwm pin 11 via 1K ohm resistor
// LED cathode connected to ground
void setup()

{
// nothing for setup
}

void loop()
{
value_RED = 200 + random(56);                  // puts a random number between 0 and 255 into value_RED
value_BLUE = 200 + random(56);                 // puts a random number between 0 and 255 into value_BLUE
value_GREEN = 200 + random(56);                // puts a random number between 0 and 255 into value_GREEN

for(value = 200; value <=255; value+=5)                             // ramp the number in "value" from 0 to 255
{
w = light_value;                                                // transfer light_value into an integer
x = light_value;                                                // transfer light_value into an integer
y = light_value;                                                // transfer light_value into an integer
analogWrite(ledpin1, y);                                        // increase the voltage in pin 9 from 0 to random volts
analogWrite(ledpin2, x);                                        // increase the voltage in pin 10 from 0 to random volts
analogWrite(ledpin3, w);                                        // increase the voltage in pin 11 from 0 to random volts
delay(30);                                                      // waits for 30 milli seconds to see the dimming effect
}
for(value = 255; value >=200; value-=5)                             // ramp the number in "value" from 255 to 0
{
w = light_value;
x = light_value;
y = light_value;
analogWrite(ledpin1, y);                                        // increase the voltage in pin 9 from random to 0 volts
analogWrite(ledpin2, x);                                        // increase the voltage in pin 10 from random to 0 volts
analogWrite(ledpin3, w);                                        // increase the voltage in pin 11 from random to 0 volts
delay(30);                                                      // waits for 30 milli seconds to see the dimming effect
}
delay(700);                                                        // wait .7 seconds between fade up and down
}
``````

here is a link showing the wiring diagram http://fritzing.org/projects/colored-light-rgb-led/

the difference between code for common anode and cathode is the values you send to the pins

for Common Anode LED Lights you have the common leg of the led attached to the (+) power so to make a color light you need to take that pin LOW (-) so if you write HIGH or 255 it will be off and if you write LOW or 0 it will be on

and for Common Cathode LED Lights you will have the opposite as the (-) led of the led is attached to the GND so to make a color light you need to take that pin High (+) so if you write LOW or 0 it will be off and if you write High or 255 it will be on

Apologies if this is a really dumb question, but this confuses me a little... I have just received my Common Anode RGB Led's and have got them working as per the above diagram... However, what I am confused about is where the GND is coming from?? The circuit goes from the 5v Arduino Pin straight into Pins 3, 5 and 6 (via a resistor)... So where is it grounded??

The arduino pin going low is the 'ground'. The LED only cares about current flow from anode to cathode, so whatever makes the ~2V & 20mA current flow will turn it on. So 5V from on one side, and current limit resistor to a low output pin that will sit at ~0.5V will do it.

Thanks Crossroads... So ultimately, any Arduino Pin set to LOW could be used as a Ground (not that you'd want to though I guess)? I was also confused about what would happen when the Pin was set to HIGH, but thinking about it, current wouldn't flow as it wouldn't be Grounded? Correct?

Correct, anode at 5V and arduino pin/cathode high means no voltage drop across the LED, thus no current flow. The arduino can source 40mA (absolute max, plan for 20-30 to avoid damge) of current when High, and can sink 40mA (absolute max again) when low.

Genius... Thanks pal.