RobTillaart / INA226

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

Higher current shunt issue #16

Closed Juicy-AUS closed 2 years ago

Juicy-AUS commented 2 years ago

I’m using an external 40A 75mV shunt with the ina226. When I input the values to the ina.setMaxCurrentShunt(40.0, 0.001875) I’m unable to measure any current. Interestingly, if I use 20a I am able to read small currents (200-400mA) fairly accurately. I’ve yet to setup a test to pull larger currents. Is having such a large max current reducing the A/mV too much that it’s unable to register any current or something else?

RobTillaart commented 2 years ago

Thanks for reporting,

As I am quite busy with some other libraries it might take some days before I can investigate. Think I need to reread the datasheet carefully for this one.

Juicy-AUS commented 2 years ago

Thank you. Im using an esp32. I will try to put a drawn schema together. I’m using the same breakout board your were using for testing in issue #2 but when I first didn’t get any readings, I decided to remove the surface mounted R002 resistor from the board, thinking since both resistances were in parallel, it may cause issues. Vin connected to one side of shunt. Vout connected to other side. Using a switch mode power supply positive connected with Vin at shunt and an led strip connected to with vout at shunt which then goes to ground of power supply. Voltage readings are fairly decent (seems to be out +/- 350mV but that could also be my dmm as I need new leads) and as mentioned earlier, fairly accurate current readings at <1A with 20A setting but feel that might start to swing away with higher currents.

RobTillaart commented 2 years ago

ina.setMaxCurrentShunt(40.0, 0.001875)

I had a quick look in the code and it is limited to 20Amps. The call should return false because params are out of range. You could test that.

Note for myself: check documentation.

(See .cpp file)

Juicy-AUS commented 2 years ago

I was worried about that. Call seemed to return just 0.00A. Is the limit changeable? When you get a chance to have a look that is. Apologies.

RobTillaart commented 2 years ago

You are free to change the code as it is open source, I do not know without further investigation if and how well that will work. Remember that the sensor has a physical limitation. Advice read the datasheet, twice

Keep me informed of your insights, might help to improve the library and documentation.

Juicy-AUS commented 2 years ago

After posting I realised I can just go to the .cpp and change it so I will try that and report back. I looked through the data sheet and didn’t see anything about a current limit. I suppose it’s not really measuring current, just the voltage drop across the pins. There is a max voltage between vin and vout of 81mV so if shunt resistance * max current was above that, I suspect chip might let the smoke out. But mine should only be 75mV at 40A. I will test it out either way and let you know.

RobTillaart commented 2 years ago

you may uncomment line 194 #define printdebug true to get some numbers over Serial.

Juicy-AUS commented 2 years ago

I changed line #198 to max current > 40 and all has worked fine. I’m yet to confirm accuracy of current but will get to that.

RobTillaart commented 2 years ago

mmm, the limit of 20 A was because the sensor can handle 20 A. Code calculates the accuracy - LSB - from this value.

There might be no reason to prevent the math if current is "too high", just return a warning if the value is above 20 A. This way the user can see if and where setting the internals failed.

Must dive into the datasheet and code to recall the details.

Just to hold my "current" thoughts, I'm thinking of this.

int INA226::setMaxCurrentShunt(float maxCurrent, float shunt, bool normalize)
{
  // #define printdebug true
  uint32_t calib = 0;
  uint32_t factor = 1;

  if (maxCurrent < 0.001) return INA_ERR_MAXCURRENT_LOW;
  if (shunt < 0.001) return INA_ERR_SHUNT_LOW;

  // do al the math

  if (maxCurrent > 20) return INA_ERR_MAXCURRENT_HIGH;
  return INA_ERR_NONE;
}
Juicy-AUS commented 2 years ago

From what I can see in the data sheet, there is no limit to what current the ina226 chip can read, since its really just reading voltage drop. But I think the breakout board, due to size of tracks, shunt resistor type etc is limited though. Since I’m not passing any current through the board and using a seperate shunt resistor, as long as the voltage drop between in+ and in- doesn’t go above 81mV, it should be ok. That’s my understanding of the datasheet.

RobTillaart commented 2 years ago

@Juicy-AUS Sorry it took so long to respond,

But I think the breakout board, due to size of tracks, shunt resistor type etc is limited though.

That is definitely true

I'm going to create a develop branch without the limit check, will set a warning or so, and update the documentation to reflect these new insights.

RobTillaart commented 2 years ago

@Juicy-AUS Created a develop branch for 0.4.0 which includes a 80 mV shuntVoltage check instead of a maxCurrent check

If you have time could you check if it works for you?

RobTillaart commented 2 years ago

@Juicy-AUS This new version should work for you, otherwise reopen the issue. If there are other problems, please open a new issue.

Juicy-AUS commented 2 years ago

Thank you @RobTillaart. I’ve been a little busy but will put in this version and test when I can. I appreciate your work sir.