Lorsque vous conduisez une LED avec PWM, la luminosité (telle que je la perçois) ne varie pas de manière linéaire avec le facteur de marche. La luminosité augmente lentement, puis augmente de façon exponentielle avec le rapport cyclique.
Quelqu'un peut-il suggérer une règle empirique à utiliser comme facteur de correction ou autre solution de contournement?
Réponses:
Pour 16 niveaux, il est facile de faire une simple table de correspondance "à la main" et de convertir la valeur de 4 bits en une valeur de 8 bits à transmettre au contrôleur PWM: il s'agit du composant que j'ai utilisé dans mon pilote de matrice à led FPGA. Pour un contrôleur de niveau 8 bits, vous avez besoin d'au moins 11-12 bits de sortie de la table de correspondance.
la source
1
bit more or less shifts left 1 position to the left with each step.In theory it should be exponential, but I've got best results for fading by using a quadratic function.
I also think you got it backwards. At low duty cycle the perceived increase in brightness is much bigger than at almost full duty cycle, where the increase in brightness is almost imperceivable.
la source
I've been looking in to this subject over the last few days as I have the same problem... trying to dim LEDs using PWM in a visibly linear way, but I want full 256 step resolution. Trying to guess 256 numbers to manually create a curve is not an easy task!
I'm not an expert mathematician, but I know enough to generate some basic curves by combining a few functions and formulas without really knowing how they work. I find that using a spreadsheet (I used Excel) you can play around with a set of numbers from 0 to 255, put a few formulas in the next cell, and graph them.
I'm using pic assembler to do the fading, and so you can even get the spreadsheet to generate the assembler code with a formula (
="retlw 0x" & DEC2HEX(A2)
). This makes it very quick and easy to try out a new curve.After a bit of playing around with LOG and SIN functions, the average of the two, and a few other things, I couldn't really get the right curve. What is happening is that the middle part of the fade was happening slower than the lower and higher levels. Also, if a fade-up is immediately followed by a fade-down there was a sharp noticeable spike in the intensity. What is needed (in my opinion) is an S curve.
A quick search on Wikipedia came up with the formula needed for an S curve. I plugged this into my spreadsheet, and made a few adjustments to make it multiply across my range of values, and came up with this:
I tested it on my rig, and it worked beautifully.
The Excel formula I used was this:
where A2 is the first value in column A, which increases A3, A4, ..., A256 for each value.
I have no idea if this is mathematically correct or not, but it produces the desired results.
Here are the full set of 256 levels that I used:
la source
I found this guy who uses a method he calls "Anti-Log Drive". Here is the direct download link for his info.
la source
I was using an ATtiny for lighting my deck. Brightness is controlled using a pot connected to ADC pin.
Tried exponential function and the PWM output based on that seems to be giving linear increase in perceived brightness.
I was using this formulae:
Attiny85 @8MHz was taking about 210us to perform the above calculation. To improve the performance, made a lookup table. Since input was from 10-bit ADC and ATtiny memory is limited, I wanted to create a shorter table as well.
Instead of making lookup table with 1024 entries, made a reverse lookup table with 256 entries (512 bytes) in program memory (PGMEM). A function was written to perform binary search on that table. This method takes only 28uS for each lookup. If I use a direct lookup table, it would require 2kb memory, but lookup would take only 4uS or so.
Calculated values in lookup table uses input range 32-991 only, discarding lower/upper range of ADC, in case there is problem with circuit.
Below is what I have now.
la source
This PDF explains the curve needed, apparently a logarithmic one. If you have a linear dimmer (your PWM value) then the function should be logarithmic.
Here you can find a lookup table for 32 steps of brightness for 8 bit PWM.
Here for 16 steps.
la source
Here is what I have done based on that arduino forum response. I have computed the values from 0 to 255 so it's easy to use with pwm on arduino
Then to use on Arduino just do like that :
Hope it is helpfull for some people ;)
la source
I'm dealing with this now, and I'm taking a slightly different approach. I want 256 levels of brightness, but mapping a linear 0-255 range to a non-linear 0-255 range winds up, as you can see in some of the other answers, with a lot of duplicate entries. (I.e., several of your input values result in the same brightness level.)
I tried modifying the algorithm to map a 0-256 input range to a 0-1023 output range, but even that had several values mapping to 0. So I'm trying something a bit different - I'm using the 0-255 level to generate non-linear values in the range 0-769 (that's 1023 minus 255) using
sin()
, then add that to the input level to get an output in the range 0-1023 with no duplicates. I'll configure a timer to use a counter of 1023, and set the comparator for the PWM output to values from the lookup table based on what lighting level I want (0-255).Here's the C program I used to generate my lookup table:
And here's the table:
I'll probably investigate other functions (like
log()
) once I've got this up and running.la source
For me this law seem to work pretty good: http://www.pyroelectro.com/tutorials/fading_led_pwm/theory2.html
la source