Pulse-Width Modulation or PWM is a technique to produce an average voltage on a pin somewhere between fully on (high) and fully off (low) by rapidly pulsing the pin on and off. In this way, it's a little like providing a pseudo-analog output from a digital pin and is used for all sorts of control applications, such as altering the brightness of LEDs, motor speed control, and servo angle control.
PWM is defined by two main characteristics:
- Duty cycle: The percentage of time the pin is high
- Frequency: The time period during which the duty cycle repeats
As illustrated in Figure 5.2 (and for a set frequency), a 50% duty cycle means the pin is high half of the time and low half of the time, while a 25% duty cycle means the pin is high only 25% of the time. And while not pictured, a 0% duty cycle would mean the pin is high 0% of the time (always low), so it's effectively off, while a 100% duty cycle is always high: