Pulse Width Modulation, or PWM is perhaps the next logical extension of a binary off/on system. Instead of only 0 or 1, what can we do with a signal that changes between these states with a carefully controlled timing scheme. In fact, there are many useful applications of PWM, some of which you probably observe everyday.
PWM Frequency and Duty Cycle
There are two parameters which full describe a PWM signal; Frequency and Duty Cycle (Duty).
A PWM signal can be thought of as a pulse train, a recurring sequence of on/off pulses, with the frequency related to the time between rising pulse edges (the period T = 1/f). The duty cycle is the percentage of time the pulse is on, compared to the period width. This is demonstrated in the figure below.
Pretty simple, right?
One of the main advantages of a PWM signal is that it can provide an average voltage less than that of the supply voltage. For example, a 5V digital output could supply a load with either 0V or 5V average voltages (just on or off), while a 5V PWM output could "chop up" and provide anywhere between 0V-5V average voltage, dependent on the Duty. The relationship is
where D is the Duty as a proportion (e.g D=0.5 for 50%). This concept of voltage average is very important in applications for PWM
One very common application of PWM is for dimming LED's. If you remember back the the the post on LED's, you'll remember that we had to carefully select a current limiting resistor based on the supply voltage. So we can't easily use a lower DC voltage to dim the LED. Using a PWM signal effectively turns the LED on and off very fast, much faster than our eyes can perceive, and so we see it as a dimmer light (pretty much in proportion to the Duty).
It is also very common to control DC motors with a PWM signal, where their speed is proportional to the Duty D. You'll also see PWM appear in servo motor control, since the internal control chip of the servo sets the angle based on the Duty and frequency of the incoming PWM signal.
How to generate a PWM signal
By far the most common way to create a PWM signal is to use a microcontroller, such as an Arduino. On an Arduino, selected GPIO pins are capable of producing a PWM signal (look for the ~ symbol!). In software you would something like
to get a 50% duty signal on pin 3. Note that it is the second argument (127 in this case) that defines the duty, as an 8 bit integer d=D/255. So 0, 64, 255 define duties of 0%, 25% and 100% respectively.
There are other methods of PWM generation, such as dedicated IC's, or you can get a bit more fundamental and build your own with a 555 timer.