STM32 drives INA226 to measure current, voltage, and power
INA226 + STM32 is a rock-solid combo for precision DC current, bus voltage, and power. Here’s a clean, copy-ready guide with wiring, calibration math, and working HAL code.
STM32 + INA226 (I²C) — Measure Current, Bus Voltage & Power
1) What the INA226 gives you
-
Bus voltage (VBUS): up to 36 V, LSB = 1.25 mV
-
Shunt voltage (VSHUNT) across your sense resistor, LSB = 2.5 µV
-
Current (after you program a calibration)
-
Power (after calibration), Power_LSB = 25 × Current_LSB
-
Programmable averaging, conversion times, and alert (over-current, over-voltage, power, etc.)
2) Typical wiring (single-supply)
-
Shunt resistor (RSHUNT) in series with the load on the low side or high side (INA226 supports both; high side is common).
-
Connect:
-
VIN+to load side (after shunt toward load) -
VIN−to source side (before shunt toward supply) -
VBUSinternally sensed from VIN+ (no extra wire for bus voltage) -
SDA/SCL→ STM32 I²C pins (e.g., PB9/PB8 on many Nucleo boards) -
A0,A1to GND/VCC for I²C address select (0x40–0x4F) -
ALERT(optional) to an STM32 EXTI-capable GPIO for interrupts -
Decoupling 0.1 µF close to INA226 VCC
-
I²C address: base 0x40 when A1=A0=GND (others up to 0x4F).
3) Key registers (16-bit)
| Register | Addr | Notes |
|---|---|---|
| Configuration | 0x00 | Averaging, conv. times, mode |
| Shunt Voltage | 0x01 | 2.5 µV/bit (signed) |
| Bus Voltage | 0x02 | 1.25 mV/bit (unsigned) |
| Power | 0x03 | 25×Current_LSB (W/bit), needs CAL |
| Current | 0x04 | Current_LSB (A/bit), needs CAL |
| Calibration | 0x05 | Sets Current_LSB & scaling |
| Mask/Enable | 0x06 | ALERT config |
| Alert Limit | 0x07 | Threshold (unit depends on mask) |
All registers are big-endian (MSB first).
4) Calibration math (do this once!)
-
Choose RSHUNT and your IMAX (expected max current).
-
Pick Current_LSB ≈ IMAX / 32768 (A/bit).
-
Compute CAL (write to Calibration register):
(CAL is a 16-bit integer; clamp to ≤ 0x7FFF if needed.)
-
Then:
-
Current (A) = Current_Reg × Current_LSB
-
Power (W) = Power_Reg × (25 × Current_LSB)
-
Example
-
RSHUNT = 0.010 Ω, IMAX = 3.0 A
-
Current_LSB = 3.0 / 32768 = 9.1552734e-5 A/bit (≈ 91.55 µA/bit)
-
CAL = 0.00512 / (9.1552734e-5 × 0.01) ≈ 5592 = 0x15D8
-
Power_LSB = 25 × Current_LSB = 0.0022888 W/bit (≈ 2.289 mW/bit)
5) Recommended configuration
-
Averaging (AVG): e.g., 16–64 (trade noise vs speed)
-
VBUS/VSHUNT conversion time: 1100 µs (or 140 µs…8.244 ms)
-
Mode: Shunt+Bus, continuous
Configuration register (0x00) fields (typical):
-
AVG = 0b010 (16x) or 0b011 (64x)
-
VBUSCT = 0b100 (1.1 ms)
-
VSHCT = 0b100 (1.1 ms)
-
MODE = 0b111 (Shunt+Bus continuous)
6) STM32 HAL code (I²C)
6.1 Helpers
6.2 Init (config + calibration)
6.3 Read & convert to units
7) Using ALERT (optional)
-
Configure Mask/Enable to select alert source (e.g., over-current).
-
Write Alert Limit with a threshold in the corresponding units (depends on alert function).
-
Route ALERT to an EXTI pin; in ISR, read Mask/Enable to clear/identify cause.
Example idea: over-current alert → set Alert Limit = threshold / Current_LSB (in Current register’s units) and enable the Conversion Ready bit to pace reads.
8) Accuracy tips
-
Choose RSHUNT so that your typical/peak current uses a healthy fraction of the shunt’s full-scale voltage (keep power dissipation acceptable: ).
-
Prefer low TCR (e.g., 50 ppm/°C) 1% or 0.5% shunts; 2512 or 1206 packages for thermal stability.
-
Use averaging for noisy loads; raise conversion time for better resolution.
-
Kelvin-connect the shunt; keep high-current paths short and away from sense lines.
-
If measuring at high bus voltages, respect creepage/clearance on the PCB.
9) Quick checklist
-
I²C at 100–400 kHz, pull-ups 2.2–4.7 kΩ
-
Correct I²C address from A0/A1 strap
-
Write CONFIG, then CAL
-
Use correct LSB scales (1.25 mV, 2.5 µV, Current_LSB, 25×Current_LSB)
-
Convert signed registers properly (shunt, current)
-
Optional: set ALERT mask & limit, handle EXTI
10) Troubleshooting
-
All zeros? Check CAL register was written and mode is continuous.
-
Crazy values? Endianness, wrong LSBs, or mis-wired VIN+/VIN-.
-
I²C NACK? Address pins (A0/A1), pull-ups, or wrong 8-bit vs 7-bit address in HAL.
-
Noisy readings? Increase averaging and conversion time; improve shunt layout.

评论
发表评论