# Battery charger: constant current, then constant voltage

A lithium-ion pack is charged in two stages. While the pack is low the charger pushes a fixed current (CC); when the pack voltage reaches its limit the charger holds that voltage (CV) and the current tapers away. Here you open a buck charger with two nested loops, read the hand-over on the scope, and change the current limit to see when it moves. About 10 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_wpt_charger_mppt.rs`, which run the same example. The pack is four cells with a capacity of 1 mAh, far smaller than any real pack: that makes the charge move in a fraction of a second instead of hours. For a real 4.5 Ah pack see [tutorial 22](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/22-long-battery-run/). All parameters are illustrative.

## 1. Open the charger

Choose `File › Open Example › DC-DC › Battery charger, constant current then constant voltage`.

A synchronous buck from 24 V charges four cells in series (`B1`, 15.3 V at 70 % charge). The inner loop makes the inductor current `il` follow a reference `iref`; the outer loop compares the filtered pack voltage with the target `Vcv` (16.6 V, 4.15 V a cell) and its PI gives `iref`, limited between 0 and `Icc` (5 A). While the pack is below the target the error is large and `iref` sits on its 5 A limit: constant current. Near the target the PI leaves the limit and the current falls: constant voltage.

## 2. Run it

Press `F5` (0.6 s). Read `ibat` and `vbat` on the scope:

- In the first 0.1 s the current is held at <!-- run:chg.icc5.cc -->5.00 A<!-- /run -->, the limit `Icc`.
- The current reference `iref` leaves the limit at <!-- run:chg.icc5.tcv -->150 ms<!-- /run -->: that is the hand-over from CC to CV.
- In the last 50 ms the pack voltage is <!-- run:chg.icc5.vcv -->16.61 V<!-- /run -->, held at the target, the current has tapered to <!-- run:chg.icc5.iend -->0.01 A<!-- /run -->, and the charge `soc` has reached <!-- run:chg.icc5.soc -->95.6 %<!-- /run -->.

The current falls in CV because the pack's rest voltage keeps rising towards the target, so less and less current is needed to hold the terminal voltage.

## 3. Change the current limit

Under `Circuit › Variables` set `Icc` to 2.5 and run again. The held current is now <!-- run:chg.icc25.cc -->2.50 A<!-- /run -->, and since the pack climbs to the target at half the rate, the hand-over moves from <!-- run:chg.icc5.tcv -->150 ms<!-- /run --> to <!-- run:chg.icc25.tcv -->330 ms<!-- /run -->. The end of the run shows <!-- run:chg.icc25.vcv -->16.60 V<!-- /run --> and <!-- run:chg.icc25.iend -->0.01 A<!-- /run -->, with the charge at <!-- run:chg.icc25.soc -->95.5 %<!-- /run -->: a lower limit charges more gently but takes longer to reach the same state.

Next: [tutorial 26](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/26-solar-mppt/) charges a battery from a solar array, with the converter finding the array's best operating point. The [manual](https://voltecx-web.nguyenvanqui291.workers.dev/docs/manual/) lists every command used here.
