Zanduino / INA

Combined Arduino library for reading multiple INA2xx power monitor devices
GNU General Public License v3.0
158 stars 41 forks source link

Possible error with INA226 #66

Closed SV-Zanshin closed 4 years ago

SV-Zanshin commented 4 years ago

Taras sent in the following:

Hi there. I used your library for INA226. Found an error. In the example DisplayReadings.ino power is not calculated correctly. I add a screen. V = 8,843 V A = 35.736 mA V A = 8.843 35.736 = 316mW , and in the example it turns out 253 mW. Could this be a library error? Maybe it's my fault?

-- INA226 ERROR

The Shunt is 0.1Ohm and the original DisplayReadings.ino settings were used.

SV-Zanshin commented 4 years ago

I am looking at the library and can't locate an error. The "getBusMicroWatts()" function will read the next computed value, so if the current changes very quickly then you might get a value that doesn't correspond to the BusmA * BusVoltage. Could this be the case for you? Is the problem reproduceable?

Samodelkin-YouTube commented 4 years ago

Today I tried to use the library "INA226" ,everything works well there. A 0.1348 V 19.120 W_ina 2.5788 W=V*A 2.5774 % 100.05

When I use the "INA" library, the power is incorrect, the error is 80%

A 0.1348 V 19.118 W_ina 2.0618 W=V*A 2.5771 % 80.01

SV-Zanshin commented 4 years ago

Since the Amps and Volts are the same in both cases, the problem lies in the computation of the Watts. Are you initializing both libraries with the same begin() statement?

Samodelkin-YouTube commented 4 years ago

"INA226.begin " and "INA.begin"

SV-Zanshin commented 4 years ago

I meant the parameters to the begin() - are they identical, especially the values for expected maximum amps

Samodelkin-YouTube commented 4 years ago

all the same

SV-Zanshin commented 4 years ago

How comfortable are you to make some small changes to your version of the library? I would like you to change the library by adding the following after line 634 "(int64_t)readWord(INA_POWER_REGISTER, ina.address) * (int64_t)ina.power_LSB / (int64_t)1000;"

The lines are:

Serial.print("power register is "); Serial.println(readWord(INA_POWER_REGISTER, ina.address)); Serial.print("power_LSB is "); Serial.println(ina.power_LSB);

Gallium70 commented 4 years ago

According to the INA226 datasheet, page 16, for INA 226, the power LSB should be 25 * Current_LSB: image

However, it's set to 20x in INA.cpp, line 27: image

And 20/25 is exactly 80%.

SV-Zanshin commented 4 years ago

Good sleuthing - I am re-installing my PC this weekend (Thank You, Mr. Gates) but once all the software is installed I'll check this out.

Samodelkin-YouTube commented 4 years ago

Согласно таблице данных INA226 , стр. 16, для INA 226 младший разряд мощности должен быть равен 25 * Current_LSB: . Why is this necessary? P.S. Installed the library INA226 ,everything works fine.

Gallium70 commented 4 years ago

Согласно таблице данных INA226 , стр. 16, для INA 226 младший разряд мощности должен быть равен 25 * Current_LSB: . Why is this necessary? P.S. Installed the library INA226 ,everything works fine.

As the datasheet explains,

This ratio is internally programmed to ensure that the scaling of the power calculation is within an acceptable range.

Below is my theory about this. The current and bus voltage are both stored in 16-bit registers. If you directy multiply them together, you'll need a 32-bit register to store the result. To put it into a 16-bit register, scaling is necessary(of course there's a loss in precision), and the scale factor should be somewhere near 2^(32-16)=65536, intuitively. TI chooses a scale factor of 20000, since the LSB for the bus voltage is fixed at 1.25mV/bit (equivalent to 800x scaling), then the power LSB should be 20000/800=25x current LSB. As for why it's exactly 20000 (not some other number), maybe only TI knows.

SV-Zanshin commented 4 years ago

I see the differences in the DataSheets, but before I commit the changes, I will need to hook up a INA219 and INA226 and test the changes. Still downloading MS-Studio, VisualMicro and need to install doxygen, clang-format, gitKraken, etc.

-Arnd.

From: Gallium70 notifications@github.com Sent: Sunday, October 25, 2020 10:24 To: SV-Zanshin/INA INA@noreply.github.com Cc: Arnd zanshin_github@sv-zanshin.com; Assign assign@noreply.github.com Subject: Re: [SV-Zanshin/INA] Possible error with INA226 (#66)

Согласно таблице данных INA226 https://www.ti.com.cn/lit/ds/symlink/ina226.pdf , стр. 16, для INA 226 младший разряд мощности должен быть равен 25 * Current_LSB: . Why is this necessary? P.S. Installed the library INA226 ,everything works fine.

As the datasheet explains,

This ratio is internally programmed to ensure that the scaling of the power calculation is within an acceptable range.

Below is my theory about this. The current and bus voltage are both stored in 16-bit registers. If you directy multiply them together, you'll need a 32-bit register to store the result. To put it into a 16-bit register, scaling is necessary(of course there's a loss in precision), and the scale factor should be somewhere near 2^(32-16)=65536, intuitively. TI chooses a scale factor of 20000, since the LSB for the bus voltage is fixed at 1.25mV/bit (equivalent to 800x scaling), then the power LSB should be 20000/800=25x current LSB. As for why it's exactly 20000 (not some other number), maybe only TI knows.

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/SV-Zanshin/INA/issues/66#issuecomment-716117682 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AFT5DRF57QSRGSEZGL5WSEDSMPVD5ANCNFSM4QPQVW6A . https://github.com/notifications/beacon/AFT5DRA2QCT677KO6CXXY5DSMPVD5A5CNFSM4QPQVW6KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOFKXRNMQ.gif

SV-Zanshin commented 4 years ago

First test looks good :)

SV-Zanshin commented 4 years ago

Fixed in Development version 1.0.13, which should be promoted to a new master version in the next couple of weeks.