Home › Wiring › INA219 current sensor with ESP32

INA219 current sensor with ESP32

How to connect an INA219 current sensor to an ESP32: the wiring, a sketch that works, and a simulator to try it in your browser before you build it.

INA219 current sensor wired to an ESP32 in Zen Circuit Lab

INA219 breakout · 0.1 Ω shunt between VIN+ and VIN− in the high side · measures shunt voltage (±320 mV → ±3.2 A, 0.1 mA steps) and bus voltage (0–26 V) · I²C at 0x40 · 3–5.5 V

Wiring

INA219 current sensor pinESP32
VIN++ of the 12 V supply
VIN−− of the 12 V supply (through a 24 Ω resistor)
VCC3V3
GNDGND
SCLGPIO22
SDAGPIO21

The INA219 goes in the + wire (high side). Its 0.1 Ω shunt measures up to 3.2 A; the bus voltage can be up to 26 V.

Run it in the labFree, no account needed. The same wiring, with the sketch already on the board.

Code

From the project ESP32 DC power meter: INA219 and OLED. It also compiles in the Arduino IDE (ESP32 core 3.x) with the libraries it includes.

// Power meter: an INA219 (I2C 0x40) in the + wire of a 12 V load measures volts, amps and watts; the OLED (0x3C) shows them
// with the energy used since power-up. Both share I2C on GPIO21 (SDA) and GPIO22 (SCL).
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_INA219.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
Adafruit_INA219 ina;
float wh = 0;
unsigned long last = 0;

void setup() {
  Serial.begin(115200);
  Wire.begin(21, 22);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) Serial.println("OLED not found");
  if (!ina.begin()) Serial.println("INA219 not found");
  last = millis();
}

void loop() {
  float v = ina.getBusVoltage_V();
  float a = ina.getCurrent_mA() / 1000.0;
  float w = v * a;
  unsigned long now = millis();
  wh += w * (now - last) / 3600000.0;
  last = now;
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(2);
  display.setCursor(0, 0);  display.print(v, 2); display.print(" V");
  display.setCursor(0, 22); display.print(a, 3); display.print(" A");
  display.setCursor(0, 44); display.print(w, 1); display.print(" W");
  display.display();
  Serial.print(v, 2); Serial.print(" V  "); Serial.print(a, 3); Serial.print(" A  "); Serial.print(w, 1); Serial.print(" W  "); Serial.print(wh, 4); Serial.println(" Wh");
  delay(500);
}

For teachers

The project runs in the browser on a laptop or a phone, so every student can build and test it before the class touches a real board. Assign it on the teacher's page and set what counts as done: a prediction, a hidden fault found, or a passing test sheet.

Related