# Luminous intensity Vs PWM

Discussion in 'General Electronics Chat' started by Shagas, Sep 12, 2013.

1. ### Shagas Thread Starter Active Member

May 13, 2013
802
74
Hello

I've been thinking about this for a while and now I need to implement it in a project.

I'm controlling 24 channels of Leds and I'm trying to linearize the percieved luminous intensity output of the Leds versus the PWM input .

Seeing as I can't find any info about this on google I've decided to draw up a diagram of what I think is going on .

The graph is a PWM duty cycle versus percieved luminous intensity
The purple curve is approximately how I think (from experiments) we percieve the light intensity(from leds at least) versus PWM duty cycle .

The dashed black is the desirable response and the green is the correction that I have to add to the purple to get the dashed linear black right ?

I am asking for some mathematical help as to how I could do it .
I've tried writing some weak algorithms to correct this but to no avail.

Basically I've got an array of values Pwm_index[240]; which hold 12 bit PWM values from 0 to 4095 and I need to shift each value (which is a function of the value in question) down by some value to give me the linear result .

Anyone have any ideas for an algorithm?
Or maybe if someone can throw me a math function ( for the green curve) which I can then program into the algorithm myself .

I program in C , atmel studio 6 (AVR).

2. ### Sensacell Well-Known Member

Jun 19, 2012
1,183
276

Here is a link to some good info on the subject

http://www.maximintegrated.com/app-notes/index.mvp/id/3667

Since this needs to be done very fast in most applications, lookup tables are usually employed instead of slow calculations.

I usually do a table with 8 bit input and 12 bit output.

3. ### Shagas Thread Starter Active Member

May 13, 2013
802
74
Hmm i'm trying to understand how to use the Gamma ratio in the algorithm.
This doesn't necesserally have to be done extra fast , I can spare a few thousand clock cycles to calculate it on the fly.

Why would you do 8 to 12 bits? Can I just do 12 to 12 bits ? Or should I scale down the 12 bits to 8 and ten convert to 12?

It doesn't have to be terribly accurate

May 13, 2013
802
74
5. ### Sensacell Well-Known Member

Jun 19, 2012
1,183
276
64 steps is going to be very 'chunky' - go with at least 8 bits.

Going 12 to 12 bits yields 'empty' resolution, especially at the critical low starting end of the curve - the curve gets fairly straight once you 'get out of the hole' at low brightness levels. I do this to keep the table from becoming too gigantic. For my applications the 8 bit input resolution is enough, as long as it's gamma corrected to 12 or 16 bits. It's all a trade-off. I use Excel to create and format the curve data, cut and paste into my source code.

6. ### Shagas Thread Starter Active Member

May 13, 2013
802
74
I found something like this:

corrected = 4095*(pow(uncorrected/4095,2.5));

7. ### Shagas Thread Starter Active Member

May 13, 2013
802
74
I see what you mean by empty resolution .
But the thing is I have 12 bit PWM values and I have to use 12 bit on the output .

Should I truncate my initial 12 bit PWM into 8 bit and then do the gamma correction?

I guess that wouldn't really help.

Last edited: Sep 12, 2013
8. ### Sensacell Well-Known Member

Jun 19, 2012
1,183
276
Sounds reasonable.

I assume you mean your 'input command brightness' is 12 bits, and your output is via a 12 bit PWM system? correct?

raising to the power of 2.5 is correct, you are also going to have to fiddle around with some offset to get the curve to start at the right place.

All this is hard to do without a way to visualize the output function, that's another good reason to use a table, you can graph it beforehand.

9. ### Shagas Thread Starter Active Member

May 13, 2013
802
74
Well I can make the input PWM values to be 8 bit but yea my output is Via 12 bit.

So by offset you mean I just manually add some intensity to the Pwm yeah?

10. ### Shagas Thread Starter Active Member

May 13, 2013
802
74
Well I did the above , I've got 8 bit PWM values and I'm converting them to 12 bit with a 2.5 gamma .

The 8 bit pwm values are basically a mapped out sinusoid wave which shifts through 24 leds and even though after the correction it's better , I still can't see a clear "sinuoidal intensity pattern" throughout the 24 leds

11. ### Shagas Thread Starter Active Member

May 13, 2013
802
74
The problem is that the part where the sinusoid is positive , about 8 leds which are around the peak look to be at exactly the same brightness

12. ### John P AAC Fanatic!

Oct 14, 2008
1,638
225
I had to do this recently, and I ended up by multiplying each setting by a constant to get the next one. My PWM unit (on a PIC16F690) had 10 bits of resolution, and I decided that 50 steps would be enough; that was based on the realization that the curve was going to take off very slowly, and even at 10 bits resolution, several of the earliest outputs didn't show a change. I'll list all 50 stages to illustrate this:

1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 10, 11, 13, 15, 17, 20, 22, 26, 30, 34, 40, 46, 53, 61, 70, 80, 93, 107, 123, 142, 163, 188, 216, 249, 287, 331, 381, 439, 506, 583, 671, 773, 891, 1026

Based on the number of steps and the desired final level, I made up a spreadsheet and simply adjusted the multiplication factor until it showed an output of 1024 after 49 increments; to achieve that, the multiplication factor ended up as 1.152. Obviously, the results were each a floating-point number which I then converted to the closest integer, and of course I substituted 1023 for the final 1026. Operating frequency of the PWM output was 500Hz.

This was a project I did for someone else, and to show him what it looked like, I made a video: