# Grid-tie inverter: lock to the grid and control the current

A grid-tie inverter feeds power into the mains. It must follow the grid's phase, push a current that is a clean sine in step with the voltage, and keep doing so when the power asked for changes or the grid voltage sags. You open the single-phase `gridTieInverter` example, read its phase-locked loop (PLL) and current controller, and measure the current against the voltage. Then Auto Design draws a three-phase grid-tie inverter with a dq current loop, and you read its loop, jump the grid's phase, step the active and the reactive power, and sag the grid. About 30 minutes.

Pictures: a numbered red box is what you click or read, `[▶ Run]` is a button, `File › Open Example` a menu path, `⌘⇧Y` a key (`⌘` is Cmd on a Mac, Ctrl elsewhere).

## 1. Open the example

Choose `File › Open Example › Grid › Grid-tie inverter`.

![The File menu open at Open Example, Grid, Grid-tie inverter, marked 1 to 4](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/grid-tie/01-open-example.png)

1. `File`.
2. `Open Example`.
3. `Grid`.
4. `Grid-tie inverter`.

## 2. Read the power stage

A 400 V DC source feeds a full bridge. An LCL filter (an inductor, a capacitor with a damping resistor, and a second inductor) joins the bridge to a 230 V, 50 Hz grid. The nets `oa` and `ob` (the bridge outputs) and the gate signals are drawn as labels, so the picture shows the stage without long wires.

![The power stage: DC source, two legs of two MOSFETs, LCL filter, ammeter, grid source and voltmeter, marked 1 to 7](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/grid-tie/02-power.png)

1. `Vdc`: the 400 V link.
2. `Qah`: the upper switch of the first leg; `Qal` below it, and `Qbh` and `Qbl` in the second leg. Both legs share one carrier and take mirrored duties, so the output has three levels and the ripple is at 40 kHz.
3. `L1`: 2 mH on the bridge side.
4. `Rd`: a 4 Ω damping resistor in series with the filter capacitor.
5. `Cf`: 5 µF. With `L1` and `L2` it resonates at 2.8 kHz; `Rd` damps that peak.
6. `L2`: 1 mH on the grid side. It keeps the 20 kHz and 40 kHz ripple out of the grid current.
7. `Vg`: the grid, 325 V peak (230 V rms) at 50 Hz. The ammeter `AmG` beside `L2` gives the grid current `ig`, and the voltmeter `VMg` gives the grid voltage `vg`.

## 3. Read the control

The control sits below the power stage and meets it through the labels `vg`, `ig` and the gate signals.

![The control: PLL, current reference, proportional-resonant controller, grid feed-forward and the two duty calculations, marked 1 to 5](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/grid-tie/03-control.png)

1. `PLL`: measures the angle of the grid from `vg` and gives `th`.
2. `Ref`: turns `th` and the two constants `Id` (0) and `Iq` (−10 A) into the reference `iref` = 10 A · sin θ, a sine that is in phase with the grid because it uses the grid's own angle.
3. `PR`: the current controller. A plain PI cannot follow a sine without error, so this is a proportional-resonant controller: `Kp` 10 V/A, plus a resonance at 50 Hz with a gain of 1000 V/A. At 50 Hz the loop gain is so high that the current ends on the reference with almost no error in amplitude or phase.
4. `Vff`: adds the measured grid voltage to the controller output, so the controller only supplies the drop across the filter.
5. `Da`: the duty of the first leg, `0.5 + v/(2·Vdc)`; `Db` makes the mirror image for the second leg. Each goes to a PWM block at 20 kHz and, through dead-time blocks, to the gates.

## 4. Run it and compare the current with the voltage

Click `[▶ Run]`, then `Open in Separate Window` at the top right of the scope, so the scope has a window of its own (the next steps use it). The scope shows the grid voltage `vg` on the top axis and the grid current `ig` below it, each on its own axis.

![The scope: vg on top, ig below, both sine waves in phase, marked 1 to 2](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/grid-tie/04-run.png)

1. `vg`: the grid voltage, 325 V peak.
2. `ig`: the grid current. It reaches its 10 A peak within the first cycle (the table says 10.17 A over the whole run, a little more than 10 A during the start) and is a sine in phase with `vg`.

## 5. Choose a window of whole cycles

The scope folds the table away while the plots would get less than half its width, so click `Measurements` in the toolbar to show it. Click `Cursors` (`Shift+C`) and place the two lines at 80 ms and 120 ms: two whole line cycles, after the start.

![The scope with two cursors; the line over the table now says cursors 80ms to 120ms, marked 1 to 2](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/grid-tie/05-window.png)

1. `Cursors`: shows two vertical lines and the values at each.
2. The line over the table now ends with `cursors 80ms – 120ms`. Every figure below is taken over that window.

## 6. Read the distortion of the grid current

Click `Measurements to show` and tick `THD`.

![The measurement picker with THD ticked, marked 1 to 2](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/grid-tie/06-thd.png)

1. `Measurements to show`: the columns of the table.
2. `THD`: the total harmonic distortion up to the 50th harmonic, as a share of the fundamental.

## 7. Read the power factor

Press `Esc` to close the picker. On the scope panel's `Power` tab, choose `vg` in the first list and `i(AmG)` in the second (the ammeter's own current, the same as `ig`), then click `[Add power]`.

![The Power tab with a power row for vg and i(AmG), marked 1 to 2](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/grid-tie/07-power.png)

1. The power row of `vg` and `i(AmG)`. The `THD` column is on the `Summary` and `Signals` tabs: 3.72 % for `ig` (`scope.measure` with the fundamental given as 50 Hz reads 3.60 %), 0.00 % for `vg`, as the grid source is a pure sine.
2. `PF` of the pair: 0.999, from `P` = 1.626 kW and `S` = 1.628 kVA. The distortion alone gives 1/√(1 + 0.0372²) = 0.9993, so the fundamental of the current is in phase with the voltage within a degree (about 0.6°, the current slightly leading). `Q`, the reactive power √(S² − P²) (tick it in the picker's Power group), is 62 var: nearly all of it is the distortion, and the figure has no sign. The expected power is 325 V · 10 A / 2 = 1.63 kW.

## 8. Let Auto Design draw a three-phase grid-tie inverter

The example is single phase. For three phases, Auto Design takes an inverter spec: a 700 V DC link, a 400 V line-to-line grid, 10 kW, and the current distortion limit of IEEE Std 1547. Its form has only DC converter rows, so give the spec on the console (`⌘⇧Y`; first close the scope window with `Dock Back`). A run replaces the open circuit, so save your work first.

```text
design.auto {"spec":{"vdc":700,"grid":{"vll":400},"pout":10000,"thd":"ieee1547"}}
```

![The Auto Design result: PASS, current distortion 0.63 % against 5 %, marked 1 to 2](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/grid-tie/08-auto.png)

1. The headline: `Three-phase two-level inverter, 10 kHz: all 2 checks pass`.
2. `Total rated-current distortion`: 0.63 % against at most 5 %. Below it: power factor 0.997 at the light 10 % load, DC-link ripple 2.65 V (14 V allowed), efficiency 98.8 %.

The reply holds the design. The bridge inductor `L1` is 2.86 mH (20 % current ripple at 10 kHz), the grid-side inductor `L2` 154 µH, the filter capacitor 9.95 µF with a 1.28 Ω damper, resonating at 4.17 kHz, and the DC link 82 µF. The grid-side current loop is a dq PI: `Kp` 12.4 V/A and `Ki` 5118 V/(A·s). It crosses over at 677 Hz with 72° of phase margin and 6.5 dB of gain margin. The PLL has a natural frequency of 15 Hz and a damping of 0.71 (`Kp` 133, `Ki` 8883).

`Design › Design Steps › Current Loop` designs the current loops of a motor from its R and L, and of a grid-tie inverter from its LCL filter. On this design it sees L1 + L2 = 3.02 mH and proposes `Kp` 12.45 V/A and `Ki` 5131 V/(A·s), crossing over at 656 Hz with 72.7° of phase margin and 6.5 dB of gain margin: gains within 1 % of Auto Design's.

## 9. Read the three-phase circuit

Press `Esc` to close the result. The controller is one module, `Ctrl`; entering it (`circuit.enterSubsystem`) shows its blocks.

![The circuit: DC source and link, three legs, an LCL filter per phase, the grid and the controller module, marked 1 to 7](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/grid-tie/09-circuit.png)

1. `Vdc`: the 700 V source, behind a small model of its impedance and the DC-link capacitor.
2. `Qah`: the upper switch of the first of three legs.
3. `L1a`: the bridge-side inductor of phase a; phases b and c repeat the filter below it.
4. `Cfa`: the filter capacitor, with its damping resistor above it.
5. `L2a`: the grid-side inductor. The ammeter `Ama` and the voltmeter `VMa` after it give `ia` and `va`.
6. `Grid`: the three-phase grid, 400 V line to line. It has the parameters `sagDepth`, `sagStart` and `sagEnd` for a voltage sag.
7. `Ctrl`: the controller. Its pins on the left are the measured phase currents, the phase voltages and the DC voltage; on the right the six gate signals.

Inside `Ctrl`, the PLL gives the angle `th`; Park transforms turn the grid voltage into `vd` and `vq` and the current into `idm` and `iqm`; the references are `Idref = Pref/(1.5·Vpk)` and `Iqref = −Qref/(1.5·Vpk)`, so `P = 1.5·vd·id` and `Q = −1.5·vd·iq`. Two PIs add the filter's drop to the measured grid voltage, and space-vector modulation drives the bridge. Two math signals, `P` and `Q`, are added to the scope for the next steps. They take `P = 1.5(vd·id + vq·iq)` and `Q = 1.5(vq·id − vd·iq)`.

## 10. Run it and read the phase current

Click `[▶ Run]`. When the run ends, click `[Show results]` on its notice, then `Open in Separate Window` on the scope, and place the cursors at 80 ms and 120 ms again.

![The scope: phase voltage va and phase current ia, with a table for 80 ms to 120 ms, marked 1 to 2](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/grid-tie/10-run.png)

1. `va`: the phase voltage, 326.6 V peak.
2. `ia`: the phase current, a sine in phase with `va`, 20.5 A peak (14.4 A rms), with a distortion of 0.63 %. The power row for `va` and `ia` reads 3.333 kW and a power factor of 1.000: three phases give 10 kW.

## 11. Jump the grid's phase and watch the PLL lock

Press `⌘⇧Y` to open the console (first click `Dock Back` on the scope). Type these two lines, each followed by `Enter`:

```text
simulation.tune {"component":"Grid","key":"phase","value":120}
simulation.continue {"duration":0.12}
```

![The console with the two commands, marked 1 to 3](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/grid-tie/11-pll-console.png)

1. `Console`: every menu item is a command, and the console runs any of them.
2. `simulation.tune` moves the grid's phase from 90° to 120° at the end of the run, 0.12 s. The inverter's state is kept.
3. `simulation.continue` runs another 0.12 s from there.

Click the `Scope` tab and `Open in Separate Window`.

![The scope after the jump: Ctrl.vq spikes and settles, ia hardly moves, marked 1 to 2](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/grid-tie/12-pll-scope.png)

1. `Ctrl.vq`: the q-axis grid voltage, which is the PLL's error: zero when the PLL is locked. The 30° jump puts it at 327 V · sin 30° = 163 V. It swings to −35 V and is within 2 V of zero 60 ms later, three line cycles, as a PLL with a 15 Hz natural frequency and 0.71 damping settles. A faster PLL would lock sooner and pass more of a distorted grid into the current.
2. `ia`: the phase current. It has a spike of 46 A that lasts a few milliseconds, then goes on with the same 20.5 A peak: the current loop does not wait for the PLL, it follows the angle the PLL has at each moment.

## 12. Step the active power

Type these two lines on the console:

```text
simulation.tune {"component":"Ctrl.IdRef","key":"value","value":10.2}
simulation.continue {"duration":0.08}
```

![The scope after the step: P falls from 10 kW to 5 kW, Ctrl.idm from 20.4 A to 10.2 A, marked 1 to 2](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/grid-tie/13-p-step.png)

1. `P`: between the cursors, 280 ms to 320 ms, it is 4.997 kW. The step took the power from 10 kW down within about 5 ms: the d-axis current is controlled directly, so the active power follows at the speed of the current loop (677 Hz).
2. `Ctrl.idm`: the measured d-axis current, 10.2 A. This is the new reference: `Idref = P/(1.5·Vpk)` = 5000 W / (1.5 · 326.6 V). `IdRef` is a constant block that held the expression `Idref`, and the tune replaces it with 10.2 A.

## 13. Step the reactive power

```text
simulation.tune {"component":"Ctrl.IqRef","key":"value","value":-10.2}
simulation.continue {"duration":0.08}
```

![The scope after the step: P stays at 5 kW, Q rises from 0 to 5 kvar, marked 1 to 2](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/grid-tie/14-q-step.png)

1. `P`: unchanged at 4.997 kW through the step.
2. `Q`: from 0 to 4.997 kvar, a share of the apparent power of 7.07 kVA (a power factor of 0.71), within about 10 ms. A negative `Iqref` gives a positive `Q` in `Q = 1.5(vq·id − vd·iq)`. The phase current grows from 10.2 A to 14.4 A peak, the vector sum of the two axes.

The two axes do not disturb each other: `Q` moves, `P` does not.

## 14. Sag the grid

A grid that drops to half its voltage for 40 ms, starting at 440 ms:

```text
simulation.tune {"component":"Grid","key":"sagEnd","value":0.48}
simulation.tune {"component":"Grid","key":"sagStart","value":0.44}
simulation.tune {"component":"Grid","key":"sagDepth","value":0.5}
simulation.continue {"duration":0.12}
```

(The end is set first because a sag must end after it starts, and both start at 0.)

![The scope through the sag: va halves for 40 ms, ia spikes, P drops to half, marked 1 to 3](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/grid-tie/15-sag.png)

1. `va`: the phase voltage falls from 326.6 V to 163.3 V peak at 440 ms and comes back at 480 ms.
2. `ia`: the grid is an ideal source, so the voltage step reaches the filter capacitors at once, and the phase current spikes to 111 A for a millisecond, five times the rated peak of 20.4 A. The d-axis current is back within 10 ms. Between the cursors, 450 ms to 470 ms, the current is still 14.4 A peak (10.2 A rms).
3. `P`: with the same current and half the voltage, the power is half, 2.499 kW (and `Q` 2.498 kvar). The controller keeps the current, not the power, and it has no current limit. The DC link holds above 695 V throughout.

Auto Design checks the same thing when the spec has `"rideThrough":{"residualPu":0.5,"duration":0.15,"maxCurrentPu":1.5}`: this design passes it, with a bridge-side current of 1.24 per unit against the 1.5 allowed. When a design fails the row, the report says what to try: a current limit in the controller, a higher switching frequency, or a grid impedance.

## The same steps from a script

```sh
powercraft-cli run \
    --cmd file.openExample   --params '{"name":"gridTieInverter"}' \
    --cmd simulation.run     --params '{}' \
    --cmd scope.measure      --params '{"signals":["ig"],"t0":"0.08","t1":"0.12","thd":{"fundamental":50},"power":{"v":"vg","i":"ig"}}'
```

The reply gives `power.powerFactor` 0.9993, `power.real` 1626 and `thd.ig.thdPercent` about 3.6. The three-phase part needs Voltecx Cloud, because `design.auto` runs there:

```sh
powercraft-cli run --new '{}' \
    --cmd design.auto --params '{"spec":{"vdc":700,"grid":{"vll":400},"pout":10000,"thd":"ieee1547"}}' \
    --cmd scope.addMath --params '{"expr":"1.5*(Ctrl.vd*Ctrl.idm+Ctrl.vq*Ctrl.iqm)","name":"P"}' \
    --cmd simulation.run --params '{}' \
    --cmd simulation.tune --params '{"component":"Ctrl.IdRef","key":"value","value":10.2}' \
    --cmd simulation.continue --params '{"duration":0.08}' \
    --cmd scope.measure --params '{"signals":["P"],"t0":"0.14","t1":"0.2"}'
```

The last `scope.measure` gives a `P` of about 5 kW. Add the math signal before the run: a later `scope.addMath` changes the circuit, and `simulation.tune` refuses a circuit that changed since the run.

Back to the [learning path](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/).
