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 () 

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


2 Answers 2


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.


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.


Since you are pulling the LED to ground to turn it on, the pins that you set to zero are the ones that are on, which gives you two pins on.

I recommend starting with one pin only; hook it up, and see if you can get just that color to turn on. Add colors and go from there.

  • \$\begingroup\$ Correct. The OP is switching the ground end, not the positive end, so the code logic is inverted. A Logic low turns on the corresponding Led color, a logic high disables it. \$\endgroup\$
    – Passerby
    Feb 10, 2013 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.