0

INA219 python library
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: INA219 python library

by faraday on Sun Sep 15, 2013 8:49 pm

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
IMG_0125.PNG (715.73 KiB) Viewed 3404 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 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
faraday
 
Posts: 1
Joined: Sun Sep 15, 2013 8:42 pm

Re: INA219 python library

by marks3 on Mon Sep 16, 2013 12:45 pm

faraday wrote: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.

Faraday


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
Location: Toronto, Ontario, Canada

Re: INA219 python library

by alvin_yc on Wed Oct 09, 2013 5:24 pm

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

by marks3 on Thu Oct 10, 2013 2:51 pm

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
Location: Toronto, Ontario, Canada

Re: INA219 python library

by mr8k on Fri Feb 06, 2015 1:03 pm

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

by marks3 on Fri Feb 06, 2015 3:21 pm

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
Location: Toronto, Ontario, Canada

Re: INA219 python library

by static on Fri Feb 06, 2015 4:03 pm

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: 182
Joined: Thu Dec 23, 2010 6:21 pm

Re: INA219 python library

by mr8k on Sun Feb 08, 2015 11:33 am

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):
result = self.i2c.readU16(self.__INA219_REG_BUSVOLTAGE)

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

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

def getBusVoltage_raw(self):
result = self.i2c.readList(self.__INA219_REG_BUSVOLTAGE,2)
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

by static on Sun Feb 08, 2015 12:41 pm

Peter,
Nice work, amigo!

static
 
Posts: 182
Joined: Thu Dec 23, 2010 6:21 pm

Re: INA219 python library

by Anna553 on Thu May 28, 2015 11:47 pm

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

by adafruit_support_mike on Fri May 29, 2015 1:43 am

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

adafruit_support_mike
 
Posts: 58799
Joined: Thu Feb 11, 2010 2:51 pm

Re: INA219 python library

by Anna553 on Fri May 29, 2015 3:59 am

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

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

Re: INA219 python library

by adafruit_support_mike on Sat May 30, 2015 2:30 am

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.

adafruit_support_mike
 
Posts: 58799
Joined: Thu Feb 11, 2010 2:51 pm

Re: INA219 python library

by Anna553 on Mon Jun 01, 2015 11:58 pm

adafruit_support_mike wrote: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.

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
Optimized-IMG_0678.JPG (334.14 KiB) Viewed 1940 times

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

Re: INA219 python library

by adafruit_support_mike on Wed Jun 03, 2015 12:53 am

That does sound like a dead sensor.

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

adafruit_support_mike
 
Posts: 58799
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.