Hue-controllable RGB LED lamp

Could this be done using more than one LED on the output? Basically I was wondering if this could be scaled up with enough LEDs to add ambient lighting to an entire room. Nothing too complex as in they could all change at once. I am guessing you would need some sort of external power supply though as they would draw too much current for just the arduino board.

akgraphics, could you explain this piece of code?

//map values - arduino is sinking current, not sourcing it
r = map(r, 0, 255, 255, 0);
g = map(g, 0, 255, 255, 0);
b = map(b, 0, 255, 255, 0);

I'm learning a lot of programming techniques at the moment and it appears you're reversing/inverting the output by mapping it? Is that just to do with how you have your LED's connected to the outputs?

Thanks in advance, Easty.

PS Sorry thebias I can't answer your question (yet) but I'm planning something similar by the sounds of it.

As you partially guessed, map() takes an amount and the range of values to which it belongs, and remaps it to another range of values. I think the documentation page on it is very clear :

http://www.arduino.cc/en/Reference/Map

Thanks Theboii, I had had a read of the reference material and seemed to understand it!

I was wondering why akgraphics had used it in his code as in my brief experiments with PWM'ing LEDs last night I didn't have to perform that change. I guess it's circuit specific though...

Cheers, Easty.

Ok, sorry ^^

Basically, you have to revert the value of an output pin when the pin is connected to the ground of the component. This is called "sinking" instead of "sourcing", which is when you output power to the component.

You often sink current from components instead of sourcing them because the µC can sink more than it can source, since sinking just basically means "connecting to GND"

So, if a component is connected on the gnd side on a pin, and on the cathode side on a power source, you just reverse the values you send : Putting the pin to 0 or LOW will sink 100% of the power (within the limit of the µC capabilities, of course.), putting it to HIGH or PWM255 will prevent electricity to circulate.

I hope I made myself clear... I'm not sure :slight_smile:

Understood 100%!

Interesting about the sink/source comparision... I think I'd better redesign my circuit!

Thanks, Easty.

It is a neat idea, only thing I would suggest is that I might not invite float to the party.

AnalogRead returns 10 bits (0-1023)
You might want to look at the individual bits and assign them to colors directly

say 1 bits for intensity, 3 bits per color

analog read returns 1023 = 1111111111 in binary
intensity, R, G, B
1 , 111, 111, 111

multiply R, G, B by 18 and if intensity is set add 128 to each.

Edit: while this isn't technically hue, it will give you access to more color combinations and intensities than hue alone would.

Here is a non-float, hue only version also. What is the application? It sounds like it could be interesting.

void setup(){
  Serial.begin(9600);
}

void loop(){
//colors out of phase by 341  (1023/3)
  Serial.println("V,R,G,B");
  for(int v = 0; v < 1024; v++){ //analogRead surrogate
    Serial.print(v);
    Serial.print(",");
    Serial.print(colVal(v+341),DEC);    //red
    Serial.print(",");
    Serial.print(colVal(v),DEC);        //green
    Serial.print(",");
    Serial.println(colVal(v+682),DEC);  //blue
  }
  while(true);
}

//based on green hue function, add phase adjustment for red or blue value
byte colVal(int val){
  int v = val % 1024;
  if(v<=170)
    return (v * 3) / 2;
  if(v<=512)
    return 255;
  if(v<=682)
 return 255 - (((v%171) * 3) / 2);
  return 0;
}

Edit: Just noticed the part about non-floats messing up the pwm functions, that doesn't sound right.

akgraphics,

Nice job with that. Wish I ran across this post earlier. I actually ended up writing pretty much the same code for the fading, just without the map() and I'm using random values so that it fades from one random color to another. It's amazing that you thought of pretty much the same exact algorithm.

I ended up adding the map() because my tri-color LED has a common Anode

Not sure if this is at all helpful for you too easty but my theory so far is to use a combo of the code you guys have put up here with 3 of these circuits Arduino Playground - HomePage in order to drive the each of 3 colours for each LED. To start with I would just like to get 3 RGB leds up and running. I think this should work bearing in mind that I want them to all do the same thing at the same time. If anyone sees any gaping holes in my theory please point it out!

Cheers

I've got a question about the 'sinking' technique:
When the source is +5V, and the PWM pin is HIGH, no electricity will flow even if the +5V is capable of higher current than the pin, correct?

Also, how much current can an arduino pin safely sink? I know the pins can source 40ma, but didnt see a value for sink.

Thanks, and very nice code!

When the source is +5V, and the PWM pin is HIGH, no electricity will flow even if the +5V is capable of higher current than the pin, correct?

Yes if there is no voltage difference then there is no current flow.

Also, how much current can an arduino pin safely sink? I know the pins can source 40ma, but didnt see a value for sink.

You can sink 60mA at 25C and get down to a voltage of 1.5v.

That means sinking current you will not get the full 5v across the load but 5 - 1.5 = 3.5v

Sinking 40mA gets you down to under 1V on the output where as 20mA gets the output voltage down to 0.5v

akgraphics, just wanted to thank you. I have used some of your code in my own project to get a pot to control RGB values. Very useful! :slight_smile:

I'm trying to do something similar, but instead of a pot, I'm using three Ping))) ultrasonic rangefinders to control fade my LED's. I'm trying to use that map command to get the distance data turned into 0-255 values. Haven't quite got it yet...

Hi,
I have used your code and get the colors to adjust based on a potentiometer input. There is one thing I observed I do not seem to see any blues. I see green, pinks, reds yellow but do not get a good blue. Any ideas why this seems to be the case.
Venkat

excellent algorithm :))) i think is good when read 0 from pot to put all colors off /switch off/ and if the pot value is max 1023 to fire all the leds /white/ this will cover this missing part ::slight_smile:

ONe can also do the RGB LED control using a mouse x and y position feed from Processing, I think Its the Firmata libary , It efectively changes the Rduino platform inti a data IO device over serial, This is plent useful for pc based control

When you output an PWM value between 0 and 255, it's not even remotely similar to the same RGB value. The problem is that the LED brightness does not climb linearly with the PWM duty cycle. If you go from 1 to 2, there's a huge jump in brightness while if you go from 100 to 200 there's almost no discernable change. Quite often just to save power, I'll PWM my LEDs at about 20% duty cycle and it looks almost the same as full brightness. I've been considering a similar project for a while but because this issue I was looking at only having 10 possible brightness levels for each colour and then using empirical observation to convert those values to PWM levels.

Also, as far as your map function...Why aren't you just using

r = 255 - r;
g = 255 - g;
b = 255 - b;

I'm using the TLC5940 PWM LED driver. No multiplexing... the rows are identical outputs. This is done with some nested loops and some trig (sine wave conversion).

I'm sure I could have a potentiometer control 1 or more of the variables used in the loops above to either fade the matrix to a specific color or just take the scrolling part out of it and have the potentiometer just send static values to be used for the color calculation.

I would also like to thank you i have used the fade function to test RGB leds in my first little project. it picks random amounts for RGB and fades between them at also a random speed, its like a full color candle :slight_smile:

eventually what i want to make is a lamp which changes color according to barometric pressure, average ambient noise level, temperature, and other environmental inputs. so this would be perfect.

I just got the arduino for a particular musical instrument project (but im having way too much fun with it)