RobTillaart / INA226

Arduino library for INA226 power sensor
MIT License
54 stars 14 forks source link

Overflow current >= 32768 (int16) #20

Closed felipengeletrica closed 2 years ago

felipengeletrica commented 2 years ago

When trying to do with currents above 33768 mA. This is probably caused by using 16-bit variables and the maximum current limit.

Thank you in advance for your time and for this library.

RobTillaart commented 2 years ago

Thanks for this issue, Think your analysis is on a right track.

RobTillaart commented 2 years ago

@felipengeletrica

Did a small test and it seems to happen when you 'normalize' the current.

The maxCurrent is calculated from the 'normailized' LSB which ends up being 32768 * LSB = 32768 mA In the code a normalized LSB is always 1 (with a power of 10)

Run this sketch shows the difference:

//
//    FILE: INA226_test.ino
//  AUTHOR: Rob Tillaart
// PURPOSE: test sketch
//     URL: https://github.com/RobTillaart/INA226

#include "INA226.h"
#include "Wire.h"

INA226 INA(0x40);

void setup()
{
  Serial.begin(115200);
  Serial.println(__FILE__);

  Wire.begin();
  if (!INA.begin() )
  {
    Serial.println("could not connect. Fix and Reboot");
  }
  INA.setMaxCurrentShunt(79, 0.001, true);
  Serial.println(INA.getMaxCurrent(), 3);
  INA.setMaxCurrentShunt(79, 0.001, false);
  Serial.println(INA.getMaxCurrent(), 3);
}

void loop()
{
}

Output: 32.768 79.000

Please check if this solves the issue.

RobTillaart commented 2 years ago

Added the above sketch to next release to show the difference between normalize and not.

RobTillaart commented 2 years ago

@felipengeletrica Closed the issue as normalization explained the behavior - see updated documentation.

felipengeletrica commented 2 years ago

Working now! Very thanks! 🚀

RobTillaart commented 2 years ago

You're welcome!