Smart plugs and clamp meters already cover mains AC. The gap that shows up in DIY projects is the other side of the wall wart: a 12 V LED strip, a LiFePO4 pack, a small solar panel, or the 5 V rail that keeps browning out. Texas Instruments' INA219 and INA226 sit on that DC bus, measure the drop across a shunt resistor, and report voltage, current and power over I²C. An ESP32 can read them and publish the numbers wherever you already send sensor data.

This guide sticks to what the datasheets and common breakout boards actually specify: which chip to pick, how to wire the shunt in series with the load, how to set the shunt value in software, and the mistakes that produce confident-looking wrong amps.

What these chips measure

Both devices are digital current-shunt and power monitors. They do two analogue measurements:

  1. The differential voltage across a shunt resistor between the IN+ and IN- pins.
  2. The bus voltage on a sense pin relative to ground.

From those, and from a calibration value that includes the shunt resistance, the chip computes current and power and exposes the results on I²C (or SMBus).

They are not energy-meter ICs for 230 V AC. For a single mains circuit you want something like a PZEM-004T or a calibrated CT clamp. Use INA219/INA226 when the rail you care about is low-voltage DC.

INA219 vs INA226

From Texas Instruments' product pages:

INA219 INA226
Bus voltage sensing 0 to 26 V 0 to 36 V
ADC 12-bit 16-bit
Chip supply 3 to 5.5 V 2.7 to 5.5 V
Typical supply current up to 1 mA max about 330 µA typ.
Extra features programmable gain (PGA) on the shunt path alert pin, averaging, higher precision
I²C addresses up to 16 via address pins (commonly 4 on breakouts) up to 16

The INA219's PGA stretches the shunt full-scale range up to ±320 mV at gain /8. With the usual 0.1 Ω shunt on hobby breakouts, that is about ±3.2 A. The INA226 has no PGA; its shunt full-scale is smaller, so a 0.1 Ω shunt only covers roughly ±0.8 A. Boards aimed at higher current usually ship with a 0.01 Ω shunt instead. Always read the resistor marking (or measure it) before you trust the amps.

Rule of thumb:

  • Already have an Adafruit-style INA219 board, or need a few amps on a 12 V rail without swapping the shunt: start with the INA219.
  • Need finer resolution, an alert pin, or bus voltages above 26 V up to 36 V: pick the INA226 and match the shunt to your current range.

Wiring: logic pins and the power path are different

Power the breakout from the ESP32's 3.3 V pin and share ground with the measured supply. On a classic ESP32 development board the usual I²C pins are GPIO21 (SDA) and GPIO22 (SCL). Many boards already include pull-ups; if a scan finds nothing, add 4.7 kΩ pull-ups to 3.3 V.

Breakout pin Connect to
VCC / VS ESP32 3.3 V (logic supply only)
GND ESP32 GND and the DC supply negative
SDA GPIO21
SCL GPIO22
VIN+ / IN+ positive of the DC supply (before the shunt)
VIN- / IN- positive feed into the load (after the shunt)

Current must flow through the shunt: supply + → VIN+ → (shunt) → VIN- → load +, with supply - tied to the load negative and to the ESP32 ground. Do not feed the load through the ESP32's 3.3 V or 5 V pin. Do not use these boards on mains.

For a first test, use a fused, current-limited bench supply or a small battery pack and a resistive load (a car bulb or a power resistor). Confirm the polarity: reverse current is fine for bidirectional chips, but a reverse-wired high-side shunt with the wrong expectation looks like a software bug.

Scan the bus once before you dig into libraries:

i2c: scan on SDA=21 SCL=22

Default address for both chips is often 0x40. Address jumpers or solder pads select other values when you put several monitors on one bus.

Calibration is the shunt resistance

Wrong shunt_resistance is the most common reason for plausible but wrong current. Libraries and firmwares multiply the measured shunt voltage by a calibration constant derived from that value. If the board has 0.01 Ω and you leave the default at 0.1 Ω, every amp reading is off by a factor of ten.

How to find the real value:

  1. Read the shunt resistor code on the board (for example R100 = 0.1 Ω, R010 = 0.01 Ω).
  2. Or put a known current through the path (bench supply in constant-current mode, or a load measured with a trustworthy meter) and adjust until the reported current matches.

Also set the expected maximum current (and, for the INA219, maximum bus voltage) so the library can choose PGA / range bits instead of clipping or wasting resolution.

Reading it from an ESP32

Arduino (INA219)

Install Adafruit INA219 from the Library Manager. Minimal sketch shape:

#include <Wire.h>
#include <Adafruit_INA219.h>

Adafruit_INA219 ina219;

void setup() {
  Serial.begin(115200);
  Wire.begin(21, 22);
  if (!ina219.begin()) {
    Serial.println("INA219 not found");
    while (true) delay(10);
  }
}

void loop() {
  float bus_v = ina219.getBusVoltage_V();
  float current_mA = ina219.getCurrent_mA();
  float power_mW = ina219.getPower_mW();
  Serial.printf("%.3f V  %.1f mA  %.1f mW\n", bus_v, current_mA, power_mW);
  delay(1000);
}

If your breakout uses a non-default shunt, use the library's calibration helpers rather than the stock 0.1 Ω assumption. For the INA226 there is no official Adafruit library; the commonly used option is Rob Tillaart's INA226 library, which exposes a similar set of getters after you pass the shunt resistance and maximum current.

ESPHome

ESPHome has first-class ina219 and ina226 sensor platforms. Both default to address 0x40 and shunt_resistance: 0.1 ohm. Change that to match the board. Example for an INA226 with a 0.01 Ω shunt and a few amps of headroom:

i2c:
  sda: 21
  scl: 22
  scan: true

sensor:
  - platform: ina226
    address: 0x40
    shunt_resistance: 0.01 ohm
    max_current: 8A
    update_interval: 2s
    current:
      name: "Battery current"
    power:
      name: "Battery power"
    bus_voltage:
      name: "Battery voltage"
    shunt_voltage:
      name: "Shunt voltage"

For the INA219, the platform also accepts max_voltage (default 32 V) and max_current (default 3.2 A). Publish over the native API, MQTT, or both - same as any other ESPHome sensor.

Checks that catch bad readings early

  • Bus voltage near zero while the load is on: VBUS on some modules is hard-wired to the supply side of the shunt, on others to the load side. Continuity-check the board if the voltage does not match what a multimeter shows at the load.
  • Current stuck at zero: the load is not actually going through the shunt (you paralleled around it), or the I²C address is wrong.
  • Current roughly 10× too high or too low: shunt value mismatch.
  • Noise on low currents: enable averaging (INA226) or read slower and median-filter in firmware; keep the shunt Kelvin connections short on custom boards.
  • Chip gets hot: you exceeded the shunt's power rating (I²R). A 0.1 Ω shunt at 3 A dissipates 0.9 W - many tiny resistors are not happy there. Step down to 0.01 Ω or an external shunt on a heatsink for high current.

Sensible uses at home

  • Watch a 12 V LED strip or a PoE splitter's 12 V output for brownouts.
  • Log how much a camera, router or Pi draws from a battery UPS DC bus.
  • Compare a small solar panel's output with and without a load.
  • Trip a relay in software when a pack exceeds a current limit (the INA226 alert pin can also do this in hardware once configured).

Keep mains energy metering on AC-side hardware. Keep DC rail budgeting on INA219/INA226. Mixing the two jobs in one board usually means the wrong tool for at least one of them.