1.2 kW two-stage interleaved MPPT solar charge controller: STM32G474 HRTIM firmware and KiCad hardware
  • HTML 59.6%
  • C++ 35.5%
  • C 4.5%
  • Linker Script 0.4%
Find a file
vratk0529-laptop-arch 88d7787c82 Prepare for publishing: MIT license, KiCad sources, safety disclaimer
- LICENSE (MIT) and THIRD_PARTY_NOTICES.md (ST variant files BSD-3, KiCad
  libraries, easyeda2kicad parts, iBOM's embedded JS).
- hardware/: KiCad projects for the control board and the power stage,
  with interactive BOMs and fab outputs (backups and .kicad_prl left out).
- README: hardware/ and license sections; the safety section now says the
  hardware has known issues and that there is no warranty of any kind.
- Victron's VE.Direct spec is linked instead of shipped; docs/hardware.md
  points at hardware/ instead of local paths.
- .gitignore: .vscode/, the Victron PDF, KiCad per-user and backup files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:31:19 +01:00
boards Initial commit 2026-08-06 16:16:13 +02:00
docs Prepare for publishing: MIT license, KiCad sources, safety disclaimer 2026-11-11 13:31:19 +01:00
hardware Prepare for publishing: MIT license, KiCad sources, safety disclaimer 2026-11-11 13:31:19 +01:00
include Prepare for publishing: MIT license, KiCad sources, safety disclaimer 2026-11-11 13:31:19 +01:00
lib Initial commit 2026-08-06 16:16:13 +02:00
src Safety review fixes: fast OVP, retry limit, atomic compares, sensing checks 2026-11-11 11:38:42 +01:00
test Initial commit 2026-08-06 16:16:13 +02:00
variants/STM32G474VET6 Initial commit 2026-08-06 16:16:13 +02:00
.gitignore Prepare for publishing: MIT license, KiCad sources, safety disclaimer 2026-11-11 13:31:19 +01:00
CLAUDE.md Prepare for publishing: MIT license, KiCad sources, safety disclaimer 2026-11-11 13:31:19 +01:00
LICENSE Prepare for publishing: MIT license, KiCad sources, safety disclaimer 2026-11-11 13:31:19 +01:00
platformio.ini Safety review fixes: fast OVP, retry limit, atomic compares, sensing checks 2026-11-11 11:38:42 +01:00
README.md Prepare for publishing: MIT license, KiCad sources, safety disclaimer 2026-11-11 13:31:19 +01:00
THIRD_PARTY_NOTICES.md Prepare for publishing: MIT license, KiCad sources, safety disclaimer 2026-11-11 13:31:19 +01:00

Proper-MPPT

Firmware for a home-built 1.2 kW MPPT solar charge controller: an STM32G474 driving a two-stage, 180°-interleaved synchronous buck converter through the high-resolution timer (HRTIM). It runs unattended on a real array, charging a 12 V LiFePO₄ pack.

The installation

Two power stages on a shared heatsink, with the control board in front. There is no enclosure yet.

What it does

  • Maximum power point tracking: perturb-and-observe with power averaging and Vin-excursion reversal. A periodic global sweep gets past flat regions of the P-V curve that P&O cannot cross, and CV-MPPT handles low light.
  • Multi-stage charging: BULK → ABSORPTION → FLOAT, with per-chemistry profiles (lead acid and LiFePO₄, 12/24/48 V) and cable-drop compensation.
  • Every limit is a duty ceiling. The applied duty is the minimum of the tracker's request and every limit loop: CV, Vcap, average current, soft peak current, feed-forward, Vin floor, and three thermal foldbacks.
  • Hardware protection on the HRTIM: a cycle-by-cycle peak-current trip, and diode emulation (a zero-cross comparator turns the low side off) with no CPU in the loop.
  • Fault handling for unattended use: environmental faults retry forever, transient faults retry a few times, hardware faults latch, and closing the remote input clears them. An independent watchdog is fed only while the control loop is ticking.
  • Outputs: a 1 Mbaud RS232 console with a 5 Hz status line, plus Victron-compatible VE.Direct text output on a second UART.
  • Fans: PWM speed control with tachometer stall detection.

Hardware

MCU STM32G474VET6 (Cortex-M4F, 170 MHz), on a custom control board
Power stage 2 × 50 A synchronous buck stages: IRFP4668 (200 V), UCC21520 isolated gate driver, INA240A3 inductor-current sense. 100 kHz, interleaved 180°
Input 3 series panels, ~126 V Voc (~140 V at −10 °C), ~1200 W (board rated 150 V in)
Output 12 V LiFePO₄, 314 Ah
Board design KiCad 10, 4 layers. Control board 75 × 50 mm, power stage 75 × 100 mm
Control board Power stage
Control board Power stage

The boards carry modifications that are not in the schematics: the output disconnect is removed and bridged, the gate harnesses are crossed, and some resistors were changed. Read docs/hardware.md before touching the hardware. The KiCad projects (schematics, PCBs, interactive BOMs and fab outputs) are in hardware/.

Building and flashing

PlatformIO, environment stm (STM32duino with the Cube HAL), flashed over a Black Magic Probe:

pio run -e stm                 # build
pio run -e stm -t upload       # flash (set upload_port in platformio.ini)

Behaviour is configured with -D flags in platformio.ini, without editing the source:

Define Default Meaning
AUTO_START 1 Start charging on boot. Set it to 0 for bench work, and the board then waits for e
REMOTE_ENFORCE 1 The remote on/off input (PB12) must be closed to run. An unwired connector reads as off
DEFAULT_PROFILE LIFEPO4_12V Battery profile at boot. It must match the connected battery
MPPT_BRINGUP / IPEAK_TRIP_BRINGUP_A 1 / 50 A Per-stage peak-current trip during staged commissioning. MPPT_BRINGUP=0 selects the final 55 A

Every hardware-derived constant (dividers, shunt, inductance, dead time, thresholds) lives in include/board_config.h, each with its derivation. Nothing else may hard-code a scaling factor.

Using it

Connect to the RS232 port (RJ45) at 1 000 000 baud. Single-letter commands:

e / d enable / disable the converter
c clear latched faults
l / p<n> list profiles / select one (only while off)
w run an MPP sweep now
x<duty> manual duty override (every ceiling still applies). x alone releases it
s toggle the status stream
? command list
<n>g<duty>, <n>z per-stage gate test and current-zero calibration (converter off)

The full command list, every status-line field and every fault code are in docs/telemetry.md.

Repository layout

Path Contents
src/main.cpp Clock setup, init order, control ISR dispatch, watchdog, auto-start and remote policy
src/mppt.cpp The controller: modes, limit loops, tracker, sweep, charge staging, faults
src/phase.cpp One power stage's HRTIM compares, gate enable, comparators, and the duty fan-out
src/sensing.cpp Per-stage ADC/DMA statistics and the combined converter view
src/hal.cpp, src/sys.cpp Cube-style peripheral and MSP init
src/{fans,remote,telemetry,vedirect}.cpp Fans, remote input, console and status line, VE.Direct
include/board_config.h All hardware constants and their derivations, with static_asserts
include/battery.h Charge profiles and pack capacity/rating
include/pins.h Pin map
boards/, variants/ PlatformIO board and variant definitions for the STM32G474VET6
hardware/ KiCad projects for the control board and the power stage

Documentation

Document
docs/hardware.md Both boards: pinouts, harness, power tree, as-built modifications, known issues
docs/telemetry.md Console commands, status line, fault meanings
docs/vedirect.md VE.Direct output fields (spec: Victron's VE.Direct Protocol 3.34)
docs/tracker-diagnosis.md Tracker invariants and how to debug it
docs/history.md The engineering log: every bug, measurement and design reversal, numbered §1–§45. Source comments cite it by section
CLAUDE.md Current-state summary and the invariants that must not be broken

Status

Working, unattended, as the primary charger. Open items include current sharing between the two stages (2.24:1, §41), the missing heatsink NTC, and refitting an output disconnect. The full list is under Open issues and Next steps in CLAUDE.md.

Safety

This is a hobby project handling ~150 V DC and 50+ A. Panel voltage is present whenever there is daylight, and the battery can deliver far more current than anything here is rated for. The output disconnect is currently bypassed, so the battery fuse is the only output protection. Do not reproduce this without understanding docs/hardware.md and the Invariants section of CLAUDE.md.

The hardware has known issues. The boards in hardware/ do not match what is running: parts failed and were bypassed, a harness is crossed to fix a PCB error, and the current sharing, gate-driver temperature and missing heatsink sensor are still open (see Open issues in CLAUDE.md). The newest firmware changes have not been tested on hardware.

There is absolutely no warranty on the hardware or the software. They are provided as is, as the license says. If you decide to build this and it burns your house down, destroys your battery, your panels or anything else, or hurts you or anyone else, I am not responsible. You use any of it entirely at your own risk.

License

MIT, for the firmware, the documentation and the hardware designs. Vendored third-party files keep their own licenses; see THIRD_PARTY_NOTICES.md.