Fading LEDs via PWM

PWM Theory
           In order to control the actual brightness of the LED we will send it a PWM signal PWM stands for pulse width modulation. Any PWM signal has three core qualities:
  1. Frequency
  2. Duty Cycle
  3. Amplitude

These three qualities tell us what type of PWM signal to expect and we will then be able to project it's effect on our system. Below are a few examples of PWM signals with their associated qualities listed out.

Some PWM Examples

           The type of PWM signal we'll be generating will ultimately look like the pictures above. We'll be shooting for a 60-120 Hz frequency range, with an amplitude of +5v (because our system uses +5v). The duty cycle, however, will vary between 0% (fully-off state) and 100% (fully-on state).

Lighting an LED via PWM
           So, if we send any of the signals seen above into an LED, what do we expect should happen? Well, the LED turns on for the short duration of the pulse. Since we will choose an output frequency of between 60-120 Hz the LED will appear to be a solid color because of persistence of vision. The brightness, however, will be controlled purely by the duty cycle %. The animation below gives you an idea of the effect of different PWM duty cycles input into an LED.

           So, you might be thinking...great! we can control LED brightness by changing the duty cycle, sweet let's get it on!...and you'd be right. There is, however, one small caveat. Light brightness in LEDs does not increase proportionally with duty cycle increases. This means if we varied duty cycle 0% to 100% back and forth the LED would fade in and out, however it would disproportionately be in the 'fully-on' state most of the time. So let's move forward and take a look at how we can use a logarithms to get a linear fade-in and fade-out with our LEDs.