RGB LED Question

I hope someone can help me with a really basic question. I thought I had seen something on the forum about this but now I can’t find it…

I’ve been trying to get an RGB LED to work as described in Getting Started with Arduino. I’ve been having so many problems I decided just to use the push button sketch from the book to try to isolate the issue.

Initially I hooked up the second pin on the LED to ground and all the others to pins on the Arduino with resistors in between (again just as in the book). However this doesn’t work. The only thing I can get working is if I connect the second pin on the LED to the Arduino and all the others to ground. What am I doing wrong? Thanks for any help.

Adam

I had the same problem. Turns out, the ones that come with an Arduino kit(I assume you bought a kit) share a common cathode instead of anode. That means that instead of 1 ground, they share a common power source. So, hook up the second pin to the 5V on the board and the others to PWM pins. Should work. Another thing to remember is that normally with RGBs, 255 is full power and 0 is off. With these, 255 is off and 0 is full power. With PWM output, you should be able to fade from color to color using for loops and the analogWrite(pin, #0-255) function.

Good luck!

Here is a bit of code that you can use to test it. Red is connected to 9, green to 10, and blue to 11.

int redpin=9;
int greenpin=10;
int bluepin=11;

void setup()
{
pinMode(redpin,OUTPUT);
pinMode(bluepin,OUTPUT);
pinMode(greenpin,OUTPUT);
}
void loop()
{
for(int i=255; i>=0; i–) //fades to blue fom green
{color(0, i ,255-i);
delay(30);
}
for(int i=0; i<=255; i++) ///…and to red
{
color(i,0,255-i);
delay(30);
}
delay(800);
for(int i=0; i<=255; i++)//then back to green
{color(255-i,i,0);
delay(30);
}

}

void color(int red, int green, int blue)
{
analogWrite(redpin, 255-red);
analogWrite(greenpin, 255-green);
analogWrite(bluepin, 255-blue);
}

Thanks Travtag. I’ll try this out and see what happens.

Adam

Ok. Once I got the 2nd LED pin wired to 5V it worked perfectly. Thanks for the help.

Adam