Arduino library 路 Sensors

Adafruit INA219: current, voltage and power on I2C

The INA219 from Texas Instruments measures current by the tiny voltage across a 0.1 ohm shunt resistor in the supply line, and the voltage of the line itself, and reports both over I2C. The Adafruit INA219 library turns the chip鈥檚 registers into milliamps, volts and milliwatts, which makes it the usual choice for measuring what a motor, LED strip or solar panel draws.

Built in on Mokxi: #include <Adafruit_INA219.h> and it builds, with nothing to install. Free, and no account needed.

What Adafruit INA219 does

Unlike a Hall sensor, the INA219 sits in the high side of the circuit: the supply goes into VIN+ and out of VIN- to the load. It measures up to 26 V on the bus. The default calibration covers 32 V and 2 A; setCalibration_32V_1A() and setCalibration_16V_400mA() trade range for finer steps when the current is small.

Bus voltage is measured at VIN-, after the shunt. The voltage the load really sees is that bus voltage, and the supply voltage is the bus voltage plus the shunt voltage, which is how the library鈥檚 example works out "load voltage".

Library Manager nameAdafruit INA219
AuthorAdafruit
Include#include <Adafruit_INA219.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesCurrent monitor, INA219

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <Adafruit_INA219.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Adafruit INA219" by Adafruit from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit INA219 to lib_deps in PlatformIO. Mokxi follows the calls of release 1.2.3, so a sketch written here builds there too.

ESP32-C3 路 Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 ESP32-C6 路 Raspberry Pi Pico 路 Raspberry Pi Pico W 路 Seeed XIAO SAMD21 路 STM32 路 STM32 Nucleo-F411RE 路 STM32 Blue Pill 路 micro:bit V2 路 Arduino Uno R4 路 ESP32 DevKit 路 ESP32-S3-DevKitC-1 路 ESP8266 NodeMCU 路 Arduino Pro Mini 3.3 V 路 Seeed XIAO ESP32-C3 路 ESP32-C3 SuperMini 路 Seeed XIAO ESP32-C6 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 Arduino Nano RP2040 Connect 路 Seeed XIAO RP2040 路 Waveshare RP2040-Zero 路 Arduino Uno R4 WiFi

The calls you will use

In the order a sketch meets them.

Adafruit_INA219 ina219

Creates the sensor at the default address 0x40. Adafruit_INA219 ina219(0x41) for a board with A0 bridged.

ina219.begin()

Starts I2C and applies the 32 V, 2 A calibration. Returns false when the chip does not answer.

ina219.getCurrent_mA()

Current through the shunt in milliamps, negative when it flows backward.

ina219.getBusVoltage_V()

The voltage on VIN- relative to ground, in volts.

float bus = ina219.getBusVoltage_V();
float shunt = ina219.getShuntVoltage_mV();
float load = bus + shunt / 1000;
Serial.println(load);

ina219.getShuntVoltage_mV()

The voltage across the shunt in millivolts, at most 320 mV.

ina219.getPower_mW()

Power in milliwatts, worked out by the chip from the current and the bus voltage.

ina219.setCalibration_16V_400mA()

A finer range for small loads: 16 V and 400 mA in 0.1 mA steps. setCalibration_32V_1A() is between the two.

Example: Current and voltage

On the ESP32-C3-DevKitM-1. It runs in the browser exactly as printed.

sketch.ino 路 ESP32-C3-DevKitM-1
// A load's current and its voltage, two ways: an INA219 at 0x40 and an
// ADS1115 at 0x48 on the ESP32-C3's I2C pins, GPIO 8 and 9.
//
// The INA219 is in series with the load: 3.3 V into VIN+, VIN- on to a 220
// ohm resistor to ground. Its 0.1 ohm shunt is a real resistor in the
// circuit, and the chip reads the few millivolts across it to 10 uV, and the
// voltage on the load side to 4 mV. The library writes the chip's calibration
// in begin(); without it the current register reads zero whatever flows.
//
// The ADS1115's A0 is on the same load-side node, so the two chips measure
// the same voltage: the INA219 in 4 mV steps, the ADS1115 at GAIN_ONE in
// 125 uV steps, a single-shot conversion taking 7.8 ms at the default rate.
//
// Change the resistor in the properties panel and watch both follow.

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

Adafruit_INA219 ina219;
Adafruit_ADS1115 ads;

void setup() {
  Serial.begin(115200);
  if (!ina219.begin()) Serial.println("no INA219 at 0x40");
  ads.setGain(GAIN_ONE);
  if (!ads.begin()) Serial.println("no ADS1115 at 0x48");
}

void loop() {
  int16_t counts = ads.readADC_SingleEnded(0);
  Serial.print("load ");
  Serial.print(ina219.getCurrent_mA());
  Serial.print(" mA, ");
  Serial.print(ina219.getPower_mW());
  Serial.print(" mW   INA219 says ");
  Serial.print(ina219.getBusVoltage_V());
  Serial.print(" V, ADS1115 says ");
  Serial.print(ads.computeVolts(counts));
  Serial.print(" V (");
  Serial.print(counts);
  Serial.println(" counts)");
  delay(500);
}

Open it in the editor, wired and ready to run 路 Current and voltage: the project page

Common errors and how to fix them

Failed to find INA219 chip

begin() returned false. Check SDA and SCL, the address jumpers (0x40 to 0x45), and that the module鈥檚 VCC is powered, not only VIN+.

Current reads 0 while the load runs

The load current is not going through the shunt. The supply must go into VIN+ and leave from VIN- to the load; the load鈥檚 ground and the board鈥檚 ground must be joined.

Current is negative

VIN+ and VIN- are swapped, so current flows backward through the shunt. Swap the two wires, or flip the sign in the sketch if the direction is intended.

Projects that use Adafruit INA219

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

Try Adafruit INA219 without installing anything

Open the example, change the sketch, and keep your version in a free account.