[RESOLVED] led RGB strange colors

Hello,

I'm trying to use my common anode rgb led but i don't understand how the mixing colors work.
RGB led: R = 1.9V , G = 3.0V , B = 3.0V; 20mA.
I calculated the resistor (5V - 1.9V) / 20mA = 150 ohm for Red
(5V - 3.0V) / 20mA = 100 ohm for Green and Blue.

I tried many colors but the don't seems right:
(i tried with Blue)

analogWrite(red, 0);
analogWrite(green, 0);
analogWrite(blue, 255);

But the color was yellow. Is it right? For me is really strange :slight_smile:
I also tried to work with a RGB color picker online:
i tried a pink rgb color (235, 47, 166) but the result was a blue-green color.

Please help! :slight_smile: Thank you!

Hello,
I resolved my problem! I don't know why but my values were inverted (0 was 255), so i mapped my color variables.
Thanks anyway :wink:

With a common-anode LED, pulling a pin low will illuminate it. Pulling red and green low together will give yellow. For blue, try:

analogWrite(red, 255);
analogWrite(green, 255);
analogWrite(blue, 0);

This is why programmers are crazy, it comes from having to think backwards sometimes :smiley:

1 Like

Create yourself a function that does the inverting for you, and then you don't have to worry about it afterwards.

I.e

Void setRGB(int redBrightbess, int greenBeightness, int blueBrightness)

Within your function take each value and invert the brightness before do an analogWrite, such as analogWrite(redPin, 255 - redBrightness)

Set the relevant LED pin assignments elsewhere.

Call your function like setRGB(255,0,0) to turn the red lED on.

If you want to do it for more than one RGB LED then you could pass the pin numbers for each one to the function or even do in a number of other ways.

Create yourself a function that does the inverting for you

Or even simpler just subtract the value you want to set from 255 like this:-

analogWrite(red, 255-redVal);
analogWrite(green, 255-greenVal);
analogWrite(blue, 255-blueVal);