# PWM Generator (`pwm`)

> Turns a duty from 0 to 1 into a gate signal.

![pwm](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/img/pwm.svg)

[Component reference](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/) · [繁體中文](https://voltecx-web.nguyenvanqui291.workers.dev/zh/docs/reference/control/pwm/)

**Group:** Control · **Kind:** `{"block":"pwm","kind":"control"}`

## When to use it

Use it for one switch or one half-bridge leg driven by a duty. Use Three-Phase PWM for three phases.

## Pins

| Pin | Domain | Meaning |
|---|---|---|
| `in` | signal | Duty: 0 always off, 1 always on |
| `phase` | signal | Optional carrier phase in degrees; while wired it replaces the `phase` parameter, so a controller can shift one leg against another (phase-shifted full bridge, DAB). |
| `freq` | signal | Optional carrier frequency in Hz; while wired it replaces `freq` and the carrier integrates it. |
| `out` | signal | Gate signal: 1 while duty is above the carrier |

## Common parameters

- `freq`: Carrier frequency; a wired freq input replaces it (Default: required)
- `shape`: Triangle or sawtooth wave shape: `triangle` or `sawtooth` (Default: triangle)
- `phase`: How far the wave is shifted, as a fraction of one period; a wired phase input (degrees) replaces it (Default: 0)

## Parameters

| Key | Unit | Default | Typical range | Description |
|---|---|---|---|---|
| `freq` | Hz | required |  | Carrier frequency; a wired freq input replaces it |
| `shape` | — | triangle |  | Triangle or sawtooth wave shape: `triangle` or `sawtooth` |
| `phase` | — | 0 |  | How far the wave is shifted, as a fraction of one period; a wired phase input (degrees) replaces it |
| `counts` | — | optional (off) |  | Timer counts in one PWM period; the duty rounds to 1/counts (none = exact) |
| `hrpwm` | s | optional (off) |  | High-resolution PWM edge step, or `f28379d` (150 ps) or `stm32g474` (184 ps); the duty rounds to step × freq |
| `offset` | — | 0 |  | Carrier's lowest value, in the input's units: an input at or below it gives a duty of 0 |
| `vpp` | — | 1 |  | Carrier's peak-to-peak value, in the input's units: the duty is (in − offset)/vpp |

_Parameter descriptions are in English in both languages._

**Use for:** Single-switch gates · Half-bridge legs · Phase-shifted bridges

**Not for:** Three-phase inverters → [`pwm3`](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/control/pwm3/) · Firmware timing and update delay → [`mcu`](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/control/mcu/)

## Formulas

| Formula | Equation | Source |
|---|---|---|
| Duty from a control voltage | D = vc/vpk | Erickson & Maksimović, Fundamentals of Power Electronics, 3rd ed. (2020), ch. 7 (modelling the pulse-width modulator) |
| Modulator gain | Gm = 1/vpk | Erickson & Maksimović, Fundamentals of Power Electronics, 3rd ed. (2020), ch. 7 (modelling the pulse-width modulator) |
| On-time per period | ton = D/freq | Holmes & Lipo, Pulse Width Modulation for Power Converters (2003), ch. 3 |
| Average switched output | Vavg = D·vdc | Erickson & Maksimović, Fundamentals of Power Electronics, 3rd ed. (2020), ch. 2 |

## Quick check

Inputs:

- `vc`: Control signal = 400m —
- `vpk`: Carrier peak (1 in this block) = 1 —
- `vdc`: DC-link voltage = 48 V
- `freq`: Switching frequency = 20k Hz, `freq`

Outputs:

- `d`: Duty = `limit(vc/vpk, 0, 1)` —
- `ton`: On-time = `d/freq` s
- `vavg`: Average output = `d*vdc` V

## Model logic

- The carrier runs from 0 to 1 at freq: a triangle rises over the first half period and falls over the second, a sawtooth rises over the whole period; phase shifts it by a fraction of a period.
- The gate is 1 while duty > carrier: with a triangle the on-time is centred on the period start, with a sawtooth it starts there; a wired phase input in degrees replaces phase.
- The duty is clamped to 0 and 1, where the gate stays off or on with no edges; counts or hrpwm round it to the nearest multiple of 1/counts or step·freq.
- The duty is read at every step, not sampled once per period, and the next edge for the present duty is reported so the solver lands a step just after it.

## Model

The output is 1 while `duty > carrier`, with the carrier between 0 and 1 at `freq` Hz (`triangle` by default, or `sawtooth`, shifted by `phase`). With a triangle the on-time is centred on the start of each period; with a sawtooth it starts at the period start. The duty is clamped to 0 ... 1, and the solver lands a step exactly on every edge for a constant duty. Wired `phase` (degrees) and `freq` inputs replace the parameters; unwired, the block is exactly the fixed carrier. A digital PWM can only place an edge on its timer: with `counts` (timer counts per period, TBPRD or ARR) the duty rounds to a multiple of 1/counts, and with `hrpwm` (a high-resolution edge step in seconds, or `f28379d` or `stm32g474`) to a multiple of step × freq. Without them the duty is not rounded, as before.

## Common mistakes

- A duty outside 0 to 1 does not make a wider or negative pulse, so the switch just stays on or off. Keep a Constant or Sine Source reference inside 0 to 1, and centre a sine on 0.5 with an amplitude of at most 0.5.
- A carrier much faster than the step is not resolved, so keep the maximum step at or below one fiftieth of the period. Write freq with its prefix, such as 20k.
- Both switches of a half bridge need a dead time. Make the opposite gate with Logic NOT and put a Dead Time after each gate.

Problems rules: `duty.out_of_range`, `step.too_large`, `param.out_of_range`, `shoot-through`

## Datasheet and sources

The value of freq is the switching frequency you design for, and the duty comes from your controller, for example a PI Controller output limited to 0 to 1. The hrpwm presets: `f28379d` is 150 ps, the typical MEP step size of TI SPRS880P (TMS320F2837xD data sheet) section 6.11.4.1.2 (310 ps maximum); `stm32g474` is 184 ps, from ST DS12288 Rev 4 section 3.24.1 and RM0440 Rev 9 section 28.3.3, Table 231 (prescaler CKPSC = 000, PWM from 83.0 kHz up).

## Example

Open the example with the command `file.openExample` and the name `demo:pwm`.

---
Generated by `cargo xtask reference` from the usage texts and parameter specs. Do not edit by hand.
