# LED drivers: constant current with PWM and analog dimming

An LED wants a current, not a voltage, so an LED driver is a converter that holds a current and lets the string's voltage fall where it will. Here you open two drivers, size the first one with `Design › Design Steps › LED Driver`, see why 1 kHz PWM dimming is a flicker problem and 2 kHz is not, and then dim the second one two ways: the current itself (analog) and the share of time it flows (PWM). About 15 minutes.

`File › Open Example` is a menu path and `F5` a key.

The numbers on this page are checked by the tests of `crates/engine/tests/tutorial_led.rs`, which run the same examples. All LED values in the examples are illustrative: each LED is a 3 V threshold and 1 Ω straight line, not a part.

## 1. Open the buck driver

Choose `File › Open Example › DC-DC › LED driver, buck with PWM dimming`.

48 V feeds a buck whose comparator `Hys` holds the LED current `iled` inside a band of ±50 mA around 0.7 A; no oscillator and no compensator are needed, the band sets the switching frequency. The string `LED1` is eight LEDs, so it sits near 29.6 V. A 1 kHz PWM `Dim` at half duty switches the reference to zero for half of each period, so the current flows in pulses.

## 2. Size it

Choose `Design › Design Steps › LED Driver` and press `[▶ Run]` with every field empty: each input is read from the circuit (the list says which ones came from the circuit and which are defaults). The command is `design.ledDriver {"verify": true}`.

- The switching frequency in regulation is <!-- run:buck.fsw -->243 kHz<!-- /run -->: the inductor's volt-second balance over the band (Erickson and Maksimović, *Fundamentals of Power Electronics*, 3rd ed., §2.2).
- The mean LED current with half-duty dimming is <!-- run:buck.iavg -->0.349 A<!-- /run -->, and the verify run simulates the circuit and finds <!-- run:buck.sim -->0.349 A<!-- /run -->, with a peak of <!-- run:buck.peak -->0.75 A<!-- /run -->, the top of the band.
- A pulse needs time to rise from zero, so a very short pulse never reaches regulation: the shortest dimming duty that does at 1 kHz is <!-- run:buck.dmin -->1.7 %<!-- /run -->.

## 3. Flicker

The same reply shows the flicker: <!-- run:buck.flicker -->100 %<!-- /run -->, because the light goes fully off between pulses. IEEE Std 1789-2015 (Recommended Practices 1 and 2) allows a low-risk flicker of 0.08 times the frequency in percent, which at 1 kHz is <!-- run:buck.limit1k -->80 %<!-- /run -->, so the verify check `flicker` fails and the notes say so. Set `dimFreq` to `2k` and the reply reads <!-- run:buck.low2k -->low risk<!-- /run -->: above 1250 Hz the standard sets no limit, so the same pulses now pass. Dimming above 1250 Hz is the rule; the other way out is analog dimming, below.

`apply` writes the design into the circuit in one history step, which `Edit › Undo` takes back.

## 4. Open the boost driver

Choose `File › Open Example › DC-DC › LED driver, boost with analog and PWM dimming`.

12 V feeds a boost onto a string of ten LEDs near 33 V, so this one has to step up. A PI loop sets the switch's duty so `iled` follows the reference `Ifull·Adim` (`Ifull` 0.5 A, `Adim` 0.6). A series switch `Sdim` cuts the string off between dimming pulses; while it is open the loop's integrator is held and the output capacitor keeps the string's voltage, so each pulse starts at the regulated current. Press `F5`: the run is 20 ms, four periods of the 200 Hz dimming.

## 5. Two ways to dim

Read `iled` in the middle of the last pulse, and its mean over the whole last period (`scope.measure`).

| `Adim` | Held current in a pulse | Mean over a period (PWM at 0.5) |
|---|---|---|
| 0.6 | <!-- run:boost.a60.held -->0.307 A<!-- /run --> | <!-- run:boost.a60.mean -->0.150 A<!-- /run --> |
| 0.3 | <!-- run:boost.a30.held -->0.152 A<!-- /run --> | <!-- run:boost.a30.mean -->0.075 A<!-- /run --> |

Change `Adim` under `Circuit › Variables` and the current inside each pulse follows it: that is analog dimming, with no flicker added. Change `Ddim` and the share of time the current flows follows it instead: that is PWM dimming, and the mean is the duty times the held current. Both work together, as the table shows (half the duty on half the current is a quarter of the full current).

The flicker of the PWM part is again 100 %, and at 200 Hz the low-risk limit is <!-- run:boost.limit200 -->16 %<!-- /run -->, so for a 200 Hz dimming the current should be dimmed with `Adim` and PWM kept above 1250 Hz. `design.ledDriver {"topology": "boost"}` reads this circuit, sizes `L1`, `Cout` and the loop gains (with the loop's phase margin against the right-half-plane zero) and, with `apply`, writes them and checks the mean current by simulation.

Next: the [manual](https://voltecx-web.nguyenvanqui291.workers.dev/docs/manual/) lists every field of `design.ledDriver`.
