Measuring negative currents with the INA219

Measuring negative currents with the INA219

 

Note:    These INA219 devices have been replaced by our 3 Channel INA3221 board.

The INA3221 Breakout Board Replaces 3 INA219 Boards
The INA3221 Breakout Board Replaces 3 INA219 Boards

We read somewhere that the Adafruit C++ library has a bug in the INA219 High Side current measuring device library so the device doesn’t correctly return negative values for current.

Obviously, for a solar power system, We need to measure negative currents (current goes both out of and into the battery- when charging). We ran across this problem a couple of months ago and figured it out. I was using the Subfact Python library for the INA219.


There is also a similar bug in the subfact python library as in the Adafruit C++ library for the INA219 (fixed as of November 2013). They aren’t calculating twos complement correctly, so negative currents aren’t reported correctly.

The ProjectCuracao unit is plugged in at moment to a USB charger, hence the funny looking efficiency numbers. On my RasPiConnect control screen, you can clearly see the -6ma heading into the battery.  Special thanks to “faraday” for finding the same issue and contributing to the fix.

Heres are the fixes for the subfact python library for the INA219.:


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])