In celebration of the 50th anniversary of Apollo 11's Moon Walk, we are offering $50 off our SparkFun Inventor's Kit for RedBot and the Arduino Engineering Kit while supplies last! Use promo code APOLLO11 at checkout until 11:59 p.m. MT to get yours today!
Track My Order
Frequently Asked Questions
International Shipping Info
Mon-Fri, 9am to 12pm and
1pm to 5pm U.S. Mountain Time:
Chat With Us
April 27, 2015
Tutorial - Pulse a LED
about 4 years ago
Because of the non-linearity of LEDs when driven by PWM, and also due to non-linearity in how the human eye percieves brightness, I found this fading effect to be not quite optimal. While mathematically correct, it didn't completely LOOK sinusoidal. What appears is that the LED remains in the region of brightness, for about twice as long as in the region of darkness.
Searching through the internet, I found various ways to compensate for this, for example (http://electronics.stackexchange.com/questions/1983/correcting-for-non-linear-brightness-in-leds-when-using-pwm), shows several possible ways to compensate, as well as another link to another method (using logarithmic functions).
Since I had very limited resources with the attiny45, I had to figure out something small and easy.
What I found satisfactory for my purposes, was to modify the variable "in" within the FOR loop.
After the analogWrite command, I simply added the line:
in = in + .001 * out /255;
Basically, that makes the loop step through the sinusoid quicker as the PWM value gets higher, so at the bottom, with the led dim or off, "in" is incremented by a very small amount or zero, in addition to the increment provided by the FOR loop. As the value rises, "in" is incremented faster and faster, up to a total of 2 times the normal amount.
For me, this produced a more even looking fade in and fade out effect. You will have to play around with the increment amount in the FOR loop to get the timing right for your purposes.
No public wish lists :(
Forgot your password?
No account? Register one!