Look at ina219_test exemple sketch. This example works out of the box for Adafruit's INA219 Breakout.
Include defintions and define needed object:
#include <Wire.h>
#include <INA219.h>
INA219 monitor;
In the setup()
function initialise the INA219:
monitor.begin();
Then in the loop()
function make calls to different functions that are returning the values:
Serial.print("raw shunt voltage: ");
Serial.println(monitor.shuntVoltageRaw());
Serial.print("raw bus voltage: ");
Serial.println(monitor.busVoltageRaw());
Serial.println("--");
Serial.print("shunt voltage: ");
Serial.print(monitor.shuntVoltage() * 1000, 4);
Serial.println(" mV");
Serial.print("shunt current: ");
Serial.print(monitor.shuntCurrent() * 1000, 4);
Serial.println(" mA");
Serial.print("bus voltage: ");
Serial.print(monitor.busVoltage(), 4);
Serial.println(" V");
Serial.print("bus power: ");
Serial.print(monitor.busPower() * 1000, 4);
Serial.println(" mW");
If you want to use a different setup or if you do not use the Adafruit's breakout then in the setup()
function you need to call configure()
and calibrate()
.
An exemple is in ina219_test_nondefault.
Extract of the setup()
function:
monitor.begin();
// setting up our configuration
monitor.configure(INA219::RANGE_16V, INA219::GAIN_2_80MV, INA219::ADC_64SAMP, INA219::ADC_64SAMP, INA219::CONT_SH_BUS);
// calibrate with our values
monitor.calibrate(SHUNT_R, SHUNT_MAX_V, BUS_MAX_V, MAX_CURRENT);
All the functions are well comented in INA219.h and INA219.cpp
begin()
starts the communication with the INA219 and does the default setup.configure()
setups the INA219 mode.
The args are: (range, gain, bus_adc, shunt_adc, mode)
calibrate(r_shunt, v_shunt_max, v_bus_max, i_max_expected)
function is doing the calculations as described in the INA219 datasheet and sets up the calibration registers.
The args are:
reconfig()
rewrites the last used config to the INA219.recalibrate()
rewrites the calibration registers as they were calculated by calibrate()
function.ready()
returns the value of the ready bit. It's value is updated according to the last call to busVoltageRaw()
or busVoltage()
.
based on the datasheet page 30: Although the data from the last conversion can be read at any time, the INA219 Conversion Ready bit (CNVR) indicates when data from a conversion is available in the data output registers. The CNVR bit is set after all conversions, averaging, and multiplications are complete. CNVR will clear under the following conditions: 1.) Writing a new mode into the Operating Mode bits in the Configuration Register (except for Power-Down or Disable) 2.) Reading the Power Register
page 15: The Conversion Ready bit clears under these conditions:
overflow()
returns the value of the overflow bit. It's value is updated according to the last call to busVoltageRaw()
or busVoltage()
.reset()
: Resets the INA219.shuntVoltageRaw()
: Returns the raw binary value of the shunt voltagebusVoltageRaw()
: Returns raw bus voltage binary value.shuntCurrentRaw()
: Returns raw bus voltage binary value.shuntVoltage()
: Returns the shunt voltage in volts.busVoltage()
: Returns the bus voltage in volts.shuntCurrent()
: Returns the shunt current in amps.busPower()
: Returns the bus power in wattsMIT license