Controlling Common Anode RGB LED

Hello,
I have Common Anode RGB LED. I want to Control It Using Arduino.

Common Anode RGB LED:

I know how to control Common Cathode RGB LED with analogWrite() method but is it possible to use negative value in analogWrite() to Control Common Anode RGB LED

Code:

int redPin = 11;
int greenPin = 10;
int bluePin = 9;

void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop()
{
  setColor(-0x0D, -0xE0, -0x11);
}

void setColor(int red, int green, int blue)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}

?

You can't write negative values to analogWrite as far as I know.

When you are writing -0x0D, -0xE0, -0x11 you are wanting to write #0DE011 which is a strong lime green.

#000000 is black for common cathode- everything off.

Everything off for a current sinking common anode is all high, which is #ffffff.

So try inverting the colors. Write white as #000000 and black as #ffffff.

Lime green inverted is F21FEE. Try that and see what happens.

Edit: Don't forget the resistors on each cathode of the LED.

No. Use 255-value instead:

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

I know how to control Common Cathode RGB LED with analogWrite() method but is it possible to use negative value in analogWrite() like this:
setColor(-0x0D, -0xE0, -0x11);

AnalogWrite(byte pin, int value) => expects an int, so the value is "legal".
It is used to set a register of the timer and depending on if it is 8 or 16 bit register the actual value will be different.

The documentation says value should be 0 .. 255, so I wonder why they defined it as an int in the first place? (never noticed)

o I wonder why they defined it as an int in the first place?

So they could use it on the Due with 12 bit PWM?

This is My Circuit:

What I have to Change/Add in My Circuit or Code to Control Common Anode RGB LED?

bigred1212:
You can't write negative values to analogWrite as far as I know.

When you are writing -0x0D, -0xE0, -0x11 you are wanting to write #0DE011 which is a strong lime green.

#000000 is black for common cathode- everything off.

Everything off for a current sinking common anode is all high, which is #ffffff.

So try inverting the colors. Write white as #000000 and black as #ffffff.

Lime green inverted is F21FEE. Try that and see what happens.

Edit: Don't forget the resistors on each cathode of the LED.

It's Good Idea but for example i want to Write #FFFFFE after inverting it will look like this: #000001 and it will analogWrite() 1 to blue Led and on Circuit on Blue Led - Voltage Will be = ~ +0.004V. So Blue Led Will be off.

Gigi10012:
What I have to Change/Add in My Circuit or Code to Control Common Anode RGB LED?

You should use one resistor per LED. You can't use a single resistor on the common terminal because that will limit the TOTAL current but there is no guarantee that the total will be shared equally by all three.

AWOL:

o I wonder why they defined it as an int in the first place?

So they could use it on the Due with 12 bit PWM?

Makes suddenly perfect sense :wink:

thx.

Gigi10012:
It's Good Idea but for example i want to Write #FFFFFE after inverting it will look like this: #000001 and it will analogWrite() 1 to blue Led and on Circuit on Blue Led - Voltage Will be = ~ +0.004V. So Blue Led Will be off.

#FFFFFE is RGB 255,255,254 and is so close to white you won't be able to tell the difference.

If you write the inverse of 000001 in a common anode setup it will be so close to white you can't tell the difference. Writing LOW or 1 turns things on.

You need a resistor on each cathode and no resistor on the anode.