Arduino library for interfacing with the Texas Instruments INA219 zero-drift, bidirectional current and power monitor with I2C interface.
The INA219 measures the voltage across a shunt resistor and the bus supply voltage, and computes current and power on chip. It supports:
- Bus voltage measurement up to 26 V (16 V or 32 V range) with 4 mV resolution
- Shunt voltage measurement with programmable gain (±40 mV to ±320 mV full scale) and 10 µV resolution
- On-chip current and power calculation using a calibration register
- Configurable ADC resolution (9 to 12 bits) and averaging (up to 128 samples)
- Continuous and triggered operating modes, with power-down
- Math overflow and conversion ready flags
- 16 selectable I2C addresses (0x40 to 0x4F)
- High accuracy: ±0.5% maximum gain error, 100 µV maximum offset
This library provides a simple interface to access these features via I2C. Note that the INA219 has no alert pin and no identification register.
Install via the Arduino Library Manager by searching for "Sitron Labs INA219".
Alternatively, install manually:
- Download or clone this repository
- Place it in your Arduino
librariesfolder - Restart the Arduino IDE
Install via the PlatformIO Library Manager by searching for "Sitron Labs INA219".
Alternatively, add the library manually to your platformio.ini file:
lib_deps =
https://github.com/sitronlabs/SitronLabs_TexasInstruments_INA219_Arduino_Library.gitConnect the INA219 to your Arduino using I2C:
- VS → 3.3V or 5V (3 V to 5.5 V)
- GND → GND
- SDA → SDA (I2C Data)
- SCL → SCL (I2C Clock)
- VIN+ → Supply side of the shunt resistor
- VIN- → Load side of the shunt resistor
The I2C address is selected with the A0 and A1 pins (0x40 when both are tied to GND):
| A1 | A0 | Address |
|---|---|---|
| GND | GND | 0x40 |
| GND | VS | 0x41 |
| GND | SDA | 0x42 |
| GND | SCL | 0x43 |
| VS | GND | 0x44 |
| VS | VS | 0x45 |
| VS | SDA | 0x46 |
| VS | SCL | 0x47 |
| SDA | GND | 0x48 |
| SDA | VS | 0x49 |
| SDA | SDA | 0x4A |
| SDA | SCL | 0x4B |
| SCL | GND | 0x4C |
| SCL | VS | 0x4D |
| SCL | SDA | 0x4E |
| SCL | SCL | 0x4F |
#include <Wire.h>
#include <ina219.h>
// Create device object
ina219 sensor;
// I2C address (A0 and A1 tied to GND)
const uint8_t I2C_ADDRESS = 0x40;
void setup() {
Serial.begin(115200);
// Initialize I2C
Wire.begin();
// Setup the INA219 (I2C library, I2C address)
if (sensor.setup(Wire, I2C_ADDRESS) != 0) {
Serial.println("Failed to setup INA219");
return;
}
// Detect the device
if (!sensor.detect()) {
Serial.println("INA219 not detected");
return;
}
// Configure measurement settings
sensor.configure(INA219_BUS_VOLTAGE_RANGE_32V,
INA219_GAIN_8_320MV,
INA219_ADC_12BIT_532US,
INA219_ADC_16SAMPLES_8510US,
INA219_MODE_SHUNT_BUS_VOLTAGE_CONT);
// Calibrate for a 0.1 ohm shunt resistor and a maximum current of 2 A
sensor.calibrate(0.1, 2.0);
Serial.println("INA219 initialized");
}
void loop() {
float shunt_voltage, bus_voltage, current, power;
// Read all measurements
if (sensor.read_all(shunt_voltage, bus_voltage, current, power) == 0) {
Serial.print("Bus voltage: ");
Serial.print(bus_voltage, 3);
Serial.print(" V, Current: ");
Serial.print(current, 3);
Serial.print(" A, Power: ");
Serial.print(power, 3);
Serial.println(" W");
}
delay(1000);
}#include <Wire.h>
#include <ina219.h>
ina219 sensor;
void setup() {
Serial.begin(115200);
Wire.begin();
sensor.setup(Wire, 0x40);
if (!sensor.detect()) {
Serial.println("INA219 not detected");
return;
}
// Triggered mode, 160 mV range for a 0.1 ohm shunt and a maximum current of 1 A
sensor.configure(INA219_BUS_VOLTAGE_RANGE_16V,
INA219_GAIN_4_160MV,
INA219_ADC_12BIT_532US,
INA219_ADC_12BIT_532US,
INA219_MODE_SHUNT_BUS_VOLTAGE_TRIG);
sensor.calibrate(0.1, 1.0);
}
void loop() {
// Start a conversion and wait for it to complete
sensor.trigger();
bool ready = false;
while (!ready) {
if (sensor.is_conversion_ready(ready) != 0) {
return;
}
}
float current;
if (sensor.read_current(current) == 0) {
Serial.print("Current: ");
Serial.print(current, 3);
Serial.println(" A");
}
delay(1000);
}Initializes the INA219 device for I2C communication.
i2c_library: I2C library instance to use (typicallyWire)i2c_address: I2C address of the device (typically 0x40)
Returns 0 on success, or a negative error code otherwise.
Detects if the INA219 device is present. The chip has no identification register, so it is reset and its configuration register is compared with the default value (0x399F). The chip must be configured and calibrated again afterwards.
Returns true if device is detected, false otherwise.
Performs a software reset of the chip. This also clears the calibration.
Returns 0 on success, or a negative error code otherwise.
Reads one or more bytes starting at the given register. The transaction is retried up to 3 times.
address: Register to read fromcontent: Buffer where the data is storedcount: Number of bytes to read (default: 1)
Returns 0 on success, or a negative error code otherwise.
Writes one or more bytes starting at the given register. The transaction is retried up to 3 times.
address: Register to write tocontent: Buffer containing the data to writecount: Number of bytes to write (default: 1)
Returns 0 on success, or a negative error code otherwise.
Configures the measurement settings.
bus_range:INA219_BUS_VOLTAGE_RANGE_16VorINA219_BUS_VOLTAGE_RANGE_32V(the voltage must not exceed 26 V in operation)gain:INA219_GAIN_1_40MV,INA219_GAIN_2_80MV,INA219_GAIN_4_160MVorINA219_GAIN_8_320MVbus_adc: Bus voltage ADC resolution or averaging (see ADC Settings)shunt_adc: Shunt voltage ADC resolution or averaging (see ADC Settings)operating_mode: Operating mode (see Operating Modes)
Returns 0 on success, or a negative error code otherwise.
Calibrates the chip for current and power measurements. The maximum expected current multiplied by the shunt resistance should not exceed the full scale range selected with the gain.
shunt_resistance_ohms: Shunt resistor value in ohmsmax_expected_current_amps: Maximum expected current in amperes
Returns 0 on success, -EINVAL if the values are invalid or cannot be represented by the calibration register, or a negative error code otherwise.
Starts a new conversion when a triggered operating mode is selected.
Returns 0 on success, or a negative error code otherwise.
Reads the voltage across the shunt resistor (10 µV resolution).
voltage: Output parameter for the shunt voltage in volts
Returns 0 on success, or a negative error code otherwise.
Reads the bus voltage (4 mV resolution).
voltage: Output parameter for the bus voltage in volts
Returns 0 on success, or a negative error code otherwise.
Reads the calculated current.
current: Output parameter for the current in amperes
Returns 0 on success, -EINVAL if the chip has not been calibrated, or a negative error code otherwise.
Reads the calculated power. This also clears the conversion ready flag.
power: Output parameter for the power in watts
Returns 0 on success, -EINVAL if the chip has not been calibrated, or a negative error code otherwise.
Reads shunt voltage, bus voltage, current and power in one call.
Returns 0 on success, or a negative error code otherwise.
Reads the math overflow flag. When set, the current and power values are meaningless, usually because the shunt voltage exceeds the range selected by the gain.
Returns 0 on success, or a negative error code otherwise.
Reads the conversion ready flag, useful in triggered mode. The flag is cleared by reading the power register or by triggering a new conversion.
Returns 0 on success, or a negative error code otherwise.
| Value | Resolution / averaging | Conversion time |
|---|---|---|
INA219_ADC_9BIT_84US |
9 bits | 84 µs |
INA219_ADC_10BIT_148US |
10 bits | 148 µs |
INA219_ADC_11BIT_276US |
11 bits | 276 µs |
INA219_ADC_12BIT_532US |
12 bits | 532 µs |
INA219_ADC_2SAMPLES_1060US |
2 samples | 1.06 ms |
INA219_ADC_4SAMPLES_2130US |
4 samples | 2.13 ms |
INA219_ADC_8SAMPLES_4260US |
8 samples | 4.26 ms |
INA219_ADC_16SAMPLES_8510US |
16 samples | 8.51 ms |
INA219_ADC_32SAMPLES_17020US |
32 samples | 17.02 ms |
INA219_ADC_64SAMPLES_34050US |
64 samples | 34.05 ms |
INA219_ADC_128SAMPLES_68100US |
128 samples | 68.10 ms |
INA219_MODE_POWER_DOWN: Power-downINA219_MODE_SHUNT_VOLTAGE_TRIG: Shunt voltage, triggeredINA219_MODE_BUS_VOLTAGE_TRIG: Bus voltage, triggeredINA219_MODE_SHUNT_BUS_VOLTAGE_TRIG: Shunt and bus voltage, triggeredINA219_MODE_ADC_OFF: ADC offINA219_MODE_SHUNT_VOLTAGE_CONT: Shunt voltage, continuousINA219_MODE_BUS_VOLTAGE_CONT: Bus voltage, continuousINA219_MODE_SHUNT_BUS_VOLTAGE_CONT: Shunt and bus voltage, continuous
- Sensor not detected: check the I2C connections, the I2C address (see the table above) and the supply voltage (3 V to 5.5 V).
- Wrong current or power: check the shunt value passed to
calibrate(), and choose the smallest gain whose range covers the maximum current times the shunt resistance. Useread_overflow()to check for saturation. - -EINVAL when reading current or power:
calibrate()has not been called, or was undone by a laterreset()ordetect().
- Communication interface: I2C
- I2C address: 0x40 to 0x4F (selectable)
- Supply voltage: 3 V to 5.5 V
- Bus voltage range: 0 V to 26 V (16 V or 32 V full scale)
- Shunt voltage range: ±40 mV to ±320 mV
- Bus voltage resolution: 4 mV
- Shunt voltage resolution: 10 µV
- Gain error: ±0.5% maximum
- Offset: 100 µV maximum