How Timer Peripherals Generate PWM for LEDs and Motors

Timer peripherals in microcontrollers generate Pulse-Width Modulation (PWM) signals by utilizing internal counters and comparators to rapidly switch digital output pins between high and low states. This article provides a comprehensive overview of how these hardware timers configure frequency and duty cycle, the specific mechanisms behind compare match registers, and how the resulting variable duty-cycle signals effectively control the brightness of LEDs and the speed of electric motors without overburdening the main processor.

The Core Mechanism of Timer Peripherals

At the heart of PWM generation is the hardware timer peripheral. Unlike software-generated delays that block the central processing unit (CPU), a timer peripheral operates independently in the background. It relies on three primary components to create a PWM signal:

  1. The Counter Register: This register increments (or decrements) continuously based on a clock source. The speed at which it counts is determined by the microcontroller's system clock divided by a programmable prescaler.
  2. The Auto-Reload Register (Period Register): This defines the maximum value the counter can reach before resetting to zero. This value sets the total period, and consequently the frequency, of the PWM signal.
  3. The Capture/Compare Register: This holds a specific threshold value that sits between zero and the Auto-Reload value. It dictates the exact moment the output signal changes state, which establishes the duty cycle.

Generating the Variable Duty Cycle

A PWM signal consists of a repeating cycle where the voltage is high (on) for a fraction of the time and low (off) for the remainder. The percentage of time the signal remains high during one complete period is called the duty cycle.

The timer peripheral generates this signal through a continuous comparison process:

  • Cycle Start: When the timer counter starts at zero, the microcontroller sets the corresponding physical output pin to a logic HIGH state.
  • Compare Match: As the counter increments with each clock tick, comparator hardware constantly checks if the counter value equals the value stored in the Capture/Compare Register. When a match occurs, the hardware immediately forces the output pin to a logic LOW state.
  • Period Reset: The counter continues to increment until it hits the value in the Auto-Reload Register. At this point, an overflow condition occurs. The counter resets back to zero, the output pin is driven HIGH again, and the cycle repeats.

To make the duty cycle variable, the microcontroller's software simply writes a new value to the Capture/Compare Register. If the Auto-Reload Register is set to 100, setting the Compare Register to 25 results in a 25% duty cycle. Changing the Compare Register to 80 dynamically shifts the signal to an 80% duty cycle on the next cycle.

Dimming LEDs with PWM

Light Emitting Diodes (LEDs) are current-driven semiconductor devices. Attempting to dim an LED by reducing its analog voltage results in poor control, color shifting, and sudden drop-offs when the voltage falls below the diode's forward voltage threshold.

Timer-generated PWM solves this by turning the LED fully on and fully off at a frequency too fast for the human eye to detect—typically above 100 Hz, though frequencies in the kilohertz range are commonly used to eliminate any perceptible flicker.

Because the human eye integrates light over time, it perceives the average power delivered to the LED rather than the rapid flashing. When the timer peripheral decreases the Compare Register value, the "on" time shrinks. A 10% duty cycle means the LED is only lit for one-tenth of each cycle, tricking the brain into seeing a dimly lit LED, while a 90% duty cycle appears nearly at full brightness.

Driving Motors with PWM

Electric motors, particularly brushed DC motors, require significant current to overcome inertia and maintain rotation. Using a variable resistor to control motor speed wastes massive amounts of energy as heat. PWM provides a high-efficiency alternative by using the timer output to switch a power transistor (such as a MOSFET) fully on or fully off.

When driving a motor, the physical mass of the rotor and the inductance of the motor windings act as a natural low-pass filter.

  • Mechanical Inertia: The rotor cannot instantly start and stop thousands of times per second. Instead, it responds to the average voltage applied over time. A 50% duty cycle from a 12V supply results in the motor behaving as if it is connected to a continuous 6V source.
  • Inductive Smoothing: The motor's coils resist rapid changes in current. During the "on" phase of the PWM cycle, energy builds up in the magnetic field of the windings. During the "off" phase, this magnetic field collapses and continues to push current through the circuit (Typically routed through a flyback diode), smoothing out the current flow.

For motors, timer peripherals are usually configured to generate PWM frequencies between 15 kHz and 25 kHz. If the frequency is set too low (within the human hearing range of 20 Hz to 20 kHz), the magnetic forces inside the motor will cause the windings to vibrate audibly, resulting in an annoying whining sound. By adjusting the timer's prescaler and Auto-Reload Register, engineers push the PWM frequency into the ultrasonic range, ensuring silent and efficient motor speed control.