There is a similar bug in the subfact python library as in the Adafruit C++ library for the INA219. They aren't calculating twos complement correctly, so negative currents aren't reported correctly.

I'm building a solar power charging system based on the RaspBerry Pi, three INA219 sensors for solar current, battery current and pi current using a LiPo Rider Pro and a couple of Adafruit Solar cells. I'm using RasPiConnect to connect to my iPad (picture attached) so I can monitor everything and control it remotely.

The unit is plugged in at moment to a USB charger, hence the funny looking efficiency numbers. But you can clearly see the -120ma heading into the battery.

Heres are the fixes:

A twos complement function:

- Code: Select all | TOGGLE FULL SIZE
`def twosToInt( val, len):`

# Convert twos compliment to integer

if(val & (1 << len - 1)):

val = val - (1<<len)

return val

Changes to subfact python library:

def getShuntVoltage_raw(self):

result = self.i2c.readList(self.__INA219_REG_SHUNTVOLTAGE,2)

if (result[0] >> 7 == 1):

testint = (result[0]*256 + result[1])

othernew = twosToInt( testint, 16)

return othernew

else:

return (result[0] << 8) | (result[1])

def getCurrent_raw(self):

result = self.i2c.readList(self.__INA219_REG_CURRENT,2)

if (result[0] >> 7 == 1):

testint = (result[0]*256 + result[1])

othernew = twosToInt( testint, 16)

return othernew

else:

return (result[0] << 8) | (result[1])

def getPower_raw(self):

result = self.i2c.readList(self.__INA219_REG_POWER,2)

if (result[0] >> 7 == 1):

testint = (result[0]*256 + result[1])

othernew = twosToInt( testint, 16)

return othernew

else:

return (result[0] << 8) | (result[1])

Now everything is happy with my solar charging system!

Faraday