How Does PWM Vary Duty Cycle to Control Motors and LEDs?

Pulse-Width Modulation (PWM) is a technique used in classical computing and digital electronics to control analog devices using purely digital signals. By rapidly switching a digital square wave between its high and low states, a microcontroller can vary the proportion of time the signal stays on, known as the duty cycle. This rapid switching alters the average power delivered to a load, allowing precise control over motor speed and LED brightness without needing a true analog voltage output.

Understanding Digital Square Waves and Duty Cycle

A digital output pin on a microcontroller or processor typically operates at two discrete voltage levels: a high state (such as \(5\text{ V}\) or \(3.3\text{ V}\)) and a low state (\(0\text{ V}\)). Unlike a digital-to-analog converter (DAC), which can output intermediate voltages, a standard digital pin can only switch fully on or fully off.

PWM overcomes this limitation by generating a periodic square wave with a fixed frequency \(f\) and period \(T = \frac{1}{f}\). Within each period, the signal remains high for a duration \(T_{\text{on}}\) and low for the remaining duration \(T_{\text{off}}\), where \(T = T_{\text{on}} + T_{\text{off}}\).

The duty cycle \(D\) is the percentage of one period in which the signal is active:

\[D = \frac{T_{\text{on}}}{T} \times 100\%\]

By adjusting \(T_{\text{on}}\) relative to \(T\), the average voltage \(V_{\text{avg}}\) delivered over time scales linearly with the duty cycle:

\[V_{\text{avg}} = V_{\text{high}} \times \frac{T_{\text{on}}}{T}\]

For example, a \(5\text{ V}\) signal with a \(50\%\) duty cycle delivers an average voltage of \(2.5\text{ V}\), while a \(25\%\) duty cycle delivers \(1.25\text{ V}\).

Controlling Analog Motor Speed

Direct current (DC) motors respond to the average voltage applied across their terminals, which determines their rotational speed. Because a motor has mechanical inertia and electrical inductance, it cannot instantaneously start and stop in response to high-frequency digital pulses.

When a PWM signal drives a motor (typically through a transistor or motor driver circuit), the motor's windings smooth out the rapid on-off pulses into a steady average current. Increasing the duty cycle increases \(V_{\text{avg}}\), causing the motor to spin faster. Decreasing the duty cycle reduces \(V_{\text{avg}}\) and slows the motor down. Because the switching transistor is either fully on (low resistance) or fully off (zero current), very little energy is wasted as heat compared to using a variable resistor.

Controlling LED Brightness

Light-emitting diodes (LEDs) turn on and off almost instantaneously, but human vision cannot perceive individual flashes above a certain frequency (typically around \(60\text{ Hz}\) to \(100\text{ Hz}\)). PWM exploits this persistence of vision by switching the LED at frequencies of several hundred hertz or kilohertz.

At a \(10\%\) duty cycle, the LED is illuminated for only a tenth of each period, appearing dim to the human eye. At a \(90\%\) duty cycle, it is on for most of the period, appearing nearly at full brightness. Varying the duty cycle provides smooth, linear-looking dimming while keeping the LED's forward current at its optimal operating level during each pulse.

Generating PWM in Classical Computing

In classical computing hardware, PWM signals are usually generated by dedicated hardware timers inside a microcontroller or system-on-chip rather than by the main CPU loop. A counter increments at a fixed clock rate until it reaches a top value that defines the period \(T\). A compare register holds a threshold value corresponding to \(T_{\text{on}}\). Whenever the counter is below the threshold, the output pin stays high; once the counter exceeds the threshold, the pin drops low until the counter resets. Updating the compare register in software changes the duty cycle immediately without interrupting the processor's other tasks.