# Worked example: a buck converter with a PI voltage loop

The `buckClosedLoop` example is the 48 V to 12 V buck with a PI controller setting the duty cycle. By hand, the loop gain crosses 0 dB three times: it falls through it near 373 Hz, rises above it again on the output filter's resonance and falls through it last at 1758 Hz, where the phase margin is about 44°. The run starts from 0 V, settles inside ±1 % of 12 V at 3.3 ms without passing 12 V, and the loop gain measured on the switching circuit falls through 0 dB last at 1751 Hz with 45° of phase margin and 47 dB of gain margin.

## Open it

In the app: **File › Open Example › DC-DC › Buck converter, closed loop**, press **F5**, then **Analysis › Loop Gain** with the signal `duty`. From the command line:

```sh
powercraft-cli run --cmd file.openExample --params '{"name": "buckClosedLoop"}' \
  --cmd simulation.run \
  --cmd analysis.loopGain --params '{"signal": "duty", "fStart": 100, "fStop": "20k", "points": 80}'
```

![The closed-loop buck: the power stage of the buck example, a voltmeter VM on the output, and the control chain Ref (12 V), Err (reference minus output), PI (Kp 0.01, Ki 40, duty limited to 0 to 0.9) and a 100 kHz PWM.](https://voltecx-web.nguyenvanqui291.workers.dev/media/worked-examples/buck-closed-loop-pi/schematic.svg)

The power stage is the one of the [open-loop buck](https://voltecx-web.nguyenvanqui291.workers.dev/worked-examples/buck-converter-48v-to-12v): 48 V in, L1 = 100 µH, C1 = 100 µF, R1 = 2.4 Ω, 100 kHz. The capacitor now starts empty.

## The hand calculation

The duty cycle drives the output through the LC filter. At low frequency the gain from duty to output is $V_{in} = 48$ V per unit of duty. The filter resonates at

$$f_0 = \frac{1}{2\pi\sqrt{L C}} = \frac{1}{2\pi\sqrt{100\ \mu\text{H} \times 100\ \mu\text{F}}} = 1.59\ \text{kHz}, \qquad Q = R\sqrt{\frac{C}{L}} = 2.4$$

The PI controller is $K_p + K_i/s$ with $K_p = 0.01$ and $K_i = 40$, so the loop gain is

$$T(s) = \left(0.01 + \frac{40}{s}\right) \frac{48}{L C s^2 + (L/R)\,s + 1}$$

Well below $f_0$ the filter is flat and $T(j\omega) \approx 48\,(0.01 + 40/(j\omega))$. Setting $|T| = 1$: $(0.48)^2 + (1920/\omega)^2 = 1$ gives $\omega = 2190$ rad/s, a first crossing near **350 Hz** (373 Hz with the filter's rise), where the phase margin is about 115°.

That is not the crossing that matters. Above it the gain keeps falling toward 0.48 (−6.4 dB), but the filter's resonance lifts it by up to $Q = 2.4$: $|T|$ rises above 1 again at 1181 Hz, peaks at +2.1 dB near 1.51 kHz and falls through 1 for the last time at **1758 Hz**. There the filter has already turned the phase most of the way to −180°, so the phase margin is about **44°** (41° with the PWM's half-period delay). The last crossing sets the damping: the closed-loop poles are at $-1418 \pm j11925$ s⁻¹, ζ ≈ 0.12, so a load step rings at about 1.9 kHz.

## The run

The example runs for 10 ms from an empty capacitor.

![Start-up over the 10 ms run: v(out) rises to the 12 V reference, ringing at the LC resonance on the way, while the duty climbs to 0.25.](https://voltecx-web.nguyenvanqui291.workers.dev/media/worked-examples/buck-closed-loop-pi/start-up.svg)

| Quantity | Hand | Run |
| --- | --- | --- |
| Settled output (last ten periods) | 12 V | 12.00 V |
| Settled duty cycle | 0.25 | 0.250 |
| Time into ±1 % of 12 V | — | 3.3 ms |
| Peak output | — | below 12.01 V |
| 0 dB crossings | 3 | 3 |
| Last fall through 0 dB | 1758 Hz | 1751 Hz |
| Phase margin there | 44.5° | 45° |
| Gain margin | — | 47 dB |

![The loop gain T measured by analysis.loopGain on the switching circuit: the magnitude falls through 0 dB near 375 Hz, rises above it on the LC resonance and falls through it last at 1751 Hz (the vertical line), where the phase is 45° from −180°.](https://voltecx-web.nguyenvanqui291.workers.dev/media/worked-examples/buck-closed-loop-pi/loop-gain.svg)

`analysis.loopGain` reads the margins at the last fall through 0 dB and says how many crossings it found (`crossings`, and a warning). Sweep densely around the resonance: the phase turns by about 90° between 1.3 and 2 kHz, so the default 50-point sweep from 10 Hz to 100 kHz reads 52° there, and a 12-point sweep from 100 Hz to 20 kHz steps over the resonance and finds only the first crossing. The ringing at start-up is that same resonance, excited while the duty cycle climbs; the integrator is slow, so it lets the output ring and then pulls it in.

`analysis.loopGain` injects a small perturbation into the `duty` signal of the switching circuit and measures the response at each frequency. It needs no averaged model, so the result includes the PWM and anything else the circuit holds.

## Try next

- Halve Kp to 0.005: the resonance hump stays below 0 dB, and by hand the loop crosses once, near 330 Hz with about 100° of margin. Double it to 0.02: one crossing near 2.1 kHz with only about 27°. Run the loop gain again and compare.
- Change the load R1 to 24 Ω (0.5 A): Q rises to 24 and the resonance peak grows. Check the gain margin.
- Read [how to design the PI for a buck](https://voltecx-web.nguyenvanqui291.workers.dev/blog/buck-converter-pi-compensator-design) for the full procedure.

Every number on this page comes from a run that a test repeats (`auto::tests_worked_examples`), so the page changes when the simulator does.
