Because of the Thanksgiving holiday in the U.S., TI E2E™ design support forum responses may be delayed from November 25 through December 2. Thank you for your patience.

This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Problems with Arduino of the INA226 module

Other Parts Discussed in Thread: INA226

Good morning,
I have a big problem with using the INA226 module.
I am attaching the connection diagram and Arduino code to see if you can help me.
My purpose is:
- read the voltage (up to 12V) on the load with good precision (also OK with 0.01V) (ie not the voltage on the Rshunt but that of the user; that is, in my case, the LED Voltage + Resistance Voltage which would then be those useful for the projects)


- read the current (up to 8A) passing through the load (with a precision of 0.0005A (ie 0.5mA)).


I have already read that to be precise what matters is the Shunt resistance and I have already ordered the one suitable for my case (ie a 10 mOhm), but my problem is precisely the programming code that does not return to me what Arduino does I look for.

From the Arduino Serial Monitor I read this:


I enclose:

- Arduino code

/*
    INA226 Bi-directional Current/Power Monitor. Simple Example.
    Read more: www.jarzebski.pl/.../cyfrowy-czujnik-pradu-mocy-ina226.html
    GIT: github.com/.../Arduino-INA226
    Web: http://www.jarzebski.pl
    (c) 2014 by Korneliusz Jarzebski
*/

#include <Wire.h>
#include <INA226.h>

INA226 ina;

void checkConfig()
{

  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.1 ohm, Max excepted current = 4A
  ina.calibrate(0.1, 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);
}

- Result on the Serial Monitor

Initialize INA226
-----------------------------------------------
Max possible current:  0.82 A
Max current:           0.82 A
Max shunt voltage:     0.08 V
Max power:             29.49 W
-----------------------------------------------
Bus voltage:   0.03500 V
Bus power:     0.00000 W
Shunt voltage: 0.00045 V
Shunt current: 0.00460 A

Bus voltage:   0.03625 V
Bus power:     0.00000 W
Shunt voltage: 0.00046 V
Shunt current: 0.00460 A

Bus voltage:   0.03375 V
Bus power:     0.00000 W
Shunt voltage: 0.00045 V
Shunt current: 0.00460 A

Bus voltage:   0.03625 V
Bus power:     0.00000 W
Shunt voltage: 0.00045 V
Shunt current: 0.00460 A

- image of the electrical connection diagram (Arduino USB power supply of the PC, while the LED is powered by an external StepDown about 5V)

Please help me.
Thanks

  • Hi Paolo,

    You didn’t make it clear what the expected readings are, in contrast to what the system returns.

    However, you passed 100mOhm shunt resistor to the calibration routine while you said you had 10mOhm in hardware. This will definitely mess it up.

    Regards, Guang

  • Hi. The code give me a Bus Voltage about plus or minor 0.04V as you can see, but the real Bus Voltage on the LOAD is  about plus or minor 5V!That is the problem. You can read the Serial Monitor that i report to the post before.

    About the resistor shunt i said that for the precision that i want i'll replace the R shunt that now is 100mOhm with the 10mOhm that was shipped from China and will arrive to me in 20 days! But the Arduino Code that i post is about the R Shunt that now i have on board (so 0.1 Ohm , 100mOhm).

  • Hi Paolo,

    If Vbus reading is wrong, have you checked with a meter what this voltage really is on the pin #3? I don’t see any connection to it in the schematic, it is shown floating.

    Regards, Guang

  • Are you speak about the pin VBS? What connection i've to do?

  • Hi Paolo,

    I thought it reads “VBus”, ha! What is “VBS” anyways?

    You want to measure on INA226 VBUS pin to make sure it shows 5V, that’s where the voltage is measured by the IC.

    Regards, Guang

  • You are right! I'have to connect the VBS before the + LOAD and now it works well and give me this results

    Bus voltage: 4.86 V    <---- OK it's right
    Bus power: 0.025 W    <---- OK it's right
    Shunt voltage: 0.00046 V
    Shunt current: 0.0046 A    <---- OK it's right

    Now i have also a question....i'd to use 4 module INA226. Each of the modules will have its address depending on the connections I will make between A0, A1, GND, VCC, etc. but my question now is another: how can I assign a name to each variable?

    To be clear, let's take the example by putting 2 INA226 modules, one on a 12V network and one on a 5V, for example the first will have address 0X40 and the other 0x41 but both will have in the code

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

    as well as
      Serial.print (ina.readBusPower (), 3);

    is
      Serial.print (ina.readShuntCurrent (), 4);

    ... How can I get the readings of what will read the 12V voltage and what will read 5v?

  • Hi Paolo,

    You’d have to call on the four INA226 one at a time. The related commands must now all reside in the forever loop.

    There are different ways of doing it, what I would do is - push the essential initialization/setup commands including some “.h” variables down to the forever loop. 

    It is easy to retrieve the readings as you know which one you’re calling, You can use the same local variables, and transfer to others.

    You can probably consolidate these commands into a sub program for better readability. This command can be enclosed inside a “for” loop such that each iteration, you only update the device address (and possibly others such as calibration value as needed) and transfer readings.

    This is just a suggestion; I believe you’re able to implement this or some other ways. Whichever you choose, give it a try and I guarantee it will be fun.

    Regards, Guang

  • ok ... but I don't know how to do it ... here below I put the connection scheme of the 4 INA226 modules each with different voltage 12V, 5V, 5V USB and 3.3V.
    The aim then is to show all the voltage and current readings of each module on the NEXTION Display (which I divided into 4 parts each for one of the 4 voltages and each letter box of the display must correspond to a variable).
    So for example the current read from the INA226 of the 5V should be called Current_5V, that of the INA226 of the 12V should be called Current_12V ... only if I am in the void loop only by code

      Serial.print ("Shunt current:");
      Serial.print (ina.readShuntCurrent (), 4); // value to 3 decimal places
      Serial.println ("A");

    then I don't know how to assign them to the different different variable modules ...
    I hope I was clear as clear as possible ... do you have any advice?

  • I tried to throw this scketch down but it doesn't work!

    /*
        INA226 Bi-directional Current/Power Monitor. Simple Example.
        Read more: www.jarzebski.pl/.../cyfrowy-czujnik-pradu-mocy-ina226.html
        GIT: github.com/.../Arduino-INA226
        Web: http://www.jarzebski.pl
        (c) 2014 by Korneliusz Jarzebski
    */
    
    #include <Wire.h>
    #include <INA226.h>
    
    INA226 ina_5V;
    INA226 ina_3.3V(0x45);
    
    void checkConfig()
    {
    
      Serial.print("Max possible current 5V:  ");
      Serial.print(ina_5V.getMaxPossibleCurrent());
      Serial.println(" A");
    
      Serial.print("Max current 5V:           ");
      Serial.print(ina_5V.getMaxCurrent());
      Serial.println(" A");
    
      Serial.print("Max shunt voltage 5V:     ");
      Serial.print(ina_5V.getMaxShuntVoltage());
      Serial.println(" V");
    
      Serial.print("Max power 5V:             ");
      Serial.print(ina_5V.getMaxPower());
      Serial.println(" W");
    
      Serial.print("Max possible current 3.3V:  ");
      Serial.print(ina_3.3V.getMaxPossibleCurrent());
      Serial.println(" A");
    
      Serial.print("Max current 3.3V:           ");
      Serial.print(ina_3.3V.getMaxCurrent());
      Serial.println(" A");
    
      Serial.print("Max shunt voltage 3.3V:     ");
      Serial.print(ina_3.3V.getMaxShuntVoltage());
      Serial.println(" V");
    
      Serial.print("Max power 3.3V:             ");
      Serial.print(ina_3.3V.getMaxPower());
      Serial.println(" W");
    
    }
    
    void setup() 
    {
      Serial.begin(115200);
    
      Serial.println("Initialize INA226");
      Serial.println("-----------------------------------------------");
    
      // Default INA226 address is 0x40
      ina_5V.begin();
      ina_3.3V.begin();
    
      // Configure INA226
      ina_5V.configure(INA226_AVERAGES_1, INA226_BUS_CONV_TIME_1100US, INA226_SHUNT_CONV_TIME_1100US, INA226_MODE_SHUNT_BUS_CONT);
      ina_3.3V.configure(INA226_AVERAGES_1, INA226_BUS_CONV_TIME_1100US, INA226_SHUNT_CONV_TIME_1100US, INA226_MODE_SHUNT_BUS_CONT);
    
    
      // Calibrate INA226. Rshunt = 0.1 ohm, Max excepted current = 4A
      ina_5V.calibrate(0.1, 4);
      ina_3.3V.calibrate(0.1, 4);
    
    
      // Display configuration
      checkConfig();
    
      Serial.println("-----------------------------------------------");
    }
    
    void loop()
    {
      Serial.print("Bus voltage 5V:   ");
      Serial.print(ina_5V.readBusVoltage(), 2); // valore a 2 cifre decimali
      Serial.println(" V");
    
      Serial.print("Bus power 5V:     ");
      Serial.print(ina_5V.readBusPower(), 3); // valore a 3 cifre decimali
      Serial.println(" W");
    
    
      Serial.print("Shunt voltage 5V: ");
      Serial.print(ina_5V.readShuntVoltage(), 5);
      Serial.println(" V");
    
      Serial.print("Shunt current 5V: ");
      Serial.print(ina_5V.readShuntCurrent(), 4); // valore a 3 cifre decimali
      Serial.println(" A");
    
    
      Serial.print("Bus voltage 3.3V:   ");
      Serial.print(ina_3.3V.readBusVoltage(), 2); // valore a 2 cifre decimali
      Serial.println(" V");
    
      Serial.print("Bus power 3.3V:     ");
      Serial.print(ina_3.3V.readBusPower(), 3); // valore a 3 cifre decimali
      Serial.println(" W");
    
    
      Serial.print("Shunt voltage 3.3V: ");
      Serial.print(ina_3.3V.readShuntVoltage(), 5);
      Serial.println(" V");
    
      Serial.print("Shunt current 3.3V: ");
      Serial.print(ina_3.3V.readShuntCurrent(), 4); // valore a 3 cifre decimali
      Serial.println(" A");
    
    
    
      Serial.println("");
      delay(1000);
    }

  • Hi Paolo,

    This has now become a pure programming issue and has little to do with INA226 itself. I can’t write the code for you but I can provide the below pseudo code to guide you through the process.

    If you haven’t already, please first read through all the original code, including the includes, so as to understand how the code works with one INA226. Then make the modifications to work with 4.

    Here is what you need to have inside the forever loop:

    AddressArray[4]={x40, x41, x42, x43}

    For i=1 to 4 loop

    {

    Ina.begin_1(AddressArray[i]) //modify as needed

    ina.configure(….)

    ina.calibrate()

    ina.readBusVoltage()

    ina.readShuntVoltage()

    ina.readShuntCurrent ()

    Print to serial port

    }

    It likely will take you some time to make it work, be patient.

    Meanwhile please click on "This Resolved my issue" button to benefit others who have the same issue.

    Regards, Guang