Skip to content

About

Arduino library for the Texas Instruments INA219 Voltage, Current and Power Monitor with I2C interface.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Designed by Sitron Labs Join the Discord community PayPal Donate License Latest Release Arduino Library Manager PlatformIO Registry

Sitron Labs Texas Instruments INA219 Arduino Library

Arduino library for interfacing with the Texas Instruments INA219 zero-drift, bidirectional current and power monitor with I2C interface.

Description

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.

Installation

Arduino IDE

Install via the Arduino Library Manager by searching for "Sitron Labs INA219".

Alternatively, install manually:

  1. Download or clone this repository
  2. Place it in your Arduino libraries folder
  3. Restart the Arduino IDE

PlatformIO

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.git

Hardware Connections

I2C Interface

Connect 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

Usage

Basic Example

#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);
}

Triggered Example

#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);
}

API Reference

setup(TwoWire &i2c_library, uint8_t i2c_address)

Initializes the INA219 device for I2C communication.

  • i2c_library: I2C library instance to use (typically Wire)
  • i2c_address: I2C address of the device (typically 0x40)

Returns 0 on success, or a negative error code otherwise.

detect(void)

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.

reset(void)

Performs a software reset of the chip. This also clears the calibration.

Returns 0 on success, or a negative error code otherwise.

register_read(enum ina219_register address, uint8_t *content, size_t count)

Reads one or more bytes starting at the given register. The transaction is retried up to 3 times.

  • address: Register to read from
  • content: Buffer where the data is stored
  • count: Number of bytes to read (default: 1)

Returns 0 on success, or a negative error code otherwise.

register_write(enum ina219_register address, const uint8_t *content, size_t count)

Writes one or more bytes starting at the given register. The transaction is retried up to 3 times.

  • address: Register to write to
  • content: Buffer containing the data to write
  • count: Number of bytes to write (default: 1)

Returns 0 on success, or a negative error code otherwise.

configure(bus_range, gain, bus_adc, shunt_adc, operating_mode)

Configures the measurement settings.

  • bus_range: INA219_BUS_VOLTAGE_RANGE_16V or INA219_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_160MV or INA219_GAIN_8_320MV
  • bus_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.

calibrate(float shunt_resistance_ohms, float max_expected_current_amps)

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 ohms
  • max_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.

trigger(void)

Starts a new conversion when a triggered operating mode is selected.

Returns 0 on success, or a negative error code otherwise.

read_shunt_voltage(float &voltage)

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.

read_bus_voltage(float &voltage)

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.

read_current(float &current)

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.

read_power(float &power)

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.

read_all(float &shunt_voltage, float &bus_voltage, float &current, float &power)

Reads shunt voltage, bus voltage, current and power in one call.

Returns 0 on success, or a negative error code otherwise.

read_overflow(bool &overflow)

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.

is_conversion_ready(bool &ready)

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.

ADC Settings

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

Operating Modes

  • INA219_MODE_POWER_DOWN: Power-down
  • INA219_MODE_SHUNT_VOLTAGE_TRIG: Shunt voltage, triggered
  • INA219_MODE_BUS_VOLTAGE_TRIG: Bus voltage, triggered
  • INA219_MODE_SHUNT_BUS_VOLTAGE_TRIG: Shunt and bus voltage, triggered
  • INA219_MODE_ADC_OFF: ADC off
  • INA219_MODE_SHUNT_VOLTAGE_CONT: Shunt voltage, continuous
  • INA219_MODE_BUS_VOLTAGE_CONT: Bus voltage, continuous
  • INA219_MODE_SHUNT_BUS_VOLTAGE_CONT: Shunt and bus voltage, continuous

Troubleshooting

  • 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. Use read_overflow() to check for saturation.
  • -EINVAL when reading current or power: calibrate() has not been called, or was undone by a later reset() or detect().

Specifications

  • 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

About

Arduino library for the Texas Instruments INA219 Voltage, Current and Power Monitor with I2C interface.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages