Due to the impacts of the coronavirus outbreak, we are experiencing longer than normal lead times on certain products. We encourage back-ordering out-of-stock items to receive them as soon as possible.
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 5 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 :(