Theory (PWM)

Pulse Width Modulation (or PWM) is a technique for controlling power.  We also use it here to control the brightness of each of the LEDs. 

The diagram below shows the signal from one of the PWM pins on the Arduino.
Roughly every 1/500 of a second, the PWM output will produce a pulse. The length of this pulse is controlled by the 'analogWrite' function. So 'analogWrite(0)' will not produce any pulse at all and 'analogWrite(255)' will produce a pulse that lasts all the way until the next pulse is due, so that the output is actually on all the time.

If we specify a value in the analogWrite that is somewhere in between 0 and 255 then we will produce a pulse. If the output pulse is only high for 5% of the time then whatever we are driving will only receive 5% of full power. 

If however the output is at 5V for 90% of the time then the load will get 90% of the power delivered to it. We cannot see the LEDs turning on and off at that speed, so to us, it just looks like the brightness is changing. 
Last updated on 2015-05-04 at 04.27.11 PM Published on 2012-11-30 at 08.57.50 AM