🌐 এই লেখাটি বাংলায় পড়ুন: বাংলা সংস্করণ →

ESP32 MPPT solar charge controller: circuit, design calculations and code

Quick answer: An ESP32 MPPT solar charge controller is a synchronous buck converter between a solar panel and a battery, with an ESP32 adjusting the PWM duty cycle so the panel always works at its maximum power point. The ESP32 measures panel and battery voltage and current, runs a perturb-and-observe (or incremental conductance) algorithm, limits the battery to safe CC/CV charging values and shows everything on a Wi-Fi dashboard.

Advertisements

This is a complete reference design for a 12 V battery and a panel of up to about 250–300 W: power stage, component selection, inductor, capacitor, loss and thermal calculations, sensing, protection, wiring, BOM in BDT, firmware architecture and about half of the code. One honest note: every number here is a design calculation, not a measurement from a tested unit. Measure your own board and report your own results.

Why an ESP32 MPPT solar charge controller is a strong final-year project in 2026

Solar in Bangladesh is moving from small off-grid home systems to rooftop and battery-backed installations. In September 2026 The Financial Express reported that IDCOL is targeting 1,000 MW of solar capacity, with 500 MW planned from a new Domestic Rooftop Solar programme, and that its industrial rooftop programme had already added more than 200 MW to the grid. Every battery-backed system needs a charge controller, and most cheap ones in our market are PWM types that waste much of the panel’s power.

For an EEE student it covers the whole power electronics syllabus on one board (converter, magnetics, MOSFET losses, gate drive, control and protection), gives measurable results for the viva, and can become a product. Many of our power electronics and renewable energy project ideas build on this same converter.

How an MPPT charge controller works

A solar panel behaves like a current source that collapses near its open-circuit voltage. Its P–V curve has one peak, the maximum power point (MPP), which moves all day with irradiance and cell temperature.

The battery fixes the converter’s output voltage. In a buck converter in continuous conduction, Vpv ≈ Vbat / D (D = high-side duty cycle), so the duty cycle sets the panel’s operating voltage. The buck is an adjustable impedance transformer, and the ESP32 tunes D until the panel gives maximum power.

MPPT vs PWM charge controller: the numbers

A PWM controller connects the panel straight to the battery, forcing it to run at battery voltage. For an illustrative 36-cell panel (Vmp ≈ 18 V, Isc ≈ 1.07 Imp) and a 13.5 V battery, PWM gets about 13.5 × 1.07 / 18 ≈ 80% of the available power. A 95%-efficient MPPT gets about 95%, which is roughly 15–20% more energy. With a 60-cell panel (Vmp ≈ 31 V), PWM would get only about 47%.

Perturb and observe (P&O) with real numbers

P&O nudges the duty cycle, measures the new panel power and compares it with the previous sample. If power rose, it keeps going in the same direction; if it fell, it reverses. One step for a 60-cell panel and a 13.2 V battery (illustrative):

SampleVpvIpvPpvDecision
k−129.8 V8.12 A241.98 W—
k30.2 V8.05 A243.11 WdP = +1.13 W, dV = +0.4 V: same sign, so the MPP is at a higher voltage
Action———Raise Vpv by lowering D: 13.2/30.2 = 0.437 → 0.433, so Vpv ≈ 13.2/0.433 = 30.49 V (+0.28 V)

Watch the sign: in a buck, a higher duty cycle pulls the panel voltage down. Getting this wrong is the most common student P&O bug. The 0.4% step (4 PWM counts) is a compromise: bigger steps follow clouds faster but oscillate more around the MPP.

Incremental conductance

At the MPP, dP/dV = 0, which means dI/dV = −I/V. Incremental conductance compares the measured ΔI/ΔV with −I/V, so it knows which side of the MPP it is on and can stop oscillating in steady sun, provided the sensors have good resolution. For your literature review, see Esram and Chapman (IEEE Trans. Energy Conversion, 2007).

Charging stages: CC/CV for lead-acid and LiFePO4

MPPT decides how much power the panel can give; the battery decides how much it can accept. The controller obeys the smallest of three limits: MPP, charge current (CC) and absorption voltage (CV). When a battery limit is active, the firmware lowers D, which moves the panel to the stable side of the MPP, towards Voc.

Setting (12 V nominal)Flooded lead-acidLiFePO4 12.8 V
Absorption / CV2.40–2.45 V/cell → 14.4–14.7 V14.2 V
Float2.25–2.27 V/cell → 13.5–13.6 V13.5 V
Temperature compensation−3 mV/°C per cell (−18 mV/°C for 12 V) above 25 °C0 mV/°C (disable)
Charge current0.3C recommended≈0.5C recommended
Cold limit—Do not charge below +5 °C

Sources: Battery University BU-403 for lead-acid and Victron’s LiFePO4 battery manual for 12.8 V LiFePO4. Use your own battery’s datasheet values.

Design specifications of this reference design

ParameterValue
Panel input (MPP range)17–38 V; Voc up to 45 V including cold-morning rise (36-, 60- or 72-cell panels)
Battery12 V lead-acid or 12.8 V LiFePO4 (4S)
Max charge current20 A (≈ 280 W at 14 V)
TopologySynchronous buck, 40 kHz, centre-aligned PWM
Controller / sensingESP32-DevKitC V4; INA228 (panel), INA226 (battery), 2× NTC
Calculated efficiency≈ 96.5% at 245 W (calculated; expect 94–96% on a real board)

ESP32 MPPT charge controller block diagram

ESP32 MPPT solar charge controller block diagram with input protection, INA228/INA226 sensing, synchronous buck and gate driver

Power flows from panel through input protection, panel sensing, the synchronous buck and battery sensing to the battery. The ESP32 reads both INA sensors over I²C, drives the gate driver and serves the Wi-Fi dashboard. The auxiliary supply runs from the battery, so the dashboard also works at night.

ESP32 MPPT solar charge controller circuit diagram

ESP32 MPPT solar charge controller circuit diagram with UCC27211, IRFB4110, LM5050-1, INA228 and INA226

The full schematic is shown above. Pin names on the ESP32 board are taken from Espressif’s ESP32-DevKitC V4 user guide (header J2/J3). Gate driver pins come from the TI UCC27211 datasheet (SOIC-8). The sections below explain where every value comes from.

Switching frequency: why 40 kHz

Switching loss of large TO-220 MOSFETs (Qg ≈ 150 nC) scales with frequency: ≈ 1.3 W at 40 kHz versus ≈ 3.4 W at 100 kHz. 40 kHz is also inaudible, keeps the inductor hand-windable, and the ESP32 MCPWM (80 MHz, up-down, period 2000 ticks) still gives 1000 duty steps. One step is ≈ 31 mV of output at Vin = 31 V.

Inductor calculation (ripple current with numbers)

From TI’s SLVA477B: ΔIL = (Vin − Vout) × D / (L × fsw). Ripple is worst at Vin,max = 38 V; with Vout = 13.6 V, D = 0.358. Target ΔI ≤ 30% of 20 A = 6 A:

  • L ≥ (38 − 13.6) × 0.358 / (6 A × 40 kHz) = 36.4 µH → choose 40 µH.
  • Ripple with 40 µH: ΔI = 24.4 × 0.358 / (40 µH × 40 kHz) = 5.46 A p-p (4.77 A at Vin = 31 V).
  • Peak = 20 + 5.46/2 = 22.7 A; require L ≥ 32 µH at 23 A DC bias (check the core’s DC-bias curve).
  • Irms ≈ 20.06 A. Skin depth at 40 kHz ≈ 0.33 mm, so wind ≈ 8 strands of 0.8 mm wire (≈ 4 mm²) on a sendust or −52 powder toroid; turns ≈ √(L/AL).

Input and output capacitors (ripple current)

  • Input: ICin,rms = Iout√(D(1−D)), worst at D = 0.5: 10 A RMS. Use 6 × 470 µF / 63 V low-ESR (each ≥ 1.7 A ripple rating) + 4 × 2.2 µF / 100 V X7R right at Q2/Q3. Ripple ≈ 20 × 0.25 / (40 kHz × 2820 µF) ≈ 44 mV.
  • Output: Irms = ΔI/√12 = 1.58 A; Cmin for 50 mV = ΔI/(8ΔVf) = 341 µF. 2 × 1000 µF / 25 V + 2 × 4.7 µF gives ≈ 8.5 mV.

MOSFET and gate driver selection

MOSFETs: IRFB4110 (Q1–Q3): 100 V, RDS(on) 3.7 mΩ typ / 4.5 mΩ max, Qg 150 nC, Qgd 43 nC, RθJC 0.402 °C/W (Infineon datasheet). 100 V gives margin over 45 V Voc plus ringing. Fakes are common in Dhaka, so read how to spot fake electronic components first.

Gate driver: UCC27211: 120 V bootstrap diode, 3.7 A / 4.5 A peak, VDD 8–17 V, TTL inputs (VIH 2.7 V max) that the ESP32’s 3.3 V drives directly. It has no interlock, so the ESP32 MCPWM provides 200 ns dead time per edge (IRFB4110 td(off) + tf = 166 ns in its datasheet test). Confirm on a scope.

  • Why not IR2104? Its few-hundred-mA gate current would make each edge of a 150 nC MOSFET last hundreds of ns, which means several watts of switching loss.
  • Bootstrap: Cboot ≥ Qg,max/ΔV = 210 nC / 0.5 V = 0.42 µF → 1 µF X7R. VDD: 10 µF + 1 µF at pins 1–7.
  • Gate resistors 3.3 Ω (peak ≈ 2.1 A) plus 10 kΩ gate–source. Snubber across Q3: start at 4.7 Ω + 2.2 nF and tune.

Loss and efficiency estimate

At Vin = 31 V, Vout = 13.6 V, Iout = 18 A (244.8 W), D = 0.439. Assumptions: RDS(on) 7.5 mΩ hot (4.5 mΩ × ≈1.67, conservative), ≈ 60 ns per switching edge (≈ 60 nC switching charge at ≈ 1 A gate current).

Loss termFormulaCalculated
Q2 conductionI² R D = 18² × 7.5 mΩ × 0.4391.07 W
Q3 conductionI² R (1−D)1.36 W
Q2 switching½ V I (tr+tf) f = ½ × 31 × 18 × 120 ns × 40 kHz1.34 W
Q3 body-diode reverse recoveryQrr V f = 140 nC × 31 × 40 kHz0.17 W
Dead-time diode conductionVf I 2tdt f = 0.8 × 18 × 400 ns × 40 kHz0.23 W
Gate drive2 Qg VDD f0.14 W
Inductor copper + core18² × 6 mΩ + ≈0.5 W (core estimate)2.44 W
Shunts RS1 + RS28.2² × 2 mΩ + 18² × 2 mΩ0.78 W
Ideal-diode MOSFET Q18.2² × ≈6 mΩ0.38 W
Capacitor ESR + auxiliary supplyestimate1.0 W
Total≈ 8.9 W → η ≈ 96.5%

A real board is usually 1–2% lower (layout, ringing, core loss), so expect 94–96%.

Thermal design: heatsink Rth

Heatsink load: Q1 + Q2 + Q3 ≈ 0.38 + 2.58 + 1.59 ≈ 4.6 W. With an insulating pad (RθCS ≈ 1 °C/W assumed), 45 °C enclosure air and a ≤ 4 °C/W heatsink: Ths ≈ 45 + 4.6 × 4 ≈ 63 °C and TJ(Q2) ≈ 63 + 2.58 × 1.4 ≈ 67 °C. A fan switched on above 60 °C adds margin for Bangladesh summers.

Current and voltage sensing (check the common-mode limits)

Many DIY designs fail here. The INA226 is rated 0–36 V common-mode: fine for the battery, not for a 45 V panel. The input uses the INA228 (−0.3 to 85 V, 20-bit, ±163.84 mV). Both sit on one I²C bus (0x40, 0x41).

  • Shunts 2 mΩ / 3 W with Kelvin traces: 40 mV and 0.8 W at 20 A (INA226 range ±81.92 mV).
  • Cheap INA226 modules carry a 0.1 Ω (R100) shunt that saturates at ≈ 0.82 A. Remove it.
  • Backup dividers on ADC1 only (ADC2 conflicts with Wi-Fi): 100 k/5.6 k puts 45 V at 2.39 V on IO34; 47 k/6.8 k puts 15 V at 1.90 V on IO35. That is inside the ≈ 150–3100 mV range of 11 dB attenuation.
  • INA ALERT outputs go to IO19/IO18 as hardware fault flags.

Protection: back-feed, reverse polarity, TVS and fuses

  • Night back-feed: Q2’s body diode lets the battery feed the dark panel. The LM5050-1 high-side OR-ing controller + Q1 (IRFB4110) acts as an ideal diode: IN senses the source (panel side), OUT the drain (VIN), and GATE (≈ 12 V above IN) is pulled low within ≈ 25 ns of reverse current (≈ 180 ns typical turn-off at 47 nF; Q1 Ciss is 9.6 nF). VS is fed from PV_F through 100 Ω with 0.1 µF (datasheet suggestion), and OFF is tied to GND. Voltage margins: panel Voc 45 V; LM5050-1 operating range 5–75 V, absolute max 100 V on IN/OUT/GATE/VS; the SMCJ48A clamps at 77.4 V at full Ipp, which is just above the 75 V operating range but well below the 100 V absolute maximum; Q1 is 100 V. The LM5050-1 is not reverse-polarity rated (pins −0.3 V min): a reversed panel forward-biases D1 (current limited to the panel’s Isc), so use keyed MC4 connectors and treat reverse-panel protection as an upgrade.
  • Reverse boosting: a sync buck with too low a duty cycle boosts from battery to panel. The firmware starts at D = Vbat/Vpv and keeps the low-side MOSFET off (diode emulation) below ≈ 1.5 A.
  • TVS: SMCJ48A on the input (48 V standoff above the 45 V Voc; 77.4 V clamp, below the 100 V rating of Q1–Q3, the LM5050-1 abs max and the INA228’s 85 V); SMCJ18A on the battery (29.2 V clamp).
  • Fuses: F1 20 A on PV+, F2 25 A on the output and one more at the battery terminal. A reversed battery blows F2 through D2/Q3; SS54 protects the auxiliary supply.
  • Driver VDD (20 V abs. max) is fed through 47 Ω with a 15 V zener (1N4744A).

Wiring table (pin-by-pin)

FromToNote / source
DevKitC IO26 (J2-10)47 Ω → UCC27211 HI (pin 5)PWM_H; 10 k to GND
DevKitC IO27 (J2-11)47 Ω → UCC27211 LI (pin 6)PWM_L; 10 k to GND
UCC27211 HO (3)3.3 Ω → Q2 gate10 k G–S
UCC27211 LO (8)3.3 Ω → Q3 gate10 k G–S
UCC27211 HB (2) / HS (4)1 µF bootstrap; HS → switch node SWQ2 source = Q3 drain
UCC27211 VDD (1) / VSS (7)+12V_DRV (10 µF + 1 µF) / PGND8–17 V operating
DevKitC IO21 (J3-6) / IO22 (J3-3)SDA / SCL of INA228 + INA2264.7 k pull-ups to 3V3
DevKitC IO19 (J3-8) / IO18 (J3-9)INA228 ALERT / INA226 ALERT10 k pull-ups
DevKitC IO34 (J2-5)VIN divider 100 k / 5.6 kADC1_CH6
DevKitC IO35 (J2-6)VBAT divider 47 k / 6.8 kADC1_CH7
DevKitC VP/IO36 (J2-3), VN/IO39 (J2-4)Heatsink NTC, battery NTC (10 k pull-up)ADC1_CH0 / CH3
DevKitC IO13 (J2-15)100 Ω → AO3400 gate (fan)100 k pull-down
DevKitC IO4 (J3-13)1 k → status LED—
DevKitC 5V (J2-19) / GNDMP1584 5 V outputNever 5 V pin + USB together
DevKitC 3V3 (J2-1)INA228/INA226 VS, pull-ups, NTC dividersOnboard LDO
LM5050-1 (SOT-23-6 DDC) IN 4 / GATE 5 / OUT 6Q1 source (PV_F) / gate / drain (VIN)VS 1 → 100 Ω to PV_F + 0.1 µF to GND; OFF 3 → GND; GND 2 → PGND

Strapping pins (GPIO0, 2, 5, 12, 15) and flash pins (GPIO6–11) are avoided.

Bill of materials and cost in Bangladesh

PartQtyApprox. BDT (Nov 2026, local market)
ESP32-DevKitC V4 (or ESP32-WROOM-32 dev board)1650–900
UCC27211 gate driver (SOIC-8)1250–400
IRFB4110 MOSFET (genuine)3450–750
LM5050-1 ideal-diode (OR-ing) controller1200–400
INA228 (IC or breakout)1600–1,800
INA226 module (remove onboard shunt)1350–500
2 mΩ 3 W shunt resistor2150–300
40 µH / 23 A toroid inductor (core + wire)1500–900
Capacitors (6×470 µF 63 V, 2×1000 µF 25 V, ceramics)—500–800
SMCJ48A, SMCJ18A, SS54, 1N4744A, 1N5819—150–250
MP1584 buck module1100–150
Fuses + holders (20 A, 25 A), screw terminals—300–500
Heatsink ≤ 4 °C/W, insulating pads, 12 V fan—500–900
NTC 10 k × 2, AO3400, resistors, LED—100–200
2-layer 2 oz PCB, 5 pcs incl. shipping—1,500–3,000
Total≈ 6,300–11,700

Ordering the PCB? See our KiCad 10 to JLCPCB guide.

Step-by-step build guide

Step 1: Lay out the power stage first

Keep the hot loop (Cin ceramics → Q2 → Q3) tiny on 2 oz copper, use Kelvin shunt traces and a star ground at Cin(−), and keep the ESP32 away from the switch node.

Step 2: Wind and measure the inductor

Measure 40–45 µH on an LCR meter; check it under DC bias with a pulse test if you can.

Step 3: Bring up the auxiliary supply

Bench supply at 13 V, 200 mA limit. Check 5.0 V, 3.3 V and +12V_DRV; both gates must sit at 0 V.

Step 4: Check PWM and dead time without power

Confirm 40 kHz complementary gate signals with ≥ 150 ns dead time on both edges, measured at the MOSFET gates.

Step 5: Low-voltage power test

20 V / 2 A current-limited input into a resistor load: check Vout = D × Vin and tune the snubber.

Step 6: Calibrate the sensors

Compare the INA readings with a calibrated meter at 0, 5, 10 and 15 A; store gain/offset in NVS.

Step 7: Battery and panel test

Battery first, then the panel through a DC breaker. Start with a 5 A firmware limit and raise it while logging heatsink temperature.

Step 8: Long-run validation

Log a sunny and a cloudy day and compare the harvest with a PWM controller.

Firmware architecture (the whole picture)

Arduino-ESP32 core 3.x (ESP-IDF 5), using the IDF MCPWM driver directly. FreeRTOS tasks keep Wi-Fi away from the control loop:

Task / moduleCore, rateWhat it does
controlCore 1, 20 msRead INAs, CC/CV limiter, MPPT every 100 ms, update duty
protectCore 1, 1 ms + ALERT IRQOV/UV/OC/OT checks; force gates low, latch FAULT
mpptin controlAdaptive P&O, IncCond option, global sweep every 10–15 min
chargerin controlBattery profiles, stage timers, temp compensation, cold inhibit
netCore 0Wi-Fi, web dashboard (1 s JSON), settings, OTA
logCore 0, 1 sWh/Ah counters, daily history, fault log

State machine: INIT → WAIT_SUN (Vpv > Vbat + 1.5 V for 30 s) → SOFT_START (diode-emulation, D = Vbat/Vpv, ramp) → BULK_MPPT → ABSORB (CV, timed) → FLOAT or HOLD → NIGHT. Any state can drop to FAULT (latched, retry with back-off). Sync rectification switches on above 1.5 A and off below 1 A.

Data flow: sensors → mutex-protected measurement struct → control law → pwmSetDuty(). The net task only reads it and sends settings through a queue, so a slow browser never blocks the converter.

ESP32 MPPT code (about 50% shown)

About 50% of the firmware is shown: config header, MCPWM driver, sensor driver and one P&O step (Espressif MCPWM API; RobTillaart INA226/INA228 libraries). Withheld: control loop, state machine, CC/CV tuning, soft-start, protection, calibration, dashboard, OTA and logging.

1. Pin map and limits (config.h)

// config.h - ESP32-DevKitC V4 pin map and design limits (reference design)
#pragma once
#define PIN_PWM_H     26   // IO26 -> 47R -> UCC27211 HI (pin 5)
#define PIN_PWM_L     27   // IO27 -> 47R -> UCC27211 LI (pin 6)
#define PIN_SDA       21   // I2C to INA228 (0x40) + INA226 (0x41)
#define PIN_SCL       22
#define PIN_ALERT_PV  19   // INA228 ALERT, open-drain, 10k pull-up
#define PIN_ALERT_BAT 18   // INA226 ALERT
#define PIN_ADC_PV    34   // ADC1: VIN x 5.6k/105.6k
#define PIN_ADC_BAT   35   // ADC1: VBAT x 6.8k/53.8k
#define PIN_NTC_HS    36   // VP: heatsink NTC
#define PIN_NTC_BAT   39   // VN: battery NTC
#define PIN_FAN       13
#define PIN_LED        4
constexpr uint32_t PWM_RES_HZ = 80000000;  // 12.5 ns per tick
constexpr uint32_t PWM_PERIOD = 2000;      // up-down: 80 MHz / 2000 = 40 kHz
constexpr uint32_t DEAD_TICKS = 16;        // 16 x 12.5 ns = 200 ns per edge
constexpr float DUTY_MIN = 0.05f, DUTY_MAX = 0.95f, DUTY_STEP = 0.004f;

2. Complementary PWM with dead time (MCPWM)

Centre-aligned with two comparators: HI is high below cmpH, LO above cmpL = cmpH + 16 ticks, giving 200 ns dead time on both edges. Each output can be forced low on its own (shutdown, diode emulation).

// pwm.cpp - centre-aligned complementary PWM with dead time (ESP-IDF MCPWM driver)
#include <Arduino.h>
#include "driver/mcpwm_prelude.h"
#include "config.h"
static mcpwm_cmpr_handle_t cmpH, cmpL;
static mcpwm_gen_handle_t genH, genL;
static const uint32_t PEAK = PWM_PERIOD / 2;   // up-down mode peak = 1000

static void act(mcpwm_gen_handle_t g, mcpwm_cmpr_handle_t c,
                mcpwm_timer_direction_t dir, mcpwm_generator_action_t a) {
  ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(g,
      MCPWM_GEN_COMPARE_EVENT_ACTION(dir, c, a)));
}

void pwmInit() {
  mcpwm_timer_handle_t tmr; mcpwm_oper_handle_t op;
  mcpwm_timer_config_t tc = {};
  tc.group_id = 0;  tc.clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT;
  tc.resolution_hz = PWM_RES_HZ;  tc.period_ticks = PWM_PERIOD;
  tc.count_mode = MCPWM_TIMER_COUNT_MODE_UP_DOWN;
  ESP_ERROR_CHECK(mcpwm_new_timer(&tc, &tmr));
  mcpwm_operator_config_t oc = {};  oc.group_id = 0;
  ESP_ERROR_CHECK(mcpwm_new_operator(&oc, &op));
  ESP_ERROR_CHECK(mcpwm_operator_connect_timer(op, tmr));
  mcpwm_comparator_config_t cc = {};  cc.flags.update_cmp_on_tez = true;
  ESP_ERROR_CHECK(mcpwm_new_comparator(op, &cc, &cmpH));
  ESP_ERROR_CHECK(mcpwm_new_comparator(op, &cc, &cmpL));
  mcpwm_generator_config_t gc = {};
  gc.gen_gpio_num = PIN_PWM_H;  ESP_ERROR_CHECK(mcpwm_new_generator(op, &gc, &genH));
  gc.gen_gpio_num = PIN_PWM_L;  ESP_ERROR_CHECK(mcpwm_new_generator(op, &gc, &genL));
  mcpwm_generator_set_force_level(genH, 0, true);   // both gates held OFF
  mcpwm_generator_set_force_level(genL, 0, true);   // until the state machine starts
  act(genH, cmpH, MCPWM_TIMER_DIRECTION_UP,   MCPWM_GEN_ACTION_LOW);   // HI = counter < cmpH
  act(genH, cmpH, MCPWM_TIMER_DIRECTION_DOWN, MCPWM_GEN_ACTION_HIGH);
  act(genL, cmpL, MCPWM_TIMER_DIRECTION_UP,   MCPWM_GEN_ACTION_HIGH);  // LO = counter > cmpL
  act(genL, cmpL, MCPWM_TIMER_DIRECTION_DOWN, MCPWM_GEN_ACTION_LOW);
  ESP_ERROR_CHECK(mcpwm_timer_enable(tmr));
  ESP_ERROR_CHECK(mcpwm_timer_start_stop(tmr, MCPWM_TIMER_START_NO_STOP));
}

void pwmSetDuty(float d) {                       // d = high-side duty 0..1
  d = constrain(d, DUTY_MIN, DUTY_MAX);
  uint32_t h = (uint32_t)(d * PEAK), l = h + DEAD_TICKS;   // gap = dead time
  mcpwm_comparator_set_compare_value(cmpH, h);
  mcpwm_comparator_set_compare_value(cmpL, l < PEAK ? l : PEAK - 1);
}

3. Sensor driver (INA228 + INA226)

// sensors.cpp - INA228 on the panel side (85 V), INA226 on the battery side (36 V)
#include <Wire.h>
#include "INA228.h"     // RobTillaart/INA228
#include "INA226.h"     // RobTillaart/INA226
INA228 inaPV(0x40);     // A1 = A0 = GND
INA226 inaBAT(0x41);    // A0 = VS

bool sensorsInit() {
  Wire.begin(PIN_SDA, PIN_SCL, 400000);
  if (!inaPV.begin() || !inaBAT.begin()) return false;
  if (inaPV.setMaxCurrentShunt(20.0, 0.002) != 0) return false;   // RS1 = 2 mOhm
  if (inaBAT.setMaxCurrentShunt(20.0, 0.002) != 0) return false;  // RS2 = 2 mOhm
  inaPV.setAverage(INA228_4_SAMPLES);
  inaBAT.setAverage(INA226_4_SAMPLES);
  analogSetAttenuation(ADC_11db);   // backup dividers stay below 2.6 V
  return true;
}
struct Meas { float vpv, ipv, vbat, ibat; };
Meas readAll() {
  return { inaPV.getBusVoltage(), inaPV.getCurrent(),
           inaBAT.getBusVoltage(), inaBAT.getCurrent() };
}

4. One perturb-and-observe step

// One perturb-and-observe step, called every 100 ms with averaged data.
// In a buck, Vpv ~ Vbat / D: raising the duty pulls the panel voltage DOWN.
float poStep(float vpv, float ipv, float duty) {
  static float pPrev = 0, vPrev = 0;
  const float P_DEADBAND = 0.3f;              // W, ignore noise-level changes
  float p = vpv * ipv, dP = p - pPrev, dV = vpv - vPrev;
  if (fabsf(dP) > P_DEADBAND) {
    bool raiseV = (dP > 0) == (dV > 0);       // dP/dV > 0 -> MPP is at higher V
    duty += raiseV ? -DUTY_STEP : +DUTY_STEP;
  }
  pPrev = p;  vPrev = vpv;
  return constrain(duty, DUTY_MIN, DUTY_MAX);
}

The complete production firmware is not published here. It includes the full MPPT engine (adaptive P&O, incremental conductance and global sweep), the CC/CV charge state machine with lead-acid/LiFePO4 profiles and temperature compensation, soft-start and diode-emulation control, latched protection and fault logging, a sensor calibration routine stored in NVS, the Wi-Fi web dashboard with live graphs and settings, OTA updates and daily energy logging. Message us on WhatsApp to get the full source code, circuit files and help building it.

Testing, expected results and accuracy

  • Tracking efficiency: well-tuned P&O typically reaches 97–99% in steady sun, less under fast clouds.
  • Conversion efficiency: ≈ 96.5% calculated; expect 94–96% measured.
  • Measurement: INA gain error 0.1% (INA226) / 0.05% (INA228) max; the 1% shunt tolerance dominates until calibrated.
  • CV accuracy: ±50 mV is realistic after calibration.

Troubleshooting common mistakes

SymptomLikely causeFix
Panel voltage runs to Voc or collapsesP&O duty sign invertedRaising D must lower Vpv; check the step direction
Input capacitor voltage rises above Voc at startConverter boosting backwards (sync FET on, D too low)Start at D = Vbat/Vpv, diode-emulation below 1.5 A
Q2/Q3 hot at light loadDead time too short (shoot-through) or ringingMeasure at gates, increase DEAD_TICKS, tune snubber
Current reading stuck near 0.8 AINA226 module’s 0.1 Ω shunt still fittedRemove it, use the external 2 mΩ shunt
INA reads nonsense above 36 VINA226 used on the panel sideUse INA228 (85 V) on the input
ESP32 resets while switchingGround bounceStar ground, ESP32 away from hot loop

Safety notes

A 12 V battery can push hundreds of amps into a short, and DC arcs do not self-extinguish. Fuse at the battery terminal, use ≥ 25 A cable, connect the battery before the panel, and cover the panel while wiring. Keep the LiFePO4 BMS in place, and ventilate lead-acid batteries.

Future upgrades and real-world uses

  • Product: enclosure, load output with low-voltage disconnect, Bangla app dashboard for shops, irrigation monitors and telecom backup.
  • Two-phase interleaved buck for 600 W–1 kW with ripple cancellation, a great M.Sc. extension. See our M.Sc. thesis topics.
  • Research: compare P&O, IncCond and ML-predicted MPP under partial shading; pair it with our ESP32 smart energy meter.

Frequently asked questions

How does an MPPT charge controller work?

A buck converter between panel and battery adjusts its duty cycle so the panel runs where V × I is highest, turning surplus panel voltage into extra charging current.

Is an MPPT charge controller better than PWM?

Usually. With a 36-cell panel MPPT harvests roughly 15–20% more than PWM, and it is the only sensible choice for 60/72-cell panels on 12 V.

Can I make my own MPPT charge controller with an ESP32 or Arduino?

Yes. You need a well-designed synchronous buck, a proper gate driver, accurate sensing and protection. The ESP32 beats an Uno here thanks to MCPWM dead time and Wi-Fi.

Which MPPT algorithm is best, P&O or incremental conductance?

Both reach the same MPP in steady sun. P&O is simpler; IncCond oscillates less with good sensors. Neither finds the global peak under shading without a periodic sweep.

What size MPPT charge controller do I need for a 300 W panel?

300 W / 12 V ≈ 25 A, so use a 25–30 A controller (≈ 15 A at 24 V) and check the cold Voc against the input limit. This 20 A design suits ≈ 250–280 W.

Can an MPPT charge controller charge a LiFePO4 battery?

Yes, with a lithium profile: ≈ 14.2 V absorption, 13.5 V float, no temperature compensation, no charging below +5 °C, and the BMS kept in place.

Conclusion

An ESP32 MPPT solar charge controller teaches nearly all of practical power electronics: ripple-based magnetics, datasheet-driven MOSFET and driver choice, loss and thermal estimates, and a control loop around a nonlinear source. Start from these calculations, build carefully, measure everything and report honest numbers.

Want this MPPT charge controller as your final-year project, with a working board, full firmware and viva support? We design custom PCBs, firmware and complete working prototypes for students and companies. Message us on WhatsApp or use the contact form. You can write in Bangla or English.


MKDas

Mithun K. Das. B.Sc. in Electrical and Electronic Engineering (EEE) from KUET. Senior Embedded Systems Designer at a leading international company. Welcome to my personal blog! I share articles on various electronics topics, breaking them down into simple and easy-to-understand explanations, especially for beginners. My goal is to make learning electronics accessible and enjoyable for everyone. If you have any questions or need further assistance, feel free to reach out through the Contact Us page. Thank you for visiting, and happy learning!