ArduinoGeneral

INA219

userHead javier.galindo 2021-12-01 06:17:16 693 Views4 Replies
Hello

I just started tu use the I2C Wattmeter INA219...

In my first test I measured a bus bar voltage above 15V... let's say 18V... well below the 26 VMax...
The fact is that, when reading this voltage with the example provided within your library, I see a negative value...
I have been reviewing the library and I think this comes from the int16_t that is used in the library in
int16_t DFRobot_INA219::readInaReg(uint8_t reg)
I think this results from a defective cast from int16 to float... for values above aprox 16V the cast overflow due to the signed int that it is returned by the library... when modifying the library to use uint16 instead of int it worked pretty well...
Could you confirm that there won't be any side effects with this change in the library?

Thanks
2025-06-02 16:33:33

Hi, here is the solution after much time spend to find it (with ChatGPT help):

 

In your copy of DFRobot_INA219.cpp, locate the existing getBusVoltage_V():

 

float DFRobot_INA219::getBusVoltage_V() {    return (float) (readInaReg(INA219_REG_BUSVOLTAGE) >> 1) * 0.001; }

 

Replace it with:

 

float DFRobot_INA219::getBusVoltage_V() {    // 1) Read the raw 16-bit bus-voltage word as unsigned:    uint16_t raw = 

 

(uint16_t)readInaReg(INA219_REG_BUSVOLTAGE);    // 2) Drop the three lowest bits (flags), leaving bits [15:3] = 10-bit bus voltage

 

raw >>= 3;   // 3) Each LSB is 4mV, so multiply by 0.004 to get volts

 

return (float)raw * 0.004f; }

 

That change forces the library to:

- Treat the register as unsigned (so bit 15 never “flips” to a negative signed value)- Shift off exactly three status bits (instead of just one), matching the datasheet’s 10-bit bus‐voltage field- Apply a 4 mV LSB scale directly

 

Once you recompile with this new getBusVoltage_V(), any voltage up to 32 V will be reported correctly (no more negative readings above 16 V).

userHeadPic treidece
2025-01-27 05:16:55

DFRobot_INA219 not compiling under arduino 2.3.1 to 2.3.4 , but working with the older 1.8.19..

 

Any fix for this ???

Got 10 pieces , would really like a solution for this issue.

 

Thanks Michael

userHeadPic Michael.Vernersen
2024-07-17 05:03:17

Hey, could you share me your whole rewritten library-code? I have the same problem too. I want to measure the voltage of a 6s LiPo battery. 

 

Thanks

userHeadPic SwermHD
2021-12-10 03:00:57 Up... userHeadPic javier.galindo