INA219 (ina219 v2.0.1)

INA219 Driver using Wafer.

Link to this section Summary

Functions

Acquire a connection to the INA219 device using the passed in I2C connection.

Retrieve the bus ADC resolution and averaging.

Set the bus ADC resolution and averaging.

Returns the bus voltage in V.

Retrieve the configured bus voltage range.

Set the configured bus voltage range to either 16 or 32V.

Get the calibration value.

Set the calibration value.

This is a convenient helper function to set the configuration to the right values for 16V input and 400mA current range at the highest resolution (0.1mA). This only works for devices with a 0.1Ω shunt resistor (ie Adafruit's breakout).

This is a convenient helper function to set the configuration to the right values for 32V input voltage range and 1A current range at the cost of accuracy. This only works for devices with a 0.1Ω shunt resistor (ie Adafruit's breakout).

This is a convenient helper function to set the configuration to the right values for 32V input voltage range and 2A current range at the cost of accuracy. This only works for devices with a 0.1Ω shunt resistor (ie Adafruit's breakout).

Are new samples ready since the last time you read them? Calling this function will clear the value until next time new samples are ready.

Returns the current in mA.

Returns true when power or current calculations are out of range. This indicates that current and power data may be meaningless.

Retrieve the current operating mode of the device.

Set the current operating mode for the device.

Returns the power in mW

Power-on-reset the device,

Retrieve the shunt ADC resolution.

Set the shunt ADC resolution and averaging.

Returns the shunt voltage in mV.

Retrieve the configured shunt voltage PGA.

Set the configured shunt voltage PGA to a configured value.

Link to this section Types

Link to this type

acquire_option()

@type acquire_option() ::
  {:conn, Wafer.Conn.t()}
  | {:current_divisor, number()}
  | {:power_divisor, number()}
Link to this type

acquire_options()

@type acquire_options() :: [acquire_option()]
@type milliamps() :: float()
Link to this type

millivolts()

@type millivolts() :: float()
Link to this type

milliwatts()

@type milliwatts() :: float()
Link to this type

operating_mode()

@type operating_mode() ::
  :power_down
  | :shunt_voltage_triggered
  | :bus_voltage_triggered
  | :adc_off
  | :shunt_voltage_continuous
  | :bus_voltage_continuous
  | :shunt_and_bus_voltage_continuous
Link to this type

sample_averaging()

@type sample_averaging() :: 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128
Link to this type

sample_resolution()

@type sample_resolution() :: 9 | 10 | 12
Link to this type

shunt_voltage_pga()

@type shunt_voltage_pga() :: 1 | 2 | 4 | 8
@type t() :: %INA219{
  conn: Wafer.Conn.t(),
  current_divisor: number(),
  power_divisor: number()
}
@type volts() :: float()

Link to this section Functions

Link to this function

acquire(options)

@spec acquire(acquire_options()) :: {:ok, t()} | {:error, reason :: any()}

Acquire a connection to the INA219 device using the passed in I2C connection.

Options:

  • conn an I2C connection, probably from ElixirALE.I2C or Circuits.I2C.
  • current_divisor see INA219.Commands for more information.
  • power_divisor see INA219.Commands for more information.
Link to this function

bus_adc_resolution_and_averaging(conn)

@spec bus_adc_resolution_and_averaging(t()) ::
  {:ok, {sample_averaging(), sample_resolution()}} | {:error, reason :: any()}

Retrieve the bus ADC resolution and averaging.

Returns the number of samples and the resolution as a two element tuple. eg {1, 9} refers to 9-bit ADC resolution and 1-sample averaging.

Link to this function

bus_adc_resolution_and_averaging(conn, arg)

@spec bus_adc_resolution_and_averaging(
  t(),
  {sample_averaging(), sample_resolution()}
) :: {:ok, t()} | {:error, reason :: any()}

Set the bus ADC resolution and averaging.

Expects a tuple of the form {samples, bits}. Be aware that changing this value affects the amount of time it takes to compute values. Refer to the datasheet for more information.

Valid values are:

  • { 1, 9} - 1 sample averaging and 9 bit ADC resolution.
  • { 1, 10} - 1 sample averaging and 10 bit ADC resolution.
  • { 1, 11} - 1 sample averaging and 11 bit ADC resolution.
  • { 1, 12} - 1 sample averaging and 12 bit ADC resolution.
  • { 2, 12} - 2 sample averaging and 12 bit ADC resolution.
  • { 4, 12} - 4 sample averaging and 12 bit ADC resolution.
  • { 8, 12} - 8 sample averaging and 12 bit ADC resolution.
  • { 16, 12} - 16 sample averaging and 12 bit ADC resolution.
  • { 32, 12} - 32 sample averaging and 12 bit ADC resolition.
  • { 64, 12} - 64 sample averaging and 12 bit ADC resolution.
  • {128, 12} - 128 sample averaging and 12 bit ADB resolution.
Link to this function

bus_voltage(conn)

@spec bus_voltage(t()) :: {:ok, volts()} | {:error, reason :: any()}

Returns the bus voltage in V.

Link to this function

bus_voltage_range(conn)

@spec bus_voltage_range(t()) :: {:ok, 16 | 32} | {:error, reason :: any()}

Retrieve the configured bus voltage range.

Link to this function

bus_voltage_range(conn, int)

@spec bus_voltage_range(t(), 16 | 32) :: {:ok, t()} | {:error, reason :: any()}

Set the configured bus voltage range to either 16 or 32V.

Link to this function

calibrate(conn)

@spec calibrate(t()) :: {:ok, non_neg_integer()} | {:error, reason :: any()}

Get the calibration value.

Link to this function

calibrate(conn, data)

@spec calibrate(t(), non_neg_integer()) :: {:ok, t()} | {:error, reason :: any()}

Set the calibration value.

Link to this function

calibrate_16V_400mA(conn)

@spec calibrate_16V_400mA(t()) :: {:ok, t()} | {:error, reason :: any()}

This is a convenient helper function to set the configuration to the right values for 16V input and 400mA current range at the highest resolution (0.1mA). This only works for devices with a 0.1Ω shunt resistor (ie Adafruit's breakout).

Make sure that you configure your device with a current_divisor of 20 and a power_divisor of 1.

Link to this function

calibrate_32V_1A(conn)

@spec calibrate_32V_1A(t()) :: {:ok, t()} | {:error, reason :: any()}

This is a convenient helper function to set the configuration to the right values for 32V input voltage range and 1A current range at the cost of accuracy. This only works for devices with a 0.1Ω shunt resistor (ie Adafruit's breakout).

Make sure that you configure your device with a current_divisor of 25 and a power_divisor of 1.

Link to this function

calibrate_32V_2A(conn)

@spec calibrate_32V_2A(t()) :: {:ok, t()} | {:error, reason :: any()}

This is a convenient helper function to set the configuration to the right values for 32V input voltage range and 2A current range at the cost of accuracy. This only works for devices with a 0.1Ω shunt resistor (ie Adafruit's breakout).

Make sure that you configure your device with a current_divisor of 10 and a power_divisor of 2.

Link to this function

conversion_ready?(conn)

@spec conversion_ready?(t()) :: boolean()

Are new samples ready since the last time you read them? Calling this function will clear the value until next time new samples are ready.

@spec current(t()) :: {:ok, milliamps()} | {:error, reason :: any()}

Returns the current in mA.

Link to this function

math_overflow?(conn)

@spec math_overflow?(t()) :: boolean()

Returns true when power or current calculations are out of range. This indicates that current and power data may be meaningless.

@spec mode(t()) :: {:ok, operating_mode()} | {:error, reason :: any()}

Retrieve the current operating mode of the device.

Link to this function

mode(conn, atom)

@spec mode(t(), operating_mode()) :: {:ok, t()} | {:error, reason :: any()}

Set the current operating mode for the device.

Valid values are:

  • :power_down
  • :shunt_voltage_triggered
  • :bus_voltage_triggered
  • :adc_off
  • :shunt_voltage_continuous
  • :bus_voltage_continuous
  • :shunt_and_bus_voltage_continuous
@spec power(t()) :: {:ok, milliwatts()} | {:error, reason :: any()}

Returns the power in mW

@spec reset(t()) :: {:ok, t()} | {:error, reason :: any()}

Power-on-reset the device,

Link to this function

shunt_adc_resolution_and_averaging(conn)

@spec shunt_adc_resolution_and_averaging(t()) ::
  {:ok, {sample_averaging(), sample_resolution()}} | {:error, reason :: any()}

Retrieve the shunt ADC resolution.

Returns the number of samples and the resolution as a two element tuple. eg {1, 9} refers to 9-bit ADC resolution and 1-sample averaging.

Link to this function

shunt_adc_resolution_and_averaging(conn, arg)

@spec shunt_adc_resolution_and_averaging(
  t(),
  {number(), number()}
) :: {:ok, t()} | {:error, any()}

Set the shunt ADC resolution and averaging.

Expects a tuple of the form {samples, bits}. Be aware that changing this value affects the amount of time it takes to compute values. Refer to the datasheet for more information.

Valid values are:

  • { 1, 9} - 1 sample averaging and 9 bit ADC resolution.
  • { 1, 10} - 1 sample averaging and 10 bit ADC resolution.
  • { 1, 11} - 1 sample averaging and 11 bit ADC resolution.
  • { 1, 12} - 1 sample averaging and 12 bit ADC resolution.
  • { 2, 12} - 2 sample averaging and 12 bit ADC resolution.
  • { 4, 12} - 4 sample averaging and 12 bit ADC resolution.
  • { 8, 12} - 8 sample averaging and 12 bit ADC resolution.
  • { 16, 12} - 16 sample averaging and 12 bit ADC resolution.
  • { 32, 12} - 32 sample averaging and 12 bit ADC resolition.
  • { 64, 12} - 64 sample averaging and 12 bit ADC resolution.
  • {128, 12} - 128 sample averaging and 12 bit ADB resolution.
Link to this function

shunt_voltage(conn)

@spec shunt_voltage(t()) :: {:ok, millivolts()} | {:error, reason :: any()}

Returns the shunt voltage in mV.

Link to this function

shunt_voltage_pga(conn)

@spec shunt_voltage_pga(t()) :: {:ok, shunt_voltage_pga()} | {:error, reason :: any()}

Retrieve the configured shunt voltage PGA.

Returns the gain value.

Valid values are:

  • 1 - gain 1, range +40mV.
  • 2 - gain +2, range +80mV.
  • 4 - gain +4, range +160mV.
  • 8 - gain +8, range +320mV.
Link to this function

shunt_voltage_pga(conn, int)

@spec shunt_voltage_pga(t(), shunt_voltage_pga()) ::
  {:ok, t()} | {:error, reason :: any()}

Set the configured shunt voltage PGA to a configured value.

Valid values are:

  • 1 - gain 1, range +40mV.
  • 2 - gain +2, range +80mV.
  • 4 - gain +4, range +160mV.
  • 8 - gain +8, range +320mV.