Skip to content

Electrical / Current · Espressif ESP32 guide

Interfacing INA219 High-Side Current, Voltage and Power Sensor with Espressif ESP32

A tiny I²C power meter that reads bus voltage, current and power up to 26 V and about 3.2 A, ready for battery, solar and load monitoring.

Updated November 21, 2026 Arduino C++Adafruit INA219Also forArduino Uno

What is the INA219?

A multimeter tells you the voltage and, if you break the circuit and put it in series, the current. The INA219 does both continuously, without breaking anything, and hands the results to your microcontroller as numbers. It is Texas Instruments' small high-side current and power monitor: it measures the voltage drop across a tiny shunt resistor in your supply line, measures the bus voltage at the same time and calculates power for you.

Most hobby boards carry the chip with a 0.1 Ω shunt and a pair of screw terminals, so the whole thing is a ready-made inline power meter. Connect VIN+ to the supply and VIN− to the load, wire four pins to your board, and read volts, milliamps and milliwatts over I²C.

This page explains how it measures, why the shunt and calibration choices matter, how to wire it without the classic mistakes, and how it compares with the INA226, INA260 and ACS712. For working code, the Arduino Uno guide builds a battery capacity tester and an LCD power meter, and the ESP32 guide builds a live power page for your phone and an electronic fuse.

Specifications

Measures
Bus voltage, shunt voltage, current and power over I²C
Bus voltage range
0 to 26 V (the library's '32 V' setting is a range option, not a safe input level)
Shunt voltage range
±40, ±80, ±160 or ±320 mV (programmable gain; ±320 mV by default)
Typical breakout shunt
0.1 Ω (marked R100), giving about ±3.2 A full scale
ADC resolution
12-bit; shunt step 10 µV, bus voltage step 4 mV
Current resolution
0.1 mA with the library's default 32 V / 2 A calibration; 40 µA with 32 V / 1 A
Offset
About ±10 µV typical, ±100 µV max on the shunt input (up to ~1 mA with 0.1 Ω)
Supply voltage (VCC)
3.0 to 5.5 V, so it works on 3.3 V and 5 V boards
Supply current
About 1 mA while converting
Interface
I²C / SMBus, up to 400 kHz on common boards
I²C address
0x40 default; 0x41, 0x44 or 0x45 with the breakout's A0 / A1 pads (the chip supports 16)
Conversion time
84 µs (9-bit) to 68 ms (128-sample average); 532 µs in the default 12-bit mode
Sensing
High-side: the shunt sits in the positive supply line; current is signed (±)

Figures from the Texas Instruments INA219 datasheet and the Adafruit INA219 library. Check your own board's shunt marking and resistor power rating.

Pinout

PinNameDescription
1VIN+Positive side of the shunt. Connect to the power source (+). Not a power input for the chip
2VIN−Other side of the shunt. Connect to the load (+). Bus voltage is measured here
3VCCChip power, 3.0 to 5.5 V from your microcontroller
4GNDGround. Must be shared with the microcontroller and the measured circuit
5SDAI²C data
6SCLI²C clock

Most breakouts repeat VIN+ and VIN− on the header and on the screw terminals; use the terminals for anything above a few hundred milliamps.

Wiring & code

Setting up INA219 with Espressif ESP32

Library

Adafruit INA219 by Adafruit

Arduino IDE → Tools → Manage Libraries → search "Adafruit INA219" → Install (accept the prompt to also install "Adafruit BusIO")

This guide connects an INA219 to an ESP32 and builds two things the Wi-Fi board does best. After a first Serial reading, you will serve a live power and energy page you can open on your phone, and build an electronic fuse that switches a load off when its current stays too high. The ESP32 is a 3.3 V board, so the sensor is powered from 3V3, while the circuit you measure can run at anything up to 26 V.

New to the sensor? The INA219 overview explains the shunt, the calibration presets and the accuracy limits behind these sketches.

What you need

  • 1ESP32 DevKit (30- or 38-pin)

    Notes
    ESP32-S3 and C3 boards work too; only the I²C pins change
  • 1INA219 breakout with 0.1 Ω shunt

    Notes
    The common board marked R100
  • 1A DC load and its supply

    Notes
    For example a 12 V fan, pump or LED strip
  • 1Fuse holder and fuse (1 to 3 A)

    Notes
    Between the supply and VIN+
  • 1Logic-level MOSFET module or a 3.3 V-compatible relay module

    Notes
    Only for Sketch 3
  • 1USB cable with data lines

    Notes
    Charge-only cables are the most common upload failure

Wiring

INA219 pinESP32 pinNote
VCC3V3Power the breakout from 3.3 V so the I²C lines stay at 3.3 V.
GNDGNDMust also be common with the ground of the circuit you measure.
SDAGPIO21ESP32 default I²C data pin.
SCLGPIO22ESP32 default I²C clock pin.
VIN+Power source (+)Not an ESP32 pin. Connect to the positive terminal of the supply or battery.
VIN−Load (+)Not an ESP32 pin. Connect to the positive input of the load you want to measure.
INA219 on an ESP32 DevKit in the SolderHub Simulator: Vcc to 3V3, Gnd to GND, Sda to GPIO21, Scl to GPIO22, and the load circuit running through the Vin+ / Vin− terminal with its ground shared

Power the breakout from 3V3, so its I²C pull-ups hold SDA and SCL at 3.3 V. The measured circuit can be a 12 V battery or supply because VIN+ and VIN− are rated to 26 V, but all grounds must be common.

Measuring the ESP32's own current? The shunt's drop lowers the voltage the ESP32 sees, and Wi-Fi bursts draw several hundred milliamps. Measure on the 5 V side of the regulator rather than the 3.3 V rail where you can. Each 12-bit conversion takes about half a millisecond, so single readings miss the shortest Wi-Fi spikes: average many samples or count energy over time, as Sketch 2 does.

Other I²C pins. GPIO21/22 are the DevKit defaults, but the ESP32 can route I²C to most GPIOs: change the two numbers in Wire.begin(SDA, SCL). On an ESP32-S3 or C3, check your board's pinout and avoid strapping pins. See ESP32 I²C pins and multiple devices.

Install the library and the board

  1. 1If the ESP32 is not in your Arduino IDE yet: Tools > Board > Boards Manager, search for esp32 and install 'esp32 by Espressif Systems'.
  2. 2Sketch > Include Library > Manage Libraries, search for "Adafruit INA219" and install it with its dependency "Adafruit BusIO".
  3. 3Select Tools > Board > esp32 > ESP32 Dev Module (or your exact board) and its port.

Sketch 1 - Read voltage, current and power

Upload it and open the Serial Monitor at 115200 baud. Wire.begin(21, 22) sets the I²C pins explicitly, so moving the sensor to other GPIOs is a one-line change.

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

Adafruit_INA219 ina219;             // I2C address 0x40

void setup() {
  Serial.begin(115200);
  Wire.begin(21, 22);               // SDA = GPIO21, SCL = GPIO22

  if (!ina219.begin()) {
    Serial.println("INA219 not found - check wiring and I2C address");
    while (1) delay(10);
  }

  ina219.setCalibration_32V_2A();   // 0.1 ohm shunt, up to ~3.2 A
  Serial.println("INA219 ready");
}

void loop() {
  float shunt_mV   = ina219.getShuntVoltage_mV();
  float bus_V      = ina219.getBusVoltage_V();
  float current_mA = ina219.getCurrent_mA();
  float power_mW   = ina219.getPower_mW();

  Serial.printf("Bus: %.2f V   Shunt: %.2f mV   Current: %.1f mA   Power: %.1f mW\n",
    bus_V, shunt_mV, current_mA, power_mW);

  delay(500);
}

Tip

Line by line this is the same sequence as the Uno version, with printf for compact output. The Uno guide has a table explaining every call, including why current can read negative.

Sketch 2 - Live power and energy page on your phone

A Serial Monitor needs a cable. This sketch joins your Wi-Fi and serves a small page showing voltage, current, power, total energy in Wh and the peak current, updated every second, with a button to reset the totals. It samples the INA219 ten times a second and integrates energy using the real time between samples, so the Wh figure keeps counting accurately whether or not a browser is open.

sketch.ino
#include <WiFi.h>
#include <WebServer.h>
#include <Wire.h>
#include <Adafruit_INA219.h>

const char* WIFI_SSID     = "YOUR_WIFI_NAME";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";

Adafruit_INA219 ina219;
WebServer server(80);

float volts = 0, current_mA = 0, peak_mA = 0;
double mWh = 0;
unsigned long lastSample;

// 10 samples a second; energy uses the real elapsed time
void sample() {
  unsigned long now = millis();
  if (now - lastSample < 100) return;
  double hours = (now - lastSample) / 3600000.0;
  lastSample = now;
  volts      = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  if (current_mA > peak_mA) peak_mA = current_mA;
  mWh += volts * current_mA * hours;
}

void handleData() {
  char json[160];
  snprintf(json, sizeof(json),
    "{\"v\":%.3f,\"mA\":%.1f,\"W\":%.3f,\"Wh\":%.4f,\"peak_mA\":%.1f,\"uptime_s\":%lu}",
    volts, current_mA, volts * current_mA / 1000.0, mWh / 1000.0, peak_mA, millis() / 1000);
  server.send(200, "application/json", json);
}

void handleReset() {
  mWh = 0;
  peak_mA = 0;
  server.send(204);
}

const char PAGE[] = R"HTML(<!DOCTYPE html><html><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>INA219 power meter</title>
<style>
body{font-family:sans-serif;margin:1.5em;background:#0b1622;color:#e8f1fb}
.grid{display:grid;grid-template-columns:1fr 1fr;gap:12px}
.card{background:#12243a;border-radius:10px;padding:14px}
.v{font-size:2em;font-weight:bold}
button{margin-top:16px;padding:10px 18px;font-size:1em}
</style></head><body>
<h1>INA219 power meter</h1>
<div class="grid">
<div class="card">Voltage<div class="v"><span id="v">--</span> V</div></div>
<div class="card">Current<div class="v"><span id="mA">--</span> mA</div></div>
<div class="card">Power<div class="v"><span id="W">--</span> W</div></div>
<div class="card">Energy<div class="v"><span id="Wh">--</span> Wh</div></div>
</div>
<p>Peak current: <span id="peak">--</span> mA</p>
<button onclick="fetch('/reset',{method:'POST'})">Reset energy and peak</button>
<script>
const $ = (id) => document.getElementById(id);
async function refresh() {
  const d = await (await fetch('/data')).json();
  $('v').textContent = d.v.toFixed(2);
  $('mA').textContent = d.mA.toFixed(1);
  $('W').textContent = d.W.toFixed(2);
  $('Wh').textContent = d.Wh.toFixed(3);
  $('peak').textContent = d.peak_mA.toFixed(0);
}
refresh();
setInterval(refresh, 1000);
</script></body></html>)HTML";

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

  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Open http://");
  Serial.println(WiFi.localIP());

  server.on("/", []() { server.send(200, "text/html", PAGE); });
  server.on("/data", handleData);
  server.on("/reset", HTTP_POST, handleReset);
  server.begin();
  lastSample = millis();
}

void loop() {
  sample();
  server.handleClient();
}

How it works. sample() runs from loop() every 100 ms and adds volts × milliamps × hours to the energy total, so energy keeps accumulating between page refreshes. /data returns everything as JSON, so Node-RED, Home Assistant or a script can poll it too. The page itself is static and only fetches numbers, which keeps the ESP32's work tiny. Totals reset on reboot; to keep history, publish the readings over MQTT to Home Assistant or stream them with WebSockets.

Tip

The ESP32 only joins 2.4 GHz Wi-Fi. If it never connects, check that your router broadcasts a 2.4 GHz network and that the name and password are exact.

Sketch 3 - Electronic fuse with a MOSFET or relay

A stalled motor or a shorted LED strip can draw far more than it should. This sketch watches the current and switches the load off when it stays above a limit for longer than a set time (200 ms here), which ignores the normal inrush when a motor starts. Once tripped, the load stays off until you press the DevKit's BOOT button, so a fault cannot keep cycling on and off.

Extra wiring for Sketch 3

  • MOSFET module IN / SIG (or relay IN)GPIO26

    Notes
    HIGH = load on. Many relay modules are active-LOW: swap HIGH and LOW in the sketch if yours is
  • MOSFET module VCC / GND3V3 / GND

    Notes
    Use a logic-level MOSFET; most 5 V relay modules do not switch reliably from 3.3 V
  • Load supply (+)Fuse → INA219 VIN+

    Notes
    VIN− then goes to the load (+) as usual
  • Load (−)MOSFET module OUT− / switched terminal

    Notes
    Low-side switching; all grounds joined
sketch.ino
#include <Wire.h>
#include <Adafruit_INA219.h>

Adafruit_INA219 ina219;

const uint8_t LOAD_PIN  = 26;          // MOSFET or relay input: HIGH = load on
const uint8_t RESET_PIN = 0;           // the DevKit's BOOT button, LOW when pressed
const float   LIMIT_mA  = 800;         // trip above this...
const unsigned long HOLD_MS = 200;     // ...if it lasts this long (ignores inrush)

bool tripped = false;
unsigned long overSince = 0;           // 0 = currently under the limit

void setup() {
  Serial.begin(115200);
  pinMode(LOAD_PIN, OUTPUT);
  digitalWrite(LOAD_PIN, LOW);
  pinMode(RESET_PIN, INPUT_PULLUP);
  Wire.begin(21, 22);
  if (!ina219.begin()) {
    Serial.println("INA219 not found - load stays off");
    while (1) delay(10);
  }
  ina219.setCalibration_32V_2A();
  digitalWrite(LOAD_PIN, HIGH);
  Serial.println("Load on");
}

void loop() {
  float mA = ina219.getCurrent_mA();

  if (!tripped) {
    if (mA > LIMIT_mA) {
      if (overSince == 0) overSince = millis();
      if (millis() - overSince >= HOLD_MS) {
        digitalWrite(LOAD_PIN, LOW);
        tripped = true;
        Serial.printf("TRIPPED at %.0f mA. Press BOOT to switch the load back on.\n", mA);
      }
    } else {
      overSince = 0;
    }
  } else if (digitalRead(RESET_PIN) == LOW) {
    delay(50);                         // debounce
    tripped = false;
    overSince = 0;
    digitalWrite(LOAD_PIN, HIGH);
    Serial.println("Reset: load on");
    while (digitalRead(RESET_PIN) == LOW) delay(10);
  }
  delay(5);
}

Warning

This is a protection add-on, not a replacement for a real fuse. A dead short can melt wires in milliseconds, faster than code, a relay or the INA219's conversion can react. Keep the fuse in the supply line and set LIMIT_mA below both the shunt's and the load's ratings.

Tuning it. Set LIMIT_mA a little above the load's normal running current (watch Sketch 1 or 2 first). If a motor trips when it starts, raise HOLD_MS; if a fault takes too long to trip, lower it. To get alerts, combine this sketch with Sketch 2's web page or publish the trip over MQTT.

Troubleshooting on the ESP32

  • 'INA219 not found'

    Likely cause
    Wrong pins in Wire.begin(), VCC on 5V instead of 3V3, or a loose header
    Fix
    SDA to GPIO21, SCL to GPIO22 (or update Wire.begin), VCC to 3V3; run an I²C scanner
  • ESP32 resets when the load switches on

    Likely cause
    Inrush or the shunt drop sags the board's supply
    Fix
    Power the ESP32 from its own USB or regulator, join grounds, add 100 µF near the board
  • Web page never loads

    Likely cause
    Different network, 5 GHz Wi-Fi, or the wrong IP
    Fix
    Use the IP printed in the Serial Monitor; the ESP32 is 2.4 GHz only
  • Energy total jumps on the first sample

    Likely cause
    lastSample not set before the first reading
    Fix
    Keep lastSample = millis() at the end of setup(), as in Sketch 2
  • Fuse trips every time the motor starts

    Likely cause
    Inrush lasts longer than HOLD_MS
    Fix
    Raise HOLD_MS to 300 to 500 ms, or raise LIMIT_mA
  • Relay never clicks

    Likely cause
    5 V relay module driven from 3.3 V, or an active-LOW input
    Fix
    Use a logic-level MOSFET module, or invert the HIGH/LOW levels

ESP32 FAQ

Can I use several INA219s on one ESP32? Yes: give them different addresses with the A0 / A1 pads (0x40, 0x41, 0x44, 0x45). The ESP32 also has a second I²C bus (Wire1) on any two free GPIOs if you run out of addresses.

Does it work with MicroPython? Yes. Community MicroPython drivers for the INA219 use the same registers and calibration; the wiring is identical.

Can I measure the ESP32's deep-sleep current with it? Not with the standard 0.1 Ω shunt: sleep current is microamps, below the 0.1 mA step and the offset. Use a 1 Ω or larger shunt, or a dedicated tool; the ESP32 deep sleep battery guide covers measurement methods.

Will the web page or fuse keep working without Wi-Fi? The fuse in Sketch 3 needs no Wi-Fi at all. Sketch 2 keeps counting energy even if the browser disconnects, but it waits for Wi-Fi at start-up.

Where next? Build the battery capacity tester in the INA219 Uno guide, or compare current sensors in the INA219 overview.

Keep learning

Related guides & projects