From PWM duration to the LED RGB color

Good morning everyone, I'm asking for your help to answer a question that has been gripping me for a few days and to which I can't find an answer.

I was playing with a led strip trying to make it of various colors, nothing too complicated, leds with 3 inputs, one for the value of R of G and of B to be given via an "ideally" analog signal between 0 and 3.3V. I succeeded in my intent without too much difficulty, however at a certain point I tried to make the LEDs take on the colors that the LED strip uses "by default" when controlled by an external controller. The external controller is nothing more than a pre-programmed microcontroller which pilots the inputs of the LEDs.

In this case obviously I can't know the RGB value set by the controller, so I tried to discover it using an oscilloscope that could read the signal while the LED was lit in one of those colours. I saw that obviously the microcontroller sends a PWM signal and not really analog one.

My question is as follows, now that I have information like PWM signal period, TON and TOFF times of the various R,G,B colors how can I convert these numbers into an RGB value to put in my code?

I tried with a proportion like: the period of the signal is 500uS, the red is high for 100uS the green is high for 10uS (overlapping with the red) and the blue is always low so the R value will be 255 x 20% and the value of G will be 255 x 2%. But the color is too dull in this way, compared to what it should be.
How can I find the color I'm interested in? (avoiding trying every color manually, I would like a mathematical formula that always works having the period of the PWM and the times TON of the signals)

You know the ratio of on and off times of the PWM signal. Use that to set the value of the analogWrite() that you drive the LED with

For example, suppose that the on/off ratio is 1 to 5 (the frequency does not matter). analogWrite() takes a value from 0 to 255 so multiply 255 by 1/5 to get the required value for analogWrite(). Note that analogWrite() takes an integer as its parameter rather than a float but in practice the fractional difference will make no visible difference to the output from the LED

You just need to know the RGB hex values and use them in analogWrite

Thanks for the answer, but as mentioned before this method does not give me exactly the original color. I get a much more muted color. In general I would like to get a mathematical formula to do the conversion

Yes, but that doesn't answer the question, how do i convert the pwm time to an rgb or hex value or whatever?

Are you using analogWrite to generate the PWM?

Have a look at - GitHub - RobTillaart/map2colour: Arduino library for mapping a float to RGB colour spectrum

PWM value = (HEX color value/255)/PWM frequency

I don't mean to sound rude, but please before giving answers to the question I asked, I would kindly ask you to read the question itself.

I need a mathematical formula which, taking as input the Ton and Toff times of 3 different signals used to respectively drive the red, green and blue color of a LED, returns the RGB value associated with that color

Don't you need 3 individual values R, G and B ?

Given the 3 on/off ratios you can derive each of them as previously described

1 Like

Thanks to all for the support,
I discovered by looking around that in these cases it is necessary to take into account the fact that an off LED cannot be chromatically displayed as RGB(0,0,0) since it cannot physically emit a black light , but it will be seen as a color with a given degree of transparency (which in the case of black will be absolute) therefore we are talking about RGBA and not RGB so it will have to be imagined as RGBA(0,0,0,0).

So the RGB values ​​that we will have used, i.e. the colors that take into account the black color are not correct, since we are talking about a light, not an electrical signal. The best way to solve this problem is to scale the values ​​according to a given aplha which equals the transparency factor.

the formula that worked for me is as follows:

  1. Get the maximum of RuS,GuS and BuS.

  2. Alpha = max/255;

  3. Scale every value by missing alpha:
    R =(RuS * (1/Alpha))
    G =(GuS * (1/Alpha))
    B =(BuS * (1/Alpha))

  4. The new color will be RGBA(R,G,B,Alpha)
    For the led AnalogWrite it will be enough to use the values ​​of R,G,B without needing to report the alpha.

@jim-p - CQ

These two formulae appear to be derivations of each other. One uses frequency (1/time) and the other uses time (1/freq).

Post #8:

Post #11

The easiest way I see is to use 3 sliders with a value from 0 to 255 for each led, there you would have an exact value of the color for the Led that are you working, you just have to use the Mit App Inventor application and create a simple code and mix the 3 colors to your taste.

I don't think a formula would be very exact since the value of the LEDs depends on each manufacturer and from 0 to 255 multiplied by 3, it is an endless number of variables.

It's like to mix real colors...depend a lot of manifacture too.
It rarely gets the same color even using digital numbers on the screens, it depends on the quality of components and even on the configuration, for example if it is warm/yellow or cold/blue.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.