Adafruit Industries, Essential service and business: NYC – Executive Order 202.6 - Read more. Accepting essential orders - here's how.

Re: INA219 python library

Hi folks,

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.

IMG_0125.PNG (715.73 KiB) Viewed 3521 times

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 valChanges 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!

Posts: 1
Joined: Sun Sep 15, 2013 8:42 pm

Re: INA219 python library

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.

Thanks for finding that bug. I have not had much time to look at my results that are being logged but I will find some time to apply the code fix to the python code locally and see what happens.

Love my solar setup. Better than a UPS. Survives long power outages nicely. I expect to get several hundred days of uptime at any given stretch out of my 2 Pi's. One does web server work. The other just watches over the solar setup.

I have given up on wireless. The Pi's are now on ethernet and so much more stable now that power supply current draw is no longer a worry.

marks3

Posts: 48
Joined: Fri Oct 12, 2012 1:07 pm

Re: INA219 python library

Hi,
I just got 2x INA219 High Side DC Current Sensor Breakout and try to read these 2x DC current & voltage output.

Here are what I did:
- I run "sudo i2cdetect -y 1" to check address. It shows address 40 & 41.
- I managed to connected up to 1st motor to Breakout board (ox40) and no problem to read DC current and voltage.
- I also set up 2nd motor to Breakout board (ox41) and change program " Subfact_ina219.py" address to "__INA219_ADDRESS = 0x41" and constructor to " def __init__(self, address=0x41, debug=False):". Then I run "ina219_example.py". I have no problem read 2nd motor current and voltage.

Any ideal how can I program to read these 2x motors' current & voltage same time?

Thanks,
alvin_yc

Posts: 1
Joined: Wed Oct 09, 2013 4:58 pm

Re: INA219 python library

One easy way and sort of a lazy approach would be to run two copies of the python script in the background. Pass an argument to the script so it can decide which port to monitor.

Or just be really sloppy and create two versions of the script with the port addresses hard-coded and then run them both in the background.

Hope this helps.

marks3

Posts: 48
Joined: Fri Oct 12, 2012 1:07 pm

Re: INA219 python library

Hi,
I am building a multi battery monitor using INA219 and INA220. Using Raspberry Pi.

I am linking my program to ADAFRUIT_I2C.py and SUBFACT_INA29.py. All is OK BUT my bus voltage ADC resolution appears to be only 9 bits. (The shunt ADC is OK at 12 bits). So I only have voltage steps of around 12mV rather than 4mV !

Can I change something in Subfact-ina219/py to resolve this. If so what?

thanks in advance for any help.

cheers

Peter

mr8k

Posts: 2
Joined: Fri Feb 06, 2015 12:57 pm

Re: INA219 python library

I still monitor this form for my own education. You got me on this one - I can't say I have looked at the resolution to that accuracy. I agree that if the chip is capable then why not use it to the best resolution in the range you desire but I can't help. I wish the code was in C and not Python :)

Mark

marks3

Posts: 48
Joined: Fri Oct 12, 2012 1:07 pm

Re: INA219 python library

I'm getting ready to move, and I don't have my INA219 anymore.
However, I'm looking at putting in a multiple battery monitor at my new house, for a solar backup system. I'll put an order in for some new parts next week, but it will be a while before I can work on this.

static

Posts: 186
Joined: Thu Dec 23, 2010 6:21 pm

Re: INA219 python library

Sorted out the issue of Adafruit in219 with a Raspberry Pi only reading 8 bit bus voltage. There is a strange set of code in "Subfact_ina219.py" that causes this.

To overcome the issue and read bus voltage down to millivolts resolution apply the following changes to the file "Subfact_ina219.py"

FIND LINES
-----------------------------------------------------------------------------------
def getBusVoltage_raw(self):

# Shift to the right 3 to drop CNVR and OVF and multiply by LSB
return (result >> 3) * 4

-----------------------------------------------------------------------------------
REPLACE WITH
-----------------------------------------------------------------------------------

def getBusVoltage_raw(self):
if (result[0] >> 7 == 1):
testint = (result[0]*256 + result[1])
othernew = self.twosToInt(testint, 16)
return othernew
else:
return (result[0] << 8) | (result[1])

------------------------------------------------------------------------------------

FIND LNES
------------------------------------------------------------------------------------
def getBusVoltage_V(self):
value = self.getBusVoltage_raw()
return value * 0.001

------------------------------------------------------------------------------------
CHANGE MAGNITUDE MULTIPLIER TO 0.0005 AS BELOW
------------------------------------------------------------------------------------
def getBusVoltage_V(self):
value = self.getBusVoltage_raw()
return value * 0.0005

------------------------------------------------------------------------------------

Hope this helps anyone with a low 8 bit resolution output from the Adafruit INA219 routines, which by the way also work for the INA220 chip which i built up myself, as I needed low side current measurements, i.e. with a shunt between ground and the power source.

cheers

Peter

mr8k

Posts: 2
Joined: Fri Feb 06, 2015 12:57 pm

Re: INA219 python library

Peter,
Nice work, amigo!

static

Posts: 186
Joined: Thu Dec 23, 2010 6:21 pm

Re: INA219 python library

Hi,
Currently, I am trying to connect the raspberry pi B+ and ina219 together. However, when I try to check the ina219 address using "sudo i2cdetect -y 1", the address was not detected. I followed this tutorial "http://www.raspberrypi-spy.co.uk/2014/11/enabling-the-i2c-interface-on-the-raspberry-pi/#prettyPhoto" to do the configuration for i2c. Can anyone tell me why wasn't the ina219 address detected?? Thanks!!
I had connected the vcc to 5v, gnd to gnd, SCL to pin 05 and SDA to pin 03.

Anna553

Posts: 11
Joined: Thu May 28, 2015 10:47 pm

Re: INA219 python library

Post a photo showing your hardware and connections and we'll take a look. 800x600 images usually work best.

Posts: 61014
Joined: Thu Feb 11, 2010 2:51 pm

Re: INA219 python library

adafruit_support_mike wrote:Post a photo showing your hardware and connections and we'll take a look. 800x600 images usually work best.
Attachments
Here it is. Thanks!! :)
Optimized-IMG_0675.JPG (701.93 KiB) Viewed 2042 times

Anna553

Posts: 11
Joined: Thu May 28, 2015 10:47 pm

Re: INA219 python library

Thank you.

Your soldering looks good and the connections are correct. Let's make sure you're actually getting contact though. Take a voltage measurement between the VCC and GND pins on the INA219 breakout, and check continuity between the solder joints on the breakout's SDA/SCL pins and the pin header on the RasPi.

Sometimes you get a broken wire inside a jumper, making a visually perfect circuit fail.

Posts: 61014
Joined: Thu Feb 11, 2010 2:51 pm

Re: INA219 python library

Your soldering looks good and the connections are correct. Let's make sure you're actually getting contact though. Take a voltage measurement between the VCC and GND pins on the INA219 breakout, and check continuity between the solder joints on the breakout's SDA/SCL pins and the pin header on the RasPi.

Sometimes you get a broken wire inside a jumper, making a visually perfect circuit fail.

Hi, thanks for your reply. I had checked the voltage and continuity, it is fine. However, this is my results. Could it be the INA219 is faulty?
Attachments
Optimized-IMG_0678.JPG (334.14 KiB) Viewed 2011 times

Anna553

Posts: 11
Joined: Thu May 28, 2015 10:47 pm

Re: INA219 python library

That does sound like a dead sensor.

Did you get it from Adafruit, and if so, when?