INA219 - Single Voltage and Current measurement

The INA219 is a single instance programable current and voltage monitor which is accessible via I2C and is fairly straight forward to use for monitoring battery circuits up to 26V. This is a 'high-side' measurement and is connected between the battery and the load, so is good for measuring individual loads, as opposed to 'low-side' units that are connected between battery and ground which are best for measuring total power consumed. As supplied, the board comes with a 0R1 resistor as a shunt which allows it to monitor a current up to 3.2A but connecting it across a standard shunt is perfectly feasible but the multipliers in the code have to be changed to match the new shunt value.

Doing this reduces the impact on the circuit, the typical voltage drop at 3.2A is 320mV but with a suitable 75mV current shunt this could be a lot less. The voltage measurement is unaffected by the addition of a shunt. Obviously it could be set up for 75mV but it's up to you as to what multiplier is used.

A 20A 75mV has a resistance of 0R00375, in parallel with 0R1 is 0R00361 

Code

There's not a lot to the code in this case, the setup routine starts communications with the INA219 and initialises the internal registers. The publication routine reads the voltages from the INA219 and calculates the current and voltage, publishing them to the MQTT server. These are called from the main routine as required by the timer. A display routine will be added to make this a basic meter type system.

/*

  INA219 sensor

  Thanks to Adafruit for supplying the library I used and other libraries are available

*/

 

#include <Adafruit_INA219.h>

Adafruit_INA219 ina219;

 

int setupINA219(int entry) {

  // Init INA219

  setupWire();

  ina219.begin();

 

  return 0;

}

 

int pubINA219(int entry) {

  float current_mA = ina219.getCurrent_mA();

  float shuntvoltage = ina219.getShuntVoltage_mV();

  float busvoltage = ina219.getBusVoltage_V();

  float voltage_V = busvoltage - shuntvoltage;

 

  sprintf(mqttData, "{\"current\": \"%.4f\", \"voltage\" : \"%.2f\"}",

          current_mA, voltage_V);

 

  client.publish(mqttContext[entry].mqttPubTopic, mqttData);

  return 0;

}