Connecting INA226 via i2C

Has anyone tried to connect a INA226 beackout-boad to a seeeduino 21?
I tried now for days with any libs I found. Some hang up, other receives only FFFF. Never got any values for voltage and current. Also tried to change frequency of wire, or change adress of the INA226, also tried different seeeduino and INA226-boards.

Best regards

Hi there,
is it this one?
INA226-board-11
where’s the code?
Use the code tags above , </> and paste it in nice like.

What is the end goal for using this?

" The INA226 is a shunt and bus voltage monitor introduced by Texas Instruments . It offers an integrated, zero-drift, bi-directional interface that monitors shunt voltage, bus voltage, current, and power. The INA226 communicates with microcontrollers through an I2C interface , allowing for easy data transfer and interpretation."

You want to monitor a system?
HTH
GL :slight_smile: PJ

The goal is to measure the capacity of a LiFePo 12V 100Ah Battery by accumulating charging and discharging current over time.

As code I tried all examples i could find in the libs.
This is one of it:

/*

INA226 Bi-directional Current/Power Monitor. Simple Example.

Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/cyfrowy-czujnik-pradu-mocy-ina226.html

GIT: https://github.com/jarzebski/Arduino-INA226

Web: http://www.jarzebski.pl

(c) 2014 by Korneliusz Jarzebski

*/

#include <Wire.h>

#include <INA226.h>

INA226 ina;

void checkConfig()

{

Serial.print("Mode: ");

switch (ina.getMode())

{

case INA226_MODE_POWER_DOWN:      Serial.println("Power-Down"); break;

case INA226_MODE_SHUNT_TRIG:      Serial.println("Shunt Voltage, Triggered"); break;

case INA226_MODE_BUS_TRIG:        Serial.println("Bus Voltage, Triggered"); break;

case INA226_MODE_SHUNT_BUS_TRIG:  Serial.println("Shunt and Bus, Triggered"); break;

case INA226_MODE_ADC_OFF:         Serial.println("ADC Off"); break;

case INA226_MODE_SHUNT_CONT:      Serial.println("Shunt Voltage, Continuous"); break;

case INA226_MODE_BUS_CONT:        Serial.println("Bus Voltage, Continuous"); break;

case INA226_MODE_SHUNT_BUS_CONT:  Serial.println("Shunt and Bus, Continuous"); break;

default: Serial.println("unknown");

}

Serial.print("Samples average: ");

switch (ina.getAverages())

{

case INA226_AVERAGES_1:           Serial.println("1 sample"); break;

case INA226_AVERAGES_4:           Serial.println("4 samples"); break;

case INA226_AVERAGES_16:          Serial.println("16 samples"); break;

case INA226_AVERAGES_64:          Serial.println("64 samples"); break;

case INA226_AVERAGES_128:         Serial.println("128 samples"); break;

case INA226_AVERAGES_256:         Serial.println("256 samples"); break;

case INA226_AVERAGES_512:         Serial.println("512 samples"); break;

case INA226_AVERAGES_1024:        Serial.println("1024 samples"); break;

default: Serial.println("unknown");

}

Serial.print("Bus conversion time: ");

switch (ina.getBusConversionTime())

{

case INA226_BUS_CONV_TIME_140US:  Serial.println("140uS"); break;

case INA226_BUS_CONV_TIME_204US:  Serial.println("204uS"); break;

case INA226_BUS_CONV_TIME_332US:  Serial.println("332uS"); break;

case INA226_BUS_CONV_TIME_588US:  Serial.println("558uS"); break;

case INA226_BUS_CONV_TIME_1100US: Serial.println("1.100ms"); break;

case INA226_BUS_CONV_TIME_2116US: Serial.println("2.116ms"); break;

case INA226_BUS_CONV_TIME_4156US: Serial.println("4.156ms"); break;

case INA226_BUS_CONV_TIME_8244US: Serial.println("8.244ms"); break;

default: Serial.println("unknown");

}

Serial.print("Shunt conversion time: ");

switch (ina.getShuntConversionTime())

{

case INA226_SHUNT_CONV_TIME_140US:  Serial.println("140uS"); break;

case INA226_SHUNT_CONV_TIME_204US:  Serial.println("204uS"); break;

case INA226_SHUNT_CONV_TIME_332US:  Serial.println("332uS"); break;

case INA226_SHUNT_CONV_TIME_588US:  Serial.println("558uS"); break;

case INA226_SHUNT_CONV_TIME_1100US: Serial.println("1.100ms"); break;

case INA226_SHUNT_CONV_TIME_2116US: Serial.println("2.116ms"); break;

case INA226_SHUNT_CONV_TIME_4156US: Serial.println("4.156ms"); break;

case INA226_SHUNT_CONV_TIME_8244US: Serial.println("8.244ms"); break;

default: Serial.println("unknown");

}

Serial.print("Max possible current: ");

Serial.print(ina.getMaxPossibleCurrent());

Serial.println(" A");

Serial.print("Max current: ");

Serial.print(ina.getMaxCurrent());

Serial.println(" A");

Serial.print("Max shunt voltage: ");

Serial.print(ina.getMaxShuntVoltage());

Serial.println(" V");

Serial.print("Max power: ");

Serial.print(ina.getMaxPower());

Serial.println(" W");

}

void setup()

{

Serial.begin(115200);

Serial.println(“Initialize INA226”);

Serial.println("-----------------------------------------------");

// Default INA226 address is 0x40

ina.begin();

// Configure INA226

ina.configure(INA226_AVERAGES_1, INA226_BUS_CONV_TIME_1100US, INA226_SHUNT_CONV_TIME_1100US, INA226_MODE_SHUNT_BUS_CONT);

// Calibrate INA226. Rshunt = 0.01 ohm, Max excepted current = 4A

ina.calibrate(0.01, 4);

// Display configuration

checkConfig();

Serial.println("-----------------------------------------------");

}

void loop()

{

Serial.print("Bus voltage: ");

Serial.print(ina.readBusVoltage(), 5);

Serial.println(" V");

Serial.print("Bus power: ");

Serial.print(ina.readBusPower(), 5);

Serial.println(" W");

Serial.print("Shunt voltage: ");

Serial.print(ina.readShuntVoltage(), 5);

Serial.println(" V");

Serial.print("Shunt current: ");

Serial.print(ina.readShuntCurrent(), 5);

Serial.println(" A");

Serial.println("");

delay(1000);

}

Next step will be to ignore any libs and to try to wirte to communication with the device completely new…

Hi there,
I’ll take that as a Yes. (and you can still go fix it, the cut & paste to </> just click it and paste your code there super easy. :face_with_peeking_eye:)
this is the proper use of the code tags, btw :v:

//
// INA226 Current, Voltage and power shunt measurement device. i2c address 0x40
//
#include <Wire.h>
#include <INA226_WE.h>   
#define I2C_ADDRESS 0x40
 
INA226_WE ina226 = INA226_WE(I2C_ADDRESS);
 
void setup() 
{
  Serial.begin(9600);
  while (!Serial); // wait until serial comes up on Arduino Leonardo or MKR WiFi 1010
  Wire.begin();
  ina226.init();
 
  /* Set Number of measurements for shunt and bus voltage which shall be averaged
    Mode *     * Number of samples
    AVERAGE_1            1 (default)
    AVERAGE_4            4
    AVERAGE_16          16
    AVERAGE_64          64
    AVERAGE_128        128
    AVERAGE_256        256
    AVERAGE_512        512
    AVERAGE_1024      1024*/
 
  //ina226.setAverage(AVERAGE_16); // choose mode and uncomment for change of default
 
  /* Set conversion time in microseconds
     One set of shunt and bus voltage conversion will take:
     number of samples to be averaged x conversion time x 2
 
       Mode *         * conversion time
     CONV_TIME_140          140 µs
     CONV_TIME_204          204 µs
     CONV_TIME_332          332 µs
     CONV_TIME_588          588 µs
     CONV_TIME_1100         1.1 ms (default)
     CONV_TIME_2116       2.116 ms
     CONV_TIME_4156       4.156 ms
     CONV_TIME_8244       8.244 ms  */
 
  //ina226.setConversionTime(CONV_TIME_1100); //choose conversion time and uncomment for change of default
 
  /* Set measure mode
    POWER_DOWN - INA226 switched off
    TRIGGERED  - measurement on demand
    CONTINUOUS  - continuous measurements (default)*/
 
  //ina226.setMeasureMode(CONTINUOUS); // choose mode and uncomment for change of default
 
  /* Set Resistor and Current Range
     if resistor is 5.0 mOhm, current range is up to 10.0 A
     default is 100 mOhm and about 1.3 A*/
 
  ina226.setResistorRange(0.1, 1.3); // choose resistor 0.1 Ohm and gain range up to 1.3A
 
  /* If the current values delivered by the INA226 differ by a constant factor
     from values obtained with calibrated equipment you can define a correction factor.
     Correction factor = current delivered from calibrated equipment / current delivered by INA226*/
 
  ina226.setCorrectionFactor(0.93);
 
  Serial.println("INA226 Current Sensor Example Sketch - Continuous");
 
  ina226.waitUntilConversionCompleted(); //if you comment this line the first data might be zero
}
 
void loop()
{
  float shuntVoltage_mV = 0.0;
  float loadVoltage_V = 0.0;
  float busVoltage_V = 0.0;
  float current_mA = 0.0;
  float power_mW = 0.0;
 
  ina226.readAndClearFlags();
  shuntVoltage_mV = ina226.getShuntVoltage_mV();
  busVoltage_V = ina226.getBusVoltage_V();
  current_mA = ina226.getCurrent_mA();
  power_mW = ina226.getBusPower();
  loadVoltage_V  = busVoltage_V + (shuntVoltage_mV / 1000);
 
  Serial.print("Shunt Voltage [mV]: "); Serial.println(shuntVoltage_mV);
  Serial.print("Bus Voltage [V]: "); Serial.println(busVoltage_V);
  Serial.print("Load Voltage [V]: "); Serial.println(loadVoltage_V);
  Serial.print("Current[mA]: "); Serial.println(current_mA);
  Serial.print("Bus Power [mW]: "); Serial.println(power_mW);
  if (!ina226.overflow)
  {
    Serial.println("Values OK - no overflow");
  }
  else
  {
    Serial.println("Overflow! Choose higher current range");
  }
  Serial.println();
 
  delay(3000);
}

Compiler output…

FQBN: Seeeduino:samd:seeed_XIAO_m0
Using board 'seeed_XIAO_m0' from platform in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\Seeeduino\hardware\samd\1.8.5
Using core 'arduino' from platform in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\Seeeduino\hardware\samd\1.8.5

Detecting libraries used...
Using library Wire at version 1.0 in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\Seeeduino\hardware\samd\1.8.5\libraries\Wire 
Using library INA226_WE at version 1.2.9 in folder: D:\Arduino_projects\libraries\INA226_WE 
"C:\\Users\\Dude\\AppData\\Local\\Arduino15\\packages\\Seeeduino\\tools\\arm-none-eabi-gcc\\7-2017q4/bin/arm-none-eabi-size" -A "C:\\Users\\Dude\\AppData\\Local\\Temp\\arduino\\sketches\\99FEF11013F6F9D450FF73C6FADF8F2F/sketch_may8a.ino.elf"
Sketch uses 41548 bytes (15%) of program storage space. Maximum is 262144 bytes.

This code compiles for Seeeduino XIAO (samD21), no errors, provided you use the correct LIB. found Here…
read the notes this was configured for a UNO originally?
Note, the default current is only 1.3A so be sure to set your range somewhere in the middle, I have seen it used to monitor a E-Golf Kart Battery of 36Volts a while back if I recall it worked very well.! Do use the Multi-meter in line (amps) mode to calibrate after you have a handle on things to get the most accurate reads, and yes the first one can be ZERO. it’s in the data-sheet.
I’ll find the link to the white paper we used as a guide time permitting.

HTH
GL :slight_smile: PJ

“read the notes this was configured for a UNO originally?” - maybe this is the reason that also this code doesnt work.
It hangs on the line: ina226.waitUntilConversionCompleted();
When I comment it out, I receive only 0.00-Values

It seems, that the i2c is not working with this lib on the seeeduino.
As I wrote, with other code showing the raw input from i2c it receives only 0xff, maybe something with leading or trailing edge on the i2c or lsb/msb.

I think I got it!

Testing with a i2c-scanner-program I got “No devies found” with both INA226-Boards (both from A…)
Then I found another old 16-bit adc-board - and this responses normally.

So I try to find another vendor for the INA-boards …

1 Like

Hi there,
Yea, READ the Notes, Is why I put that there.
I don’t understand why you can’t post it correctly, and You don’t have the delay’s between acquisitions, that code was example code not YOUR runtime, so :index_pointing_at_the_viewer:
need to read the data sheet and add the delay’s to get it to respond.
You seem as though your comprehension is not good?
what address have you set? or are you just trying examples from the internet?
GL :slight_smile: PJ