*linear* PWM LED fade with arduino
I found a simple look-up table to correct for this on this page.
255, 180, 128, 90, 64, 45, 32, 23, 16, 12, 8, 6, 4, 3, 2, 1,0
This seems to work very well, but I don't know where this comes from. Does anyong know the ectual equation to get the % duty cycle from the %brightness? I like to abstractise things.
6
comments
|
Add Comment
|
using zunzun.com to perform curve fitting I have created the following c function which converts a required percentage (0..100) to a pwm value (255). Tested on an arduino uno it appears quite smooth.
I hope it is of some use to fellow faffers.
// returns a pwm value (0..255) for a required percentage (0..100)
// as to provide a linear fade as perceived by eye
int linearPWM(int percentage){
// coefficients
double a = 9.7758463166360387E-01;
double b = 5.5498961535023345E-02;
return floor((a * exp(b*percentage)+.5))-1;
}
This is a VERY common non-linear relationship in nature. Look up the "inverse square law", regarding light intensity, sound etc.
The sqr root of 2 = ~1.414, so just multiply the preceding number in the ascending sequence (or for descending use half, or * 0.707).
Those numbers are integers, of course. You'll either have to use a "float" variable and cast it as an integer, or you'll have to use "fixed point" arithmetic (look it up).
(you like to "abstractise" words, too... ;-)
Thanks! :D
Tables work well, but some uC compilers automatically copy all flash ROM tables to RAM even if they are static, rather than accessing the ROM directly. Depends on the chip and the compiler, of course. If it's a large table, that could be an issue.
![]() |















Vancouver Mini Maker Faire 2012
Rebuilding NordicTrack ski machine drive rollers
Looking for New Zealand-based Instructables authors for conference on August 27 in Wellington
Call to makers - Brighton Mini Maker Faire
Milk Crates - not as green as you think
TEDxBaghdad - Iraq - violence, dust storms and open sourced manufacturing
UK Mini Maker Faire - The Derby Silk Mill - New Poster to Share!







