Query about INA237 Library

Hi,

I have to get the parameters value from ina237 using Arduino. But library is not available in Arduino IDE.

So could you please send me the library as soon as possible?

Regards,
Nitheesh

I'm afraid that you have to write something yourself if there is no usable library in the IDE and there is no usable library on the web (e.g. github).

Google says there is such a library

1 Like

Hi ua6em,

I already found this library. But here it is not compatible for arduino.

So could you please send me some other library?

Regards,
Nitheesh

I would say it's compatible; you just have to fix two mistakes in it.

One way to fix it is by modifying the .cpp file.

  1. Replace size_t read = _i2c->requestFrom(_device_address, cnt, true); by size_t read = _i2c->requestFrom(_device_address, (uint8_t)cnt, (uint8_t) true);.
  2. Replace while (cnt > _i2c->available()) by while (cnt > (uint8_t)_i2c->available()).

Note that those changes will be undone if you ever update the library.

Test with a Nano.

#include "INA237.h"

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Please provide feedback why those changes were made.

PS

  1. The .h and .cpp file were placed in the sketch directory, hence the double quotes for the include.
  2. No idea if it works; but it does compile without errors or warnings.

Hi sterretje,

I have tried the application program by using this INA237 library. But we are getting Bus voltage and shunt voltage as 0. I have uploaded used code below. Can you please review the code and give suggestions to move forward.

I have connected my A0 and A1 pin to Vs and Ground. As per the configuration, I given the slave address as 0x41.

#include "INA237.h"

INA237 sensor(&Wire, 0x41); // Default: Wire and address 0x40

void setup() {
Serial.begin(9600);
// sensor.begin();
sensor.reset();
sensor.calibrate(0.002, 10.0); // 0.01 Ohm shunt, 10A max current
}

void loop() {
float voltage = sensor.getBusVoltage();
float Shunt = sensor.getShuntVoltage();
Serial.print("Bus Voltage: ");
Serial.print(voltage, 3);
Serial.println(" V");
Serial.print("Shunt Voltage");
Serial.print(Shunt);
Serial.println(" V");
delay(1000);
}

Or If you have any application code using INA237 library, please share the code.

Thanks in advance.

Regards,
Nitheesh

I can only help you to get code to compile.

Did you run the I2C scanner example to determine the address of the INA237?

I don't have the sensor so can't further advise.

PS
Next time use code tags when posting code. See https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#posting-code-and-common-code-problems

Please show a schematic of your complete circuit.
What voltage is applied to IN+ and IN- ?
Have you run the I2C scanner program?

Hi,

Thanks for your suggestion. Next time I will use code tags :innocent:

I have checked with i2c scanner. Actually ,I am using two i2c devices. I got I2C address of INA237 as 0x41 in arduinomega2560.

Our Vbus voltage is 36V [Max].

So How can I proceed further?

Regards,
Nitheesh

Two INA237?
Do you have the correct pull-up resistors on the Mega?
Are all grounds connected?
What is the shunt resistance?
Are you measuring high side or low side?
What is the current?
Have you verified the voltage on +IN and -IN with a voltmeter.
Are all grounds connected?

Try to get the parameters set in INA237:

    uint16_t getAlertFlag(void) const;
    double getBusVoltage(void) const;
    double getShuntVoltage(void) const;
    double getCurrent(void) const;
    double getPower(void) const;
    double getTemp(void) const;
    double getShuntOvervoltageTreshold(void) const;
    double getShuntUndervoltageTreshold(void) const;
    double getBusOvervoltageTreshold(void) const;
    double getBusUndervoltageTreshold(void) const;
    double getTempOverlimitTreshold(void) const;
    double getPowerOverlimitTreshold(void) const;
    double getOverCurrentTreshold(void) const;
    double getUnderCurrentTreshold(void) const;
    uint16_t getManufacturerID(void) const;

Hi guys,

I am using rtc and ina237.

Yes.

Yes

5 mA.

low side.

10A

Yes.

I will try to get all the parameters and let you know.

Regards,
Nitheesh

Let's do a test and see if you can even talk to the INA237.
This code will read the Manufacturer's ID.

// FOR TEST

/*
   Error codes for Wire.endTransmission
    0: success
    1: data too long to fit in transmit buffer (32 byte max)
    2: received NACK on transmit of address
    3: received NACK on transmit of data
    4: other error (???)
*/

// Read the MANUFACTURER_ID = 0x5449


#include <Wire.h>

const byte reg_pointer = 0x3E;
const byte addr = 0x41;
byte dataH, dataL;
byte I2CError;
unsigned long timeout = 1000;
unsigned long time_n;

void setup()
{
  Wire.begin();
  Wire.setClock(100000);  // Make sure SCL is 100kHz
  Serial.begin(9600);
  I2CError = 0;
}

void loop()
{
  bool flag = false;
  Wire.beginTransmission(addr);
  Wire.write(reg_pointer);

  I2CError = Wire.endTransmission(true);
  if (I2CError == 0)
    Serial.println ("OK ");
  else
  {
    Serial.print ("Error      ");
    Serial.println (I2CError);
  }

  Wire.requestFrom(addr, 2, true);
  
  time_n = millis();
  while ((Wire.available() < 2) & (flag == false))
  {
    delay(1);
    if ((millis() - time_n) > timeout) flag = true;
  }
  Serial.println();
  if (flag) Serial.println("** TIMOUT **");

  dataH = Wire.read();
  dataL = Wire.read();
  Serial.print(reg_pointer, HEX); Serial.print("\t");
  Serial.print(dataH, HEX); Serial.print("\t");
  Serial.print(dataL, HEX);
  Serial.println();

  delay (2000); // Wait 2
}

Hi,

I have checked with your code. I have received these below response.

** TIMOUT **
3E FF FF
Error 4.

For your kind information, Earlier we have used INA226 with the same circuit connection. It received the response properly. But, Why it is giving trouble here?

Regards,
Nitheesh

Hi ua6em,

Sorry for the late response.

I have called all the api's from ina237 library. It is giving return value as 0.

Regards,
Nitheesh

It must be a hardware issue. Bad connections, no or wrong pull-ups or the IC is bad.

Hi Jim-p and Victor,

Thank you for your guidance.

We will check the hardware issue on our side.

Regards,
Nitheesh

So did you find the problem?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.