RGB LEDs HELP!!!!

hey guys, I'm really new to the Arduino, so bear with me. I've been (trying) to work with RGB LEDs. I have this big project/idea in my head, so i bought one RGB LED to see if this idea can be brought to life! So i have a 4 pin common anode LED. I've been experimenting with the LED library, using "void setValue(byte val)" to set each RGB color. It's working ok...but not really. Any help would be much appreciated! I was also wondering how i would hook up 3 potentiometers, one to control the RED, one to control the GREEN, and one to control the BLUE. Thanks a ton!

working ok...but not really. Any help would be much appreciated!

So? Where is it?

BTW there are plenty of threads with exactly this code in, on the forum.

It would really help to see your sketch.

I don’t really have a code. I don’t know what to do! I’ve tried doing something like this:

#include <LED.h>

LED led9 = LED(9);
LED led10 = LED(10);
LED led11 = LED(11);

void setup() {
}

void loop() {
led9.setValue(0);
led10.setValue(0);
led11.setValue(255);
}

But that’s not really working. I figured that if i could get any color by setting the RGB colors to the byte value.

But my main problem is that when i try to connect a potentiometer, it grounds out and turns everything off! Any help with that!?!
Here’s what i have:
Common Anode(+) to 5V
RED LED pin to 11
GREEN LED pin to 10
BLUE LED pin to 9
Potentiometer middle pin to Analog 2
Potentiometer Ground to GND
Potentiometer Power to 5V

Like i said. I would like to be able to control the RGB values with a Potentiometer. I only have one potentiometer at the time, so lets say that i would like to control the RED value and leave the the Green and Blue at high (or something like that). Thanks!

You MUST have current limiting resistors between your Arduino pins and the LEDs.

I'd break down the problem first. Assuming you can get "Serial" to work, try reading values off your pots, and print them so that you can verify that the pot is working.

You MUST have current limiting resistors between your Arduino pins and the LEDs.

Additionally, if your LED is truly common anode, you will have to invert the value you pass to setValue(), using something like

lled9.setValue(intensity ^ 255);

ahh. ok. I tested my potentiometer using the serial.println and it seemed to work fine. I will try the intensity thing and post back soon. Thanks for all the quick responses!

You said when you connect the pot , it resets - what value pot have you got?

(well-spotted EJ, I just saw the LED-to-pin connection and knee-jerked. I guess at low PWM ratios the LEDs and output pins would probably be OK without resistors, but as the ratio got higher, maybe the average current gets too great)

Hmm, i don't know. I just hooked up my Potentiometer and it worked fine. Didn't reset. Wierd. Anyway, i edited my code:

led10.setValue(value ^ 255);

and that seemed to help a little. However when its all the way, one way...it'll get a deep red and as i start to turn it it gets whiter and whiter, then it goes back to red and does that about 4 times before the POT is all the way, the other way. Any suggestions?

Let's see all the code.

"value = map (value, 0, 1023, 0, 255);" before you call "setValue". Or "value /= 4;"

analogRead goes 0...1023, analogWrite goes 0...255.

BTW red to "white" sounds bad - better get those current limiters in soon, or it'll end in tears!

Bingo!

YES! That worked! The map thingy! That’s amazing! Thank you so much. So theoretically if i would get two more potentiometers and use the following code, i could get any color i want?

#include <LED.h>
int potPin1 = 1;
int potPin2 = 2;
int potPin3 = 3;
LED led9 = LED(9);
LED led10 = LED(10);
LED led11 = LED(11);

void setup() {
pinMode(potPin1, OUTPUT);
pinMode(potPin2, OUTPUT);
pinMode(potPin3, OUTPUT);
}

void loop() {
int value1 = analogRead(potPin1);
int val1 = map(value1, 0, 1023, 0,255);
int value2 = analogRead(potPin2);
int val2 = map(value2,0,1203,0,255);
int value3 = analogRead(potPin3);
int val3 = map(value3,0,1203,0,255);

led9.setValue(val1 ^ 255);
led10.setValue(val2 ^ 255);
led11.setValue(val3 ^ 255);
}

That should do it shouldn’t it?!?!?
Also, how many ohm resistor would i need? I have an 650mcd(green) 120mcd(blue) RGB LED with a FW current: 30mA(green/blue) 50mA (red)

Yay!

50 ohm resistors will work for all three. It calculates more like 60 for the red but 50 should be close enough.

:smiley: Thanks guys! You guys are great! I don’t mean to keep bothering you, but do any of you know anything about processing? I was thinking of a way to control the RGB values using processing instead of Potentiometers (or maybe use both!) I can’t seem to find any good tutorials on Processing and the Arduino, so i was wondering if any of you knew?! Thanks again! :wink:

To do your own calculations:

Look on the data sheet for the forward voltage drop for each color. It is usually abbreviated Vf.

Then, where Rs = the series resistor and Vcc = Arduino power supply voltage, usually 5V or 3.3V, and If = desired current in amps,

Rs = (Vcc - Vf) / If

For example:

Rs = (5.0 - 3.5) / 0.03 = 50 ohms for your green or blue LEDS.

(well-spotted EJ, I just saw the LED-to-pin connection and knee-jerked. I guess at low PWM ratios the LEDs and output pins would probably be OK without resistors, but as the ratio got higher, maybe the average current gets too great)

Thanks, there was a similar one the other day. Some day when I'm feeling especially cruel, I'd like to see which gives up first, the LED or the Arduino. :)