Member #677111

Member Since: April 27, 2015

Country: United States

  • 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 (, 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 :(