Pulse-Width Modulation: Duty Cycle, Timers, and Applications

жовт. 21 2025
Source: DiGi-Electronics
Browse: 1199

Pulse-width modulation (PWM) is method microcontrollers use to control power by switching signals on and off at high speed. It is used in LEDs, motors, servos, audio, and power systems. This article explains PWM basics, duty cycle, timer operation, modes, frequency, resolution, and advanced techniques in clear detail.

Figure 1: Pulse-width Modulation

Pulse-width Modulation (PWM) Overview 

PWM timers are built-in hardware modules inside microcontrollers that generate digital pulse signals with adjustable duty cycles. Instead of relying on software to toggle pins, which consumes processing power and risks timing jitter, the microcontroller offloads this job to the hardware timer. This allows it to maintain accuracy while freeing the CPU to handle other tasks. The result is efficient multitasking, reduced latency, and better performance in actual applications such as motor control, LED dimming, audio modulation, and signal generation. PWM’s efficiency and precision make it the backbone of modern embedded systems, bridging the gap between digital control and analog behavior. 

Pulse-width Modulation Duty Cycle

Figure 2: Pulse-width Modulation Duty Cycle

The waveform shows a repeating signal that switches between 0V and 5V. The period is marked as 10 ms, which represents the time for one complete cycle. Within that period, the signal stays high (5V) for 3 ms, known as the pulse width. The duty cycle is then calculated as the ratio of the high time to the total period, giving 30% in this case. This means the signal delivers power only 30% of the time per cycle. The frequency is also derived from the period, calculated as 1 ÷ 10 ms = 100 Hz.

Duty Cycle Calculation in Microcontroller Timers

The duty cycle tells us how much of the total time a signal is turned on compared to the full cycle of the waveform. In a microcontroller, this is important because it decides how much power is being sent to a device over each cycle.

To calculate it, you use a simple formula: Duty Cycle (%) = (Pulse Width ÷ Period) × 100. If the signal is active HIGH, the duty cycle is the fraction of time the signal stays HIGH. If the signal is active LOW, the duty cycle is the fraction of time it stays LOW.

Pulse-width Modulation Timer 

Figure 3: Pulse-width Modulation Timer

This image shows how a PWM timer works by linking the voltage output to a counter. The counter repeatedly counts from 0 up to 9, then resets, creating the signal’s period. When the counter reaches a set match value (here, 2), the output goes high and stays high until the counter overflows, defining the pulse width. The overflow point resets the cycle, starting a new period.

The timer determines the duty cycle by controlling when the output turns on (match) and when it resets (overflow). Adjusting the match value changes the width of the high signal, directly controlling how much power the PWM delivers to a load.

Edge-Aligned and Center-Aligned PWM Modes

Edge-Aligned Mode

Figure 4: Edge-Aligned Mode

In edge-aligned PWM, the counter only counts up from zero to a set maximum, and switching occurs at the beginning or end of the cycle. This makes it straightforward to implement and highly efficient since most microcontrollers and timers natively support it. Because all switching edges are aligned to one side of the period, it can lead to uneven current ripple and higher electromagnetic interference (EMI).

Center-Aligned (Phase-Correct) Mode

Figure 5: Center-Aligned (Phase-Correct) Mode

In center-aligned PWM, the counter counts up and then back down within each cycle. This ensures that switching edges are distributed around the center of the waveform, creating a more balanced output. The symmetry reduces harmonics, torque ripple in motors, and EMI in power systems. Although it is slightly more complex and less efficient in terms of frequency utilization, it provides much cleaner output quality.

Selecting the Right PWM Frequency

• LED dimming requires frequencies above 200 Hz to eliminate visible flicker, while display backlighting and high-quality lighting systems often use 20–40 kHz to stay beyond human perception and minimize noise.

• Electric motors operate best with PWM frequencies between 2–20 kHz, balancing switching losses with torque smoothness; lower values provide higher duty cycle resolution, while higher values reduce audible noise and ripple.

• Standard hobby servos rely on fixed control signals around 50 Hz (20 ms period), where pulse width, not frequency, determines angular position.

• Audio generation and digital-to-analog conversion require PWM well above the audible spectrum, over 22 kHz, to prevent interference and allow clean filtering of signals.

• In power electronics, frequency selection often trades off between efficiency, switching losses, electromagnetic interference, and the specific load’s dynamic response.

PWM Resolution and Step Size

Resolution (steps)

The number of discrete duty-cycle levels is set by the timer’s period count (N). For example, if a counter runs from 0 to 1023, that gives 1024 distinct duty-cycle steps. Higher counts mean finer control of output.

Bit-depth

Resolution is often expressed in bits, calculated as log₂(N). A 1024-step counter corresponds to 10-bit resolution, while a 65536 counter corresponds to 16-bit resolution. This defines how precisely the duty cycle can be adjusted.

Time step

The system clock determines the smallest increment, equal to 1 ÷ fClock. Faster clock speeds allow shorter periods and higher PWM frequencies while still maintaining fine resolution.

Trade-offs

Increasing resolution requires more timer counts, which in turn lowers the maximum PWM frequency for a given clock. Conversely, higher frequencies reduce available resolution. 

PWM Prescaler and Period Setup Example

StepCalculationResultExplandetailsation
MCU clock-24 MHzBase frequency driving the timer.
Apply prescaler ÷824 MHz ÷ 83 MHzThe timer clock has been reduced to a manageable counting range.
Timer period3 MHz × 0.020 s60,000 countsSetting the auto-reload/period register to 60,000 gives a 20-ms frame.
Resolution per tick1 ÷ 3 MHz0.333 µsEach timer increment equals\~0.33 microseconds.
Servo pulse control1–2 ms pulse width = 3000–6000 ticksProvides smooth angular control within the 20 ms frame.-

Advanced PWM Channel Techniques

Dead-Time Insertion

Dead-time is a small, controlled delay inserted between the switching of complementary transistors in a half-bridge or full-bridge circuit. Without it, both the high-side and low-side devices could momentarily conduct at the same time, causing a short-circuit known as shoot-through. By adding a few tens or hundreds of nanoseconds of dead-time, the hardware ensures safe transitions, protecting MOSFETs or IGBTs from damage.

Complementary Outputs

Complementary outputs generate two signals that are logical opposites of each other. This is especially useful in push–pull circuits, motor drivers, and inverter stages, where one transistor must turn off precisely when the other turns on. Using complementary PWM pairs simplifies the driver circuitry and ensures symmetry, improving efficiency and reducing distortion.

Synchronous Updates

In systems with multiple PWM channels, synchronous updates enable all outputs to refresh simultaneously. Without this feature, small timing mismatches (skew) could occur, leading to uneven operation. In three-phase motor drives or multi-phase converters, synchronized PWM ensures balance, smooth performance, and reduced electromagnetic interference.

Cross-Triggering

Cross-triggering enables timers to interact with each other, so that one PWM event can start, reset, or adjust another timer. This feature is powerful in advanced control systems, allowing precise coordination of multiple signals. Applications include cascaded motor drives, interleaved power converters, and synchronized sensor sampling, where timing relationships between channels are critical.

Servo Movement with PWM Signals

Pulse WidthServo Movement
\~1.0 msTurns fully to the left or spins clockwise at full speed
\~1.5 msStays in the middle or stops moving
\~2.0 msTurns fully to the right or spins counterclockwise at full speed

Conclusion

PWM is a main tool that lets digital systems control analog devices with accuracy and efficiency. By learning duty cycles, timer setup, frequency choices, resolution trade-offs, and advanced methods like dead-time or gamma correction, you can design reliable systems. PWM continues to support modern electronics in lighting, motion, audio, and power applications.

Frequently Asked Questions [FAQ]

Does PWM improve power efficiency?

Yes. PWM switches devices fully ON or OFF, minimizing heat loss compared to analog voltage control.

Does PWM create electromagnetic interference (EMI)?

Yes. Fast switching generates harmonics that cause EMI. Center-aligned PWM reduces it, and filters help suppress noise.

Why use a low-pass filter with PWM?

A low-pass filter smooths the square wave into an average DC voltage, useful for audio, analog outputs, and sensor simulation.

Can PWM control heating elements?

Yes. Heaters respond slowly, so even low PWM frequencies (10–100 Hz) provide stable temperature control.

What is phase-shifted PWM used for?

It shifts timing between channels to reduce current spikes and balance loads, common in multiphase converters and motor drives.

How do microcontrollers prevent PWM jitter?

They use double-buffered registers and synchronized updates so duty cycle changes apply cleanly at the start of each cycle.