ina219 - Driver for INA219/INA220 bidirectional current/power monitor

group ina219

ESP-IDF driver for INA219/INA220 Zerø-Drift, Bidirectional Current/Power Monitor.

Copyright (C) 2019 Ruslan V. Uss unclerus@gmail.com

BSD Licensed as described in the file LICENSE

Defines

INA219_ADDR_GND_GND

I2C address, A1 pin - GND, A0 pin - GND.

INA219_ADDR_GND_VS

I2C address, A1 pin - GND, A0 pin - VS+.

INA219_ADDR_GND_SDA

I2C address, A1 pin - GND, A0 pin - SDA.

INA219_ADDR_GND_SCL

I2C address, A1 pin - GND, A0 pin - SCL.

INA219_ADDR_VS_GND

I2C address, A1 pin - VS+, A0 pin - GND.

INA219_ADDR_VS_VS

I2C address, A1 pin - VS+, A0 pin - VS+.

INA219_ADDR_VS_SDA

I2C address, A1 pin - VS+, A0 pin - SDA.

INA219_ADDR_VS_SCL

I2C address, A1 pin - VS+, A0 pin - SCL.

INA219_ADDR_SDA_GND

I2C address, A1 pin - SDA, A0 pin - GND.

INA219_ADDR_SDA_VS

I2C address, A1 pin - SDA, A0 pin - VS+.

INA219_ADDR_SDA_SDA

I2C address, A1 pin - SDA, A0 pin - SDA.

INA219_ADDR_SDA_SCL

I2C address, A1 pin - SDA, A0 pin - SCL.

INA219_ADDR_SCL_GND

I2C address, A1 pin - SCL, A0 pin - GND.

INA219_ADDR_SCL_VS

I2C address, A1 pin - SCL, A0 pin - VS+.

INA219_ADDR_SCL_SDA

I2C address, A1 pin - SCL, A0 pin - SDA.

INA219_ADDR_SCL_SCL

I2C address, A1 pin - SCL, A0 pin - SCL.

Enums

enum ina219_bus_voltage_range_t

Bus voltage range.

Values:

enumerator INA219_BUS_RANGE_16V

16V FSR

enumerator INA219_BUS_RANGE_32V

32V FSR (default)

enum ina219_gain_t

PGA gain for shunt voltage.

Values:

enumerator INA219_GAIN_1

Gain: 1, Range: +-40 mV.

enumerator INA219_GAIN_0_5

Gain: 1/2, Range: +-80 mV.

enumerator INA219_GAIN_0_25

Gain: 1/4, Range: +-160 mV.

enumerator INA219_GAIN_0_125

Gain: 1/8, Range: +-320 mV (default)

enum ina219_resolution_t

ADC resolution/averaging.

Values:

enumerator INA219_RES_9BIT_1S

9 bit, 1 sample, conversion time 84 us

enumerator INA219_RES_10BIT_1S

10 bit, 1 sample, conversion time 148 us

enumerator INA219_RES_11BIT_1S

11 bit, 1 sample, conversion time 276 us

enumerator INA219_RES_12BIT_1S

12 bit, 1 sample, conversion time 532 us (default)

enumerator INA219_RES_12BIT_2S

12 bit, 2 samples, conversion time 1.06 ms

enumerator INA219_RES_12BIT_4S

12 bit, 4 samples, conversion time 2.13 ms

enumerator INA219_RES_12BIT_8S

12 bit, 8 samples, conversion time 4.26 ms

enumerator INA219_RES_12BIT_16S

12 bit, 16 samples, conversion time 8.51 ms

enumerator INA219_RES_12BIT_32S

12 bit, 32 samples, conversion time 17.02 ms

enumerator INA219_RES_12BIT_64S

12 bit, 64 samples, conversion time 34.05 ms

enumerator INA219_RES_12BIT_128S

12 bit, 128 samples, conversion time 68.1 ms

enum ina219_mode_t

Operating mode.

Values:

enumerator INA219_MODE_POWER_DOWN

Power-done.

enumerator INA219_MODE_TRIG_SHUNT

Shunt voltage, triggered.

enumerator INA219_MODE_TRIG_BUS

Bus voltage, triggered.

enumerator INA219_MODE_TRIG_SHUNT_BUS

Shunt and bus, triggered.

enumerator INA219_MODE_DISABLED

ADC off (disabled)

enumerator INA219_MODE_CONT_SHUNT

Shunt voltage, continuous.

enumerator INA219_MODE_CONT_BUS

Bus voltage, continuous.

enumerator INA219_MODE_CONT_SHUNT_BUS

Shunt and bus, continuous (default)

Functions

esp_err_t ina219_init_desc(ina219_t *dev, uint8_t addr, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)

Initialize device descriptor.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • addr: Device I2C address

  • port: I2C port

  • sda_gpio: SDA GPIO

  • scl_gpio: SCL GPIO

esp_err_t ina219_free_desc(ina219_t *dev)

Free device descriptor.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

esp_err_t ina219_init(ina219_t *dev)

Init device.

Read current device configuration into dev->config

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

esp_err_t ina219_reset(ina219_t *dev)

Reset device.

Same as power-on reset. Resets all registers to default values. You still need to calibrate device to read current, otherwise only shunt voltage readings will be valid.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

esp_err_t ina219_configure(ina219_t *dev, ina219_bus_voltage_range_t u_range, ina219_gain_t gain, ina219_resolution_t u_res, ina219_resolution_t i_res, ina219_mode_t mode)

Set device configuration.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • u_range: Bus voltage range

  • gain: Shunt voltage gain

  • u_res: Bus voltage resolution and averaging

  • i_res: Shunt voltage resolution and averaging

  • mode: Device operational mode

esp_err_t ina219_get_bus_voltage_range(ina219_t *dev, ina219_bus_voltage_range_t *range)

Get bus voltage range.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] range: Bus voltage range

esp_err_t ina219_get_gain(ina219_t *dev, ina219_gain_t *gain)

Get shunt voltage gain.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] gain: Shunt voltage gain

esp_err_t ina219_get_bus_voltage_resolution(ina219_t *dev, ina219_resolution_t *res)

Get bus voltage resolution and averaging.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] res: Bus voltage resolution and averaging

esp_err_t ina219_get_shunt_voltage_resolution(ina219_t *dev, ina219_resolution_t *res)

Get shunt voltage resolution and averaging.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] res: Shunt voltage resolution and averaging

esp_err_t ina219_get_mode(ina219_t *dev, ina219_mode_t *mode)

Get operating mode.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] mode: Operating mode

esp_err_t ina219_calibrate(ina219_t *dev, float i_expected_max, float r_shunt)

Perform calibration.

Current readings will be valid only after calibration

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • i_expected_max: Maximum expected current, A

  • r_shunt: Shunt resistance, Ohm

esp_err_t ina219_trigger(ina219_t *dev)

Trigger single conversion.

Function will return an error if current operating mode is not INA219_MODE_TRIG_SHUNT/INA219_MODE_TRIG_BUS/INA219_MODE_TRIG_SHUNT_BUS

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

esp_err_t ina219_get_bus_voltage(ina219_t *dev, float *voltage)

Read bus voltage.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] voltage: Bus voltage, V

esp_err_t ina219_get_shunt_voltage(ina219_t *dev, float *voltage)

Read shunt voltage.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] voltage: Shunt voltage, V

esp_err_t ina219_get_current(ina219_t *dev, float *current)

Read current.

This function works properly only after calibration.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] current: Current, A

esp_err_t ina219_get_power(ina219_t *dev, float *power)

Read power.

This function works properly only after calibration.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] power: Power, W

struct ina219_t
#include <ina219.h>

Device descriptor.