# PWM explained simply: carrier, duty cycle, and what the filter sees

Almost every power converter does the same trick: it cannot make an in-between voltage directly, so it switches between two voltages it does have, very fast, and lets a filter average the result. The pattern of on and off times is pulse-width modulation, PWM. This article builds PWM up from a light switch to the sine PWM of an inverter, with every waveform taken from a Voltecx simulation and every number checked against the formula.

## The idea: a light switch flicked very fast

Imagine a lamp on a switch. Flick it on for one second and off for three, again and again. Your eye sees a lamp that blinks. Now do the same a hundred thousand times a second: on for 2.5 µs, off for 7.5 µs. Your eye can no longer follow, and the lamp looks steady at a quarter of its full brightness.

That is all PWM is:

- A **switch** that is either fully on or fully off. A transistor that is fully on or fully off wastes very little power, which is why converters work this way and not like a variable resistor.
- A **period** $T_s$ that repeats at the **switching frequency** $f_{sw} = 1/T_s$.
- A **duty cycle** $D$: the fraction of each period the switch is on.
- A **filter** (the inductor and capacitor in a converter, the eye in the lamp) that is too slow to follow each pulse and so passes on only the average.

## Carrier and reference: how the pulses are made

How does a controller decide when to turn the switch off? The classic method compares two signals:

- the **carrier**, a fast repeating ramp that runs from 0 to 1 once per period;
- the **reference** (or modulating signal), here simply the duty cycle we want, a number between 0 and 1.

The gate is on while the reference is above the carrier and off while it is below. With a sawtooth carrier, the ramp starts at 0 at the beginning of each period, so the switch turns on at the start and turns off when the ramp crosses the reference, after $D \cdot T_s$.

The textbook buck example in Voltecx, `buckIdeal`, does exactly this: a constant `D = 0.25` feeds a PWM block at 100 kHz, which drives the switch of a buck converter from 48 V into a 2.4 Ω load.

![The buck example in Voltecx: a constant duty D feeds the PWM block, whose gate signal drives the switch S1; the diode D1, L1 (100 µH) and C1 (100 µF) filter the switch node into R1 (2.4 Ω).](https://voltecx-web.nguyenvanqui291.workers.dev/media/blog/pwm-explained/buck.svg)

![Three switching periods of the buck in Voltecx: the sawtooth carrier and the duty of 0.25 (top), the gate g (middle), and the switch-node voltage v(sw) with the output voltage v(out) (bottom).](https://voltecx-web.nguyenvanqui291.workers.dev/media/blog/pwm-explained/buck-pwm.svg)

Read the figure from top to bottom. Where the red duty line is above the blue ramp, the gate is 1. The switch node $v_{sw}$ copies the gate: 48 V while the switch is on, about 0 V while the diode carries the current. The output is flat at 12 V.

## Duty cycle and the average voltage

The switch node is a rectangular wave between 0 and $V_{in}$. Its average over one period is the height times the fraction of time it is high:

$$\overline{v_{sw}} = \frac{1}{T_s}\int_0^{T_s} v_{sw}\,dt = \frac{V_{in} \cdot D\,T_s}{T_s} = D\,V_{in}$$

An ideal inductor cannot hold a DC voltage across it in steady state (its average voltage must be zero, or its current would grow without limit), so the average of the switch node appears at the output:

$$V_{out} = D\,V_{in} = 0.25 \times 48\ \text{V} = 12\ \text{V}$$

This is the buck converter's conversion ratio, derived this way in R. W. Erickson and D. Maksimović, *Fundamentals of Power Electronics*, 3rd ed., chapter 2 (the volt-second balance).

**In the simulation:** over the last ten periods of a 4 ms run, the switch node averages 12.0 V and the output 12.0 V (11.99 V, the switch and diode each have 1 mΩ).

The important intuition: **the duty cycle is a knob on the average voltage.** A controller that wants more output raises $D$; one that wants less lowers it. Everything else in this article is about the price of making that average out of pulses.

## The price of pulses: ripple

The filter does not average perfectly. While the switch is on, the inductor sees $V_{in} - V_{out}$ and its current ramps up; while it is off, it sees $-V_{out}$ and ramps down. The peak-to-peak current ripple is

$$\Delta i_L = \frac{(V_{in} - V_{out})\,D}{L\,f_{sw}}$$

and that triangular current, flowing into the output capacitor, leaves a small voltage ripple of

$$\Delta v_{out} \approx \frac{\Delta i_L}{8\,C\,f_{sw}}$$

(both from Erickson and Maksimović, chapter 2, for a capacitor without ESR).

### Worked example

For the buck example at 100 kHz:

$$\Delta i_L = \frac{(48 - 12) \times 0.25}{100\ \mu\text{H} \times 100\ \text{kHz}} = 0.90\ \text{A}$$

$$\Delta v_{out} = \frac{0.90}{8 \times 100\ \mu\text{F} \times 100\ \text{kHz}} = 11.3\ \text{mV}$$

The simulation gives 0.90 A and 11.3 mV.

### Ripple against switching frequency

Both formulas have $f_{sw}$ in the denominator, and the voltage ripple has it twice. Double the frequency and the current ripple halves while the voltage ripple falls to a quarter. That is why converters keep moving to higher frequencies: the same ripple needs a smaller inductor and capacitor.

![Inductor current ripple and output voltage ripple of the buck example against the switching frequency, from 25 kHz to 400 kHz: the formulas and nine Voltecx runs.](https://voltecx-web.nguyenvanqui291.workers.dev/media/blog/pwm-explained/ripple-vs-fsw.svg)

In nine runs from 25 kHz to 400 kHz, the simulated ripple stays within 2 % of both formulas: from 3.6 A and 180 mV at 25 kHz down to 0.22 A and 0.70 mV at 400 kHz.

The catch, covered in [Choosing a switching frequency](https://voltecx-web.nguyenvanqui291.workers.dev/blog/choosing-switching-frequency), is that every switching edge costs energy, so switching losses grow in proportion to $f_{sw}$. PWM frequency is always a trade between filter size and switching loss.

## Sawtooth or triangle: where in the period the pulse sits

The carrier does not have to be a sawtooth. A **triangle** carrier ramps up for half the period and down for the other half. Compared with the same reference, it gives the same duty cycle, but the pulse sits in the middle of the period instead of at its start. This is called **centre-aligned** (or up-down) PWM; the sawtooth version is **edge-aligned**.

![The same duty of 0.25 compared with a sawtooth carrier (top two panels) and a triangle carrier (bottom two): the gate is on for the same 2.5 µs per period, at the start of the period or centred on the carrier's valley.](https://voltecx-web.nguyenvanqui291.workers.dev/media/blog/pwm-explained/sawtooth-vs-triangle.svg)

Why would anyone care where the pulse sits?

- **Current sampling.** With a triangle carrier, the middle of the on-time and the middle of the off-time fall on the carrier's valley and peak. At those two instants the inductor current is exactly at its average value, because the ripple is a symmetric triangle around it. A controller that samples its ADC at the carrier's valley reads the average current with no filter and no delay. In the simulation, the inductor current at each valley and peak equals its mean (5.00 A) to within 0.02 %.
- **Multiple legs.** In a full bridge or a three-phase inverter, centre-aligned PWM keeps the pulses of different legs symmetric, which reduces the ripple and the harmonics.
- **Edges spread out.** With edge-aligned PWM all legs turn on at the same instant, at the start of each period; centre-aligned PWM moves the turn-on edges apart.

Most motor-drive and inverter microcontrollers offer an "up-down" counter mode for exactly this reason.

## Digital PWM: a counter, not a ramp

In a microcontroller the carrier is not a smooth ramp but a **timer counter**. It counts clock ticks from 0 to a period value $N$ and starts again; the compare register holds the duty in counts. The duty can therefore only take the values $0, 1/N, 2/N, \dots, 1$:

$$N = \frac{f_{clk}}{f_{sw}} \quad\text{(edge-aligned)}, \qquad N = \frac{f_{clk}}{2 f_{sw}} \quad\text{(centre-aligned, up-down)}$$

The resolution in bits is $\log_2 N$, and the smallest step of the buck's average output is $V_{in}/N$.

| Timer clock | $f_{sw}$ | Counts $N$ (edge-aligned) | Resolution | Output step of the 48 V buck |
|---|---|---|---|---|
| 100 MHz | 20 kHz | 5000 | 12.3 bits | 9.6 mV |
| 100 MHz | 100 kHz | 1000 | 10.0 bits | 48 mV |
| 100 MHz | 1 MHz | 100 | 6.6 bits | 0.48 V |

The higher the switching frequency, the coarser the duty. To make the steps visible, the next figure feeds the buck a duty command that falls smoothly from 0.30 to 0.20 over 1 ms and limits the PWM to 20 and to 100 counts per period.

![Mean switch-node voltage of each 10 µs period while the duty command falls from 0.30 to 0.20: the command times 48 V, and the PWM rounding the duty to 20 counts (2.4 V steps) and to 100 counts (0.48 V steps).](https://voltecx-web.nguyenvanqui291.workers.dev/media/blog/pwm-explained/pwm-resolution.svg)

With 20 counts the average can only move in steps of 48 V / 20 = 2.4 V. Between 430 µs and 440 µs the command is 0.26: 20 counts round it to 0.25 (12.0 V), 100 counts keep 0.26 (12.48 V).

Why this matters in practice:

- **Limit cycles.** If the duty step is coarser than what the ADC can measure, a closed loop can never land exactly on its set point. It hunts between two duty values forever, a low-frequency oscillation called a limit cycle. The usual rule is that the PWM resolution must be finer than the ADC's resolution at the output (A. V. Peterchev and S. R. Sanders, "Quantization resolution and limit cycling in digitally controlled PWM converters", *IEEE Transactions on Power Electronics*, vol. 18, no. 1, 2003).
- **High-resolution PWM.** Many controllers add a fine delay line to the timer. TI specifies a typical 150 ps step for the F2837xD (data sheet SPRS880P, section 6.11.4.1.2) and ST 184 ps for the STM32G474's HRTIM (DS12288 Rev 4, section 3.24.1). At 1 MHz, 184 ps is a duty step of 0.000184, about 12.4 bits instead of 6.6.

## From DC to AC: sine PWM in a full bridge

So far the reference was constant, so the output was DC. Now let the reference move **slowly** compared with the carrier. Each period still produces a pulse whose width follows the reference at that moment, so the average voltage follows the reference. Make the reference a 50 Hz sine and the averaged output is a 50 Hz sine. This is **sine PWM** (sine-triangle PWM), the basis of most inverters.

The `fullBridgeSpwm` example is a single-phase inverter: two legs (a and b) across a 400 V link, a triangle carrier at 10 kHz, and an LC filter (1 mH in each leg, 10 µF) into a 50 Ω load. Leg a's duty is $0.5 + 0.4\sin(\omega t)$ and leg b's is its mirror image, $0.5 - 0.4\sin(\omega t)$.

### The modulation index

The ratio of the reference's amplitude to the carrier's half range is the **modulation index** $m$. Here the reference swings ±0.4 around 0.5 on a carrier that spans 1, so $m = 0.4 / 0.5 = 0.8$. Each leg then averages $V_{dc}/2 + m\,(V_{dc}/2)\sin\omega t$, and the voltage between the legs has a fundamental of

$$\hat V_{ab,1} = m\,V_{dc} = 0.8 \times 400\ \text{V} = 320\ \text{V peak} \quad (226\ \text{V rms})$$

as in N. Mohan, T. M. Undeland and W. P. Robbins, *Power Electronics: Converters, Applications and Design*, 3rd ed., chapter 8. As long as $m \le 1$ the relation is linear; above 1 the reference leaves the carrier's range, some periods have no pulse at all, and the output grows more slowly than $m$ (overmodulation).

To see the comparison, the next figure slows the carrier down to 1 kHz, so that each triangle is visible.

![Sine PWM of the full bridge with the carrier slowed to 1 kHz: the triangle carrier and the two leg references da and db (top), and the bridge voltage vab (bottom), whose pulses widen and narrow with the sine.](https://voltecx-web.nguyenvanqui291.workers.dev/media/blog/pwm-explained/sine-pwm-slow-carrier.svg)

At the real 10 kHz the pulses are too dense to see one by one, but the filter output is a clean sine:

![One 50 Hz cycle of the full bridge at 10 kHz: the bridge voltage vab (a dense band of pulses) and the filtered output vo, and the inductor current i(L1) with its switching ripple.](https://voltecx-web.nguyenvanqui291.workers.dev/media/blog/pwm-explained/bridge-and-output.svg)

The simulated fundamental of $v_{ab}$ is 314.2 V. The 6 V missing from 320 V go to the 500 ns dead time (each leg loses $V_{dc}\,t_d\,f_{sw} = 2$ V, see [Dead-time effects](https://voltecx-web.nguyenvanqui291.workers.dev/blog/dead-time-effects)), the 50 mΩ switches and the simulation's 500 ns time step.

### Unipolar and bipolar PWM

A full bridge can be modulated in two ways:

- **Bipolar:** the two legs switch as one. The diagonal switches turn on together, so the bridge puts out only $+V_{dc}$ or $-V_{dc}$.
- **Unipolar:** each leg compares its own reference (the sine and its mirror image) with the carrier. The bridge also uses the state where both legs are at the same rail, so its output steps between $+V_{dc}$, 0 and $-V_{dc}$, and in each half-cycle it only switches between one rail and zero.

Both give the same fundamental (the bipolar run gives 315.1 V). The difference is in the pulses.

![0.6 ms around the positive peak of the output: the bridge voltage and the inductor current with unipolar PWM (the example as it is) and with bipolar PWM (each switch of leg b gated with the diagonal switch of leg a).](https://voltecx-web.nguyenvanqui291.workers.dev/media/blog/pwm-explained/unipolar-vs-bipolar.svg)

With unipolar PWM the bridge pulses at **twice** the carrier frequency and swings by only $V_{dc}$ instead of $2V_{dc}$. The inductor current ripple near the peak is 1.81 A peak to peak, against 3.91 A with bipolar PWM, with the same filter and the same switching frequency.

## What the filter sees: the spectrum

The clearest way to see what PWM does is its spectrum. A Fourier analysis of $v_{ab}$ over one 50 Hz cycle (`analysis.fft` in Voltecx) shows three things:

1. a large line at 50 Hz, the fundamental we wanted;
2. almost nothing at low frequencies: below 2 kHz no line reaches 2 % of the fundamental;
3. groups of lines around multiples of the carrier, at $k\,f_{sw} \pm n\,f_1$.

![Spectrum of the bridge voltage vab over one 50 Hz cycle, up to 45 kHz: unipolar PWM (top) has no group at 10 kHz and its first group at 20 kHz; bipolar PWM (bottom) has a 331 V line at the 10 kHz carrier.](https://voltecx-web.nguyenvanqui291.workers.dev/media/blog/pwm-explained/bridge-spectrum.svg)

- **Bipolar:** the largest unwanted line is the carrier itself, 331 V at 10 kHz. The analytic result for naturally sampled sine PWM, $(4V_{dc}/\pi)\,J_0(m\pi/2)$, gives 327 V, within 1.5 % (D. G. Holmes and T. A. Lipo, *Pulse Width Modulation for Power Converters*, Wiley-IEEE Press, 2003, chapter 3).
- **Unipolar:** the carrier group at 10 kHz cancels between the two legs (under 3 V is left), and the first group sits at 20 kHz, with sidebands of 128 V at both 20.05 kHz and 19.95 kHz.

This is why the spectrum matters more than the waveform: the **filter only has to remove what is far above the fundamental.** The LC filter here resonates at 1.1 kHz, and above that it attenuates by about 40 dB per decade. With bipolar PWM it leaves 4.2 V at 10 kHz on the output; with unipolar PWM only 0.41 V at 20 kHz, ten times less, from the same filter and the same switching losses.

The rule of thumb that follows: put the switching frequency (or, with unipolar PWM, twice it) a decade or more above the filter's resonance, and the resonance a decade or more above the fundamental.

## Practical pitfalls

- **The filter averages, the switches do not.** The switch, the diode and the inductor carry the full pulse current, with peaks of $I_{out} + \Delta i_L/2$. Size them for the peak, not the average.
- **Duty limits.** A real gate driver needs a minimum on-time and off-time, and a bootstrap supply needs the low switch to turn on every period. Near $D = 0$ and $D = 1$ the PWM cannot do what the formula says.
- **Dead time.** A half bridge needs a short gap between turning one switch off and the other on. That gap shifts the average voltage by $V_{dc}\,t_d\,f_{sw}$ per leg, as in the full bridge above.
- **Carrier and reference must not be related by accident.** If the reference contains a component near the carrier frequency, the comparison aliases it down to low frequency. Filter or sample the reference properly.
- **Simulate the edges where they are.** A fixed-step simulation puts each switching edge on its time grid, which adds a timing error of up to one step per edge. Use a step well below the smallest pulse width, or a solver that lands its steps on the PWM edges (the fast solver in Voltecx does).

## Try it in Voltecx

- Open the `buckIdeal` example (`File › Open Example`, DC-DC). Change the variable `D` in `Circuit › Variables` and run again: the output follows $D \cdot 48$ V. Change `fsw` to see the ripple change as in the figure above.
- On the PWM block, `shape` switches between a `sawtooth` and a `triangle` carrier, `counts` rounds the duty to a timer's counts per period, and `hrpwm` rounds it to a high-resolution step (`f28379d` or `stm32g474`).
- A `carrier` block with the same frequency and shape records the carrier itself, so you can plot it against the reference as in the figures.
- Open `fullBridgeSpwm` (Inverters) and run `analysis.fft` on the bridge voltage or on `vo` to see the carrier groups and how much of them the filter leaves. `analysis.paramSweep` over `fsw` or `Mi` gives the ripple or the fundamental against either.

Every figure in this article comes from a Voltecx test that runs these examples with the numbers above, and the test fails if a simulated number quoted here no longer matches the run.
