Electronic – arduino – Anode RGB LED Arduino Hookup Confusion

arduinoledpwmresistorsrgb

I'm having a really hard time getting this RGB Led working with my arduino. First i'll describe my setup (schematic) , and then i'll show you the code I have.

I have 5 volts going into the second pin of the RGB LED , then I have 3 200 ohm resistors on each of the pins going out of the LED which respectively lead into PMW 9,10,11 of my arduino.

(The led setup)
pin 1 Red (-)
pin 2 Positive Voltage
pin 3 Blue (-)
pin 4 Green (-)

The code I have is

byte Red = 9; 
byte Blu = 10;
byte Grn = 11;  

void setup () 
{  
 pinMode (Red, OUTPUT);
 pinMode (Blu, OUTPUT);
 pinMode (Grn, OUTPUT);
}

 int x=255;
 int y=0;
 int z=0;
 void loop () 
{
 analogWrite(Red,x);
 analogWrite(Blu,y);
 analogWrite(Grn,z);
}

The above code is supposed to produce a red light, but it produces a purple. I feel like it is something really simple…. Thanks

Best Answer

I had the same issue trying to use RGB LED that came with my kit.

Indeed, all the examples for my kit were written for common-cathode RGB LEDs. The PWM output values in the code assume common-cathode, and the wiring diagrams have the non-RGB lead wired to GND. It took me some time to figure out that the LEDs I got were common-Anode, and that the non-RGB lead should be connected to VCC instead of GND.

In adapting your examples, try this trick of inverting the scale just as your write the value out onto the PWM pins.

 analogWrite(Red,255-x);
 analogWrite(Blu,255-y);
 analogWrite(Grn,255-z);

This nice thing about doing it that way is all the other example Arduino code out there written for common-cathode can be used basically as-is with just this final adjustment of the values written.