Go Down

Topic: is there a formula for each color in a RGB LED ? (Read 4121 times) previous topic - next topic

Grumpy_Mike

#15
Mar 16, 2017, 12:58 pm Last Edit: Mar 16, 2017, 01:02 pm by Grumpy_Mike
Quote
Is it ok just to add 220R resistors to each pin or is it important to use exact values
It is fine to use the same values because you compensate for different brightness in the PWM value.
Ideally you would play about with different resistor values to give white when they are all full on, but that is a bit more tricky to do.

There are some colours that you can get on a computer scree that you can't get on an LED. Brown is one of them and orange is tricky. I did once try to get all the colours of the resistor colour code on LEDs but I failed, you just can't get some colours. The other thing that makes this tricky is that the eye responds not only to the colour of the LED but also the colour of the background when it makes up it's mind what colour you are seeing. This famous optical illusion illustrates the point nicely.


Squares A and B are the same colour.
From https://en.wikipedia.org/wiki/Optical_illusion

thanks, great info here.
I had no idea that there were colors that you coudn't do with a LED, brown, of course, never thought about a brown led... I want to ask why but I'm going to look for it. :-D
I know that it's almost impossible to get GOLD on a monitor, but I've seen some quite good goldens.

Anyway, I'm trying to do the thing with pots, I've been trying for hours and no luck, the analog pins that I try to read return a lot of noise, specially on the pot that controls de red led.

Here's the circuit



I tried a few variations, a 10k resistor from pot to GRD instead of a direct link, a capacitor between the 0% and 100% pins... Nothing, I must be doing something fundamentally wrong.

And here's the code.

Code: [Select]

int potR=A0, potG=A1, potB=A2;
int potRval=0, potGval=0, potBval=0;
int Rv=0, Gv=0, Bv=0;
int lastRv=0, lastGv=0, lastBv=0;
char s[16];

int redPin = 9;
int grnPin = 10;
int bluPin = 11;

int redVal = 0;
int grnVal = 0;
int bluVal = 0;

void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(grnPin, OUTPUT);  
  pinMode(bluPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  potRval=analogRead(potR);
  Rv=map(potRval,0,1024,0,256);
  //delay(5);
 
  potGval=analogRead(potG);
  Gv=map(potGval,0,1024,0,256);
  // delay(5);
 
  potBval=analogRead(potB);
  Bv=map(potBval,0,1024,0,256);
  // delay(5);
 
 
  if(Rv != lastRv)
  {
    lastRv=Rv;
    analogWrite(redPin, Rv);
  }
  if(Gv != lastGv)
  {
    lastGv=Gv;
    analogWrite(grnPin, Gv);
  }
  if(Bv != lastBv)
  {
    lastBv=Bv;
    analogWrite(bluPin, Bv);
  }
  sprintf(s,"%d,%d,%d",Rv,Gv,Bv);
  Serial.println(s); 
}


The green and blue are more or less ok, there are ranges where they flicker a bit but probably that's normal.
But the red one is giving me nightmares.
Not only it flickers a lot at low values, the red pot somehow affects the other pots.

With all pots to the left, I have values 0,0,0, with all the pots to the right I have 255, 255, 255.
I start to turn the red pot to the right, at some point the values from the other pots start to get affected, for example

Code: [Select]

167,5,5
167,5,5
167,6,0
165,0,0
167,5,5
167,14,5
167,5,6
165,0,0
165,5,5
167,5,5
167,5,5
167,0,0
165,0,5
167,5,5
167,5,5
167,6,0
165,0,0
167,5,5
167,13,5
167,5,6


And this keeps changing without me touching anything.

It's quite interesting to use the Serial Plotter in the Arduino IDE, but the fact is that I have no idea about what I'm doing wrong.
Another strange thing, with the serial output reporting a consisten 6, 0, 0 I see flashes of blue and green, it looks that somehow the red current leaks to the other colors... I tried another led (some batch, same seller) and it do the same thing.
--
You never learn anything by doing it right.

Grumpy_Mike

Quote
Here's the circuit
No it is an insufficient part of a physical layout diagram, you should post it all.
What value of pots do you have they should be 10K or smaller?
How have you routed the ground? Their should not be a part of the ground that passes the current from the LEDs through the analogue return path.

The code looks fine if a little over long for what you want to do. There is no need to only output if you see a change just keep writing. Just use a delay to control the printout speed.

You have to click in the image, the thumbnail crops some at the sides, I *was* passing the GND for the cathode with a single wire on the left of the board. I did it like this because of the holly dogma "there must be only one" (ground). 8)

*BUT* in an inspired moment I decided to move the wire, I moved it to a hole closer to the cathode of the led and noticed that the flicker pattern changed, so I went further, I connected that GND rail to the other GND pin in the other side of the nano (using the same wire). Well, the flickery totally vanished, everything is working as it should I can control each led, green is still quite stronger than red but I can get some nice oranges... There is no more interference from the red pot in the other pins.

By the way, the pots are not 10k, they are 100k which is all that I have at the moment.

Was I in some way, overloading that GND pin ? Even is I was shouldn't it be electrically connected to the other GND pin ? Why does this make a difference ? Should this teach me to connect all the GND pins available in a MCU to a common bus to make sure that any (what? noise?) is spread as much as possible ?

The values in the analog pins are not yet 100% stable, but now I have sequences of 40 or more X value and then one X+1 or in other cases X-1 (but never X, X+1 and X-1), and then another 40 or more X... I guess this means an intermediate value between X and X+1 or X and X-1. Is there a way to mute the sensibility just a bit ?

I thank you guys, you have been a great help.
--
You never learn anything by doing it right.

Grumpy_Mike

Quote
You have to click in the image, the thumbnail crops some at the sides,
This is how you post images here image guide

Quote
By the way, the pots are not 10k, they are 100k which is all that I have at the moment.
That will explain why changing one pot affected the other values.

Quote
Was I in some way, overloading that GND pin ?
No.
It looks like the long ground was propagating interference to the pots, which being 100K, are more susceptible that 10K pots.

Quote
I did it like this because of the holly dogma "there must be only one"
Sorry that is not correct. Sure avoid ground loops, but grounds must meet all at the same point. What you did was make that point the Arduino, which is good.

You will always get a +/- 1 reading from any analogue to digital converter. You can take averages if you don't like this, and changing to 10K will make it more stable.

With this 'controller' working I was able to find an approximate mix for the colors I want.
I thought that I could find the relation I was looking for in order to get a formula rgb->this_led by finding the 1st white, ie the lowest values for each color that produced a white color.
I got that at 19,1,5 (r,g,b) and it looks to be linear, 19*n, 1*n, 5*n produces whites until n=13, with n=14 the red color overflows and the relation is lost.
I guess that there should be a way to keep that relation, the obvious MOD is, of course, wrong, because the red overflows first.
Code: [Select]

n=1   ->  19,   1,   5
n=2   ->  38,   2,  10
n=3   ->  57,   3,  15
n=4   ->  76,   4,  20
n=5   ->  95,   5,  25
n=6   -> 114,   6,  30
n=7   -> 133,   7,  35
n=8   -> 152,   8,  40
n=9   -> 171,   9,  45
n=10  -> 190,  10,  50
n=11  -> 209,  11,  55
n=12  -> 228,  12,  60
n=13  -> 247,  13,  65
n=14  ->  10,  14,  70
n=15  ->  29,  15,  75
n=16  ->  48,  16,  80
n=17  ->  67,  17,  85
n=18  ->  86,  18,  90
n=19  -> 105,  19,  95


I'm at a loss, any suggestions ?
thanks!
--
You never learn anything by doing it right.

Grumpy_Mike

#21
Mar 27, 2017, 10:47 am Last Edit: Mar 27, 2017, 10:47 am by Grumpy_Mike
Quote
and it looks to be linear,
But it is not. See gamma correction

raschemmel

FYI,
ORANGE is Red = pwm value 255,
                 Green = pwm value 50,
                 Blue = pwm value 0.
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

Hi,

Can you explain what you mean ?

I haven't played with RGB leds in same time, but I learned that you can't do much without a datasheet, so, buying leds from ebay probably is not the best when trying to do this type of thing.
--
You never learn anything by doing it right.

raschemmel

I am fond of saying :

"There's a special Hell for people who don't read datasheets;

it's a place where everything blows up when you turn on the power and nothing works..."


That being said , there are some cases where you only need one or two parameters from the datasheet
and once you know those you can throw away the datasheet because you'll probably never need it again.
RGB leds are something like that.
What you need to know is:

The foward voltage (Vf)

and

The forward current (If)

For the following rgb led:

rgb led datasheet

those values are 20mA and 2.0 V.

You need to know this:
AnalogWrite

and you need to know how to write a function

and you need to know how to wire up an RGB led

and then you need to know where (or how ) to get the three PWM values you need to write to the
three pwm pins that drive the red , green and blue leds.

Then you simple use the following color mixer, pick a color and use the values given for that color as
the PWM values you use with the analogWrite function to drive the three leds.

RGB COLOR MIXER

For example, if you pick light orange , you get red = 255, green = 128 & blue = 0.
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

Grumpy_Mike

If you want to know what RGB values make what colour then see this interactive web page. https://www.rapidtables.com/web/color/RGB_Color.html

I always try to get a datasheet, but sometimes I don't know how to read it. :-(


I guess that for what I was trying to do at the time, a datasheet with the luminous intensity for each color for a given intensity whould have permit me to convert RGB values to a real color.
But, I know that the eye is not sensitive to all colors in the same way so, this values

For If=20mA
RED=800mcd
GREEN=4000mcd
BLUE=900mcd

Would need some more tweaking in order to give me a decent approximation to the problem RGB -> Real life color.

In the end I accepted that what I was trying to do was well above my skill and knowledge and just used the values that I got with the 3 pots. Perfect ? not at all, but good enough.
--
You never learn anything by doing it right.

raschemmel

Mike,
That's the same link as the RGB Color Mixer I linked in my last post.
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

Grumpy_Mike


Go Up