The INA219 measures the voltage across a tiny shunt resistor and the bus voltage, and reports current and power over I2C — ideal for solar, battery and router-power monitors.
The I2C side is four wires to the ESP32 at 3.3 V. The measurement side goes in series with the positive line of the DC circuit you want to watch. The sketch prints load voltage, current and power every second.
Wiring
| ESP32 DevKit V1 | INA219 DC current/voltage sensor | Wire | Notes |
|---|---|---|---|
| 3V3 | VCC | red | |
| GND | GND | black | |
| D22 | SCL | yellow | |
| D21 | SDA | blue |
3.0–5.5 V part, 3.3 V board. Power VCC from your board's logic voltage (3.3 V or 5 V); the I2C pull-ups go to VCC, so they match your board.
Board quirks
- VIN+ and VIN− go in series with the positive wire of the circuit you measure (supply + → VIN+, VIN− → load +). The load’s ground must be joined to the ESP32 GND.
- The default 0.1 Ω shunt measures up to about ±3.2 A; the sketch uses the 32 V / 2 A calibration for 0.8 mA resolution.
- DC only, 26 V maximum on VIN+/VIN− — never mains.
Code
// INA219 current / voltage / power monitor on an ESP32 DevKit V1.
// VCC → 3V3, GND → GND, SDA → D21, SCL → D22. Put the load's positive line through VIN+ → VIN−.
#include <Wire.h>
#include <Adafruit_INA219.h>
Adafruit_INA219 ina219(0x40);
void setup() {
Serial.begin(115200);
Wire.begin(21, 22); // SDA, SCL
if (!ina219.begin(&Wire)) {
Serial.println("INA219 not found at 0x40 — check wiring and the A0/A1 address jumpers");
while (true) delay(1000);
}
ina219.setCalibration_32V_2A(); // default 0.1 Ω shunt: up to 32 V, ±3.2 A, 0.8 mA steps
Serial.println("INA219 ready");
}
void loop() {
float busV = ina219.getBusVoltage_V();
float shuntmV = ina219.getShuntVoltage_mV();
float mA = ina219.getCurrent_mA();
float mW = ina219.getPower_mW();
Serial.printf("Load %.2f V Shunt %.2f mV Current %.1f mA Power %.0f mW\n", busV + shuntmV / 1000.0F, shuntmV, mA, mW);
delay(1000);
}// INA219 current / voltage / power monitor on an ESP32 DevKit V1.
// VCC → 3V3, GND → GND, SDA → D21, SCL → D22. Put the load's positive line through VIN+ → VIN−.
#include <Wire.h>
#include <Adafruit_INA219.h>
Adafruit_INA219 ina219(0x40);
void setup() {
Serial.begin(115200);
Wire.begin(21, 22); // SDA, SCL
if (!ina219.begin(&Wire)) {
Serial.println("INA219 not found at 0x40 — check wiring and the A0/A1 address jumpers");
while (true) delay(1000);
}
ina219.setCalibration_32V_2A(); // default 0.1 Ω shunt: up to 32 V, ±3.2 A, 0.8 mA steps
Serial.println("INA219 ready");
}
void loop() {
float busV = ina219.getBusVoltage_V();
float shuntmV = ina219.getShuntVoltage_mV();
float mA = ina219.getCurrent_mA();
float mW = ina219.getPower_mW();
Serial.printf("Load %.2f V Shunt %.2f mV Current %.1f mA Power %.0f mW\n", busV + shuntmV / 1000.0F, shuntmV, mA, mW);
delay(1000);
}Library: Adafruit INA219 (Adafruit) — compiled with Adafruit INA219 1.2.3.
Expected serial output
INA219 ready
Load 12.03 V Shunt 18.40 mV Current 184.0 mA Power 2214 mW
Load 12.03 V Shunt 18.30 mV Current 183.0 mA Power 2202 mWINA219 ready Load 12.03 V Shunt 18.40 mV Current 184.0 mA Power 2214 mW Load 12.03 V Shunt 18.30 mV Current 183.0 mA Power 2202 mW
Common errors
Current always 0 mA
VIN+/VIN− are not in series with the load, or the load ground is not shared with the ESP32.
Negative current
VIN+ and VIN− are swapped — supply goes to VIN+.