Closed xkachya closed 1 week ago
Please have a look at PR #8, , there is similar work in progress
updated row 306 to make it possible to configure Shunt to 0.0002 Om, because in my INA228 module, there is an integrated 0.0002 Om Shunt. This change will make it possible to use the library with such devices as mine.
Then 0.0001 Ohm should be used, I think to not be affected by float precission.
Please have a look at PR #8, , there is similar work in progress
I will
Think I got the getCurrent()
The SHUNT_CAL register must be written before the CURRENT register is read.
8.1.2 _Note that the current is calculated following a shunt voltage measurement based on the value set in the SHUNT_CAL register. If the value loaded into the SHUNT_CAL register is zero, the current value reported through the CURRENT register is also zero. After programming the SHUNT_CAL register with the calculated value, the measured current in Amperes can be read from the CURRENT register. The final value is scaled by CURRENTLSB
float INA228::getCurrent()
{
// PAGE 31 (8.1.2)
float shunt_cal = 13107.2e6 * _current_LSB * _shunt;
// depends on ADCRANGE in INA228_CONFIG register.
if (getADCRange() == 1)
{
shunt_cal *= 4;
}
// shunt_cal must be written to REGISTER.
// work in progress PR #7
_writeRegister(INA228_SHUNT_CAL, shunt_cal);
// remove reserved bits.
uint32_t value = _readRegister(INA228_CURRENT, 3) >> 4;
return value * _current_LSB;
}
please give it a try.
@xkachya
I have updated a lot of code in the develop branch, this includes your work in this PR (including my remarks). Have walked through the whole code and refactored several functions to either fix or make it more maintainable rtc.
If you have time could you give it a try to see it it works.
Again thanks for your time and work to improve this library!
@xkachya
Please note that 13107.2 * 1000000.0 = 13107.2e6 == scientific notation, Arduino floats can work with that. Don't know if you are familiar with it.
Note: the getDiagnoseAlertBit functions still need attention, but that is for another time.
Regarding SHUNT_CAL register. In my understanding, a configuration of this register should be done before the start of the measurement, during the initialization phase of the program. This is a configuration register, similar to ADC_CONFIG. We are configuring ADC_CONFIG during the initialization phase and then just reading from it. We need to configure the device once, and then retrieve the measurement results as much as we need. Also, based on my observation, I see the impact of the SHUNT_CAL register on the Power results. In my case, when I calculate and write the SHUNT_CAL during Setup() in my program, it is enough to read the correct values during the Loop() cycle. As for me, the setMaxCurrentShunt() is a better place to update the SHUNT_CAL register.
@xkachya Added a performance sketch in the examples to get a first level indication.
Regarding SHUNT_CAL register. ... As for me, the setMaxCurrentShunt() is a better place to update the SHUNT_CAL register.
mmm, you have a very good argument here. Will try to refactor that in today.
I patched the files + examples. build runs.
I've used develop branch to test it on my Project. I've removed my routines related to the SHUNT_CAL register update in the Project. And I've got valid results using the library only. It looks like _writeRegister() works fine now.
INA228 found.
#1: Shunt Calibration: 4096 - Shunt Calibration (bits): 1000000000000
#2: Shunt Calibration calc: 50 - Shunt Calibration (bits): 110010
#3: Shunt Calibration: 50 - Shunt Calibration (bits): 110010
#4: Temperature (DIETEMP): 2703 - DIETEMP (bits): 101010001111
#5: Temperature: 21.117188
Thank you for your work.
Thank you for your work.
You're welcome,
Maybe it is possible to use your test-project as an example with the library? Only if you are willing (and allowed) to share of course,
And I've got valid results using the library only. It looks like _writeRegister() works fine now.
Thanks for testing! That result means develop could be merged into master. (I want to check the readme against the .h file once again to see if I missed things.)
BTW, did you notice the new function getCurrentLSB()?
And final question, can you run the performance sketch, and post the results (+ board you used)?
(I want to check the readme against the .h file once again to see if I missed things.)
done
@xkachya I close this issue as the proposal are all in PR https://github.com/RobTillaart/INA228/pull/6
Today I got two INA228's and based upon the first test runs I had several minor changes in the above PR. So I'm going to merge and release 0.1.2, and then we can take it from there.
I was finally able to get the correct measurement of the Current. I've committed changes, which I'm sure are needed. But this is not enough to fix the measurement issue completely.
Change 1: Update for the getCurrent()
Change 2: Update for the setMaxCurrentShunt()
Change 3: In my project, I've added some routines to setup(void) to write SHUNT_CAL register calculated value
Here is the TERMINAL OUTPUT:
As a Load in my tests, I'm using a precise 126 Om resistor with 0.1% tolerance. Finally, I was able to get using the INA228 pretty close values to the INA231 results.