INA219 python library

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
marks3
 
Posts: 52
Joined: Fri Oct 12, 2012 1:07 pm

Re: INA219 python library

Post by marks3 »

I don't understand why 'support_mike' is saying that the INA219 won't measure voltage. The product description indicates it will be very suitable for solar panel monitoring up to 26 volts.

Mike, please clarify. Also, do you know of any code that will read this breakout board on a Raspberry? I would rather not have to use an Arduino in addition to the Pi.

Thanks.

User avatar
Renate
 
Posts: 291
Joined: Tue Nov 13, 2012 3:21 pm

Re: INA219 python library

Post by Renate »

The INA219 will most certainly measure voltage with an LSB of 4 mV up to the maximum rating of 26 V.
It will also internally multiply voltage and current for a power reading.

The INA219 is a pretty neat chip, but I'd prefer to do away with the shunt resistor and use a Hall effect sensor.
The Allegro chips are neat but only have an analog output.
http://www.allegromicro.com/en/Products ... r-ICs.aspx
Does anyone know of a nice integrated conductor Hall effect digital solution?

User avatar
marks3
 
Posts: 52
Joined: Fri Oct 12, 2012 1:07 pm

Re: INA219 python library

Post by marks3 »

Downloaded the python library from the adafruit git hub. Some good stuff there for i2c interfacing.

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: INA219 python library

Post by adafruit_support_mike »

You're right that the INA219 will measure the supply voltage. I was thinking of the INA216, an analog current monitor that amplifies the voltage across a shunt resistor.

User avatar
marks3
 
Posts: 52
Joined: Fri Oct 12, 2012 1:07 pm

Re: INA219 python library

Post by marks3 »

Thanks Mike. I am making headway with the INA219. Got the python code working. There is a bug with Raspberry Pi version detection in the Adafruit code off the git hub. I have a few Pi version 1's that have a '/proc/cpuinfo' reporting '0003' rather than the '0001' or '0002'. My version 2 Pi units report '000f'. Not sure how to post the simple code fix up to the git hub.

In any event... My next question is what constants in the python code need to be changed when I snip out the R100 shunt and put in a 0.025 ohm resistor from Ohmite. Maybe nothing has to change??

Not sure. Help from Adafruit and/or the community would be appreciated.

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: INA219 python library

Post by adafruit_support_mike »

Took me a bit of digging to find the code you were talking about.. everything we have for the INA219 is written in C++ and made for the Arduino.

If you're referring to this: https://github.com/scottjw/subfact_pi_ina219 , that's a port of our C++ library to Python written by scottjw.. awesome on his part, but not in the official chain-o-stuff-we-control.

To adjust the code for a .025 ohm current sense resistor, you'll need to change a couple of numbers in Subfact_ina219.py:: ina219SetCalibration_32V_2A(). I'm leery of giving you specific numbers though, because the configuration depends on the supply voltage and the amount of current you expect to draw. The relevant part of the datasheet is pages 17-24: http://www.adafruit.com/datasheets/ina219.pdf

User avatar
marks3
 
Posts: 52
Joined: Fri Oct 12, 2012 1:07 pm

Re: INA219 python library

Post by marks3 »

adafruit_support_mike wrote:Took me a bit of digging to find the code you were talking about.. everything we have for the INA219 is written in C++ and made for the Arduino.

If you're referring to this: https://github.com/scottjw/subfact_pi_ina219 , that's a port of our C++ library to Python written by scottjw.. awesome on his part, but not in the official chain-o-stuff-we-control.

To adjust the code for a .025 ohm current sense resistor, you'll need to change a couple of numbers in Subfact_ina219.py:: ina219SetCalibration_32V_2A(). I'm leery of giving you specific numbers though, because the configuration depends on the supply voltage and the amount of current you expect to draw. The relevant part of the datasheet is pages 17-24: http://www.adafruit.com/datasheets/ina219.pdf
I did get scottjw's code to run without modification and no mod to the breakout board (so just using the 0.1 Ohm resistor for testing).
The current sensor numbers reported were dead on as compared to my measurements with a Fluke meter.
But I do see that the the Adafruit code and the python code is NOT fully complete. Both the cpp code and the python code are using hard coded values and the computations are commented out. Maybe someone will complete the library some day. I can code but I don't know enough about shunt resistors to be trusted.

User avatar
john444
 
Posts: 443
Joined: Sun Mar 04, 2012 2:42 pm

Re: INA219 python library

Post by john444 »

Hi Mark S3,

Just a comment for you about the 0.025-ohm resistor.
marks3 wrote: constants in the python code need to be changed when I snip out the R100 shunt and put in a 0.025 ohm resistor from Ohmite.
First, since the 0.025-ohm resistor is 1/4th the resistance of the 0.1-ohm (R100)
At the same current, the voltage drop across the 0.025 will be 1/4th
the voltage drop of the 0.1. When you find the place in the code, the
value will need to be changed to 4x larger to display correctly.

Second, instead of soldering the 0.025 onto the INA219's PC-board, connect
the 0.025 to a convenient location in the load circuit and extend a separate
pair of wires (voltage sense-wires) from the solder-pads of the 0.025 to
the the holes, located either side of where R100 was located.
Connect the sense-wires directly to the 0.025, not to a PC-board
or lead-wires going to the load.

Using sense-wires will allow you to use a physically larger resistor
(than the surface-mount 1206) or a shunt-resistor that would be
larger than would fit onto the INA219's PC-board.

Good Luck with your project, John

User avatar
marks3
 
Posts: 52
Joined: Fri Oct 12, 2012 1:07 pm

Re: INA219 python library

Post by marks3 »

john444 wrote:Hi Mark S3,

First, since the 0.025-ohm resistor is 1/4th the resistance of the 0.1-ohm (R100)
At the same current, the voltage drop across the 0.025 will be 1/4th
the voltage drop of the 0.1. When you find the place in the code, the
value will need to be changed to 4x larger to display correctly.

Second, instead of soldering the 0.025 onto the INA219's PC-board, connect
the 0.025 to a convenient location in the load circuit and extend a separate
pair of wires (voltage sense-wires) from the solder-pads of the 0.025 to
the the holes, located either side of where R100 was located.
Connect the sense-wires directly to the 0.025, not to a PC-board
or lead-wires going to the load.

Using sense-wires will allow you to use a physically larger resistor
(than the surface-mount 1206) or a shunt-resistor that would be
larger than would fit onto the INA219's PC-board.
Thanks for that advice. I just assumed that I would have to mutilate the breakout board. And yes, the Ohmite resister is a big axial thing with wire gauge that is even too big for thru hole or breadboard.

For those of you who haven't followed the entire thread... The goal here is to be able to handle up to 8 amps in a voltage range of about 10 to 18 VDC so that I can monitor the solar panel, charge controller and battery.

Mark

User avatar
marks3
 
Posts: 52
Joined: Fri Oct 12, 2012 1:07 pm

Re: INA219 python library

Post by marks3 »

Found the Texas Instruments Evaluation Module Software free download link. It calculates the Calibration Register Value but the result of '0x1A1F' when used in the python code gives me erroneous results with the Ohmite 0.025 Ohm resistor.

So still stumped as to how to program the breakout board for 32V mode, 8 Amps expected max load (12.8 possible max load).

I am thinking there are 3 constants that I need to get it right - Calibration register value, Current divisor, Power divisor.

Hmmm?

User avatar
marks3
 
Posts: 52
Joined: Fri Oct 12, 2012 1:07 pm

Re: INA219 python library

Post by marks3 »

marks3 wrote:So still stumped as to how to program the breakout board for 32V mode, 8 Amps expected max load (12.8 possible max load).
I am thinking there are 3 constants that I need to get it right - Calibration register value, Current divisor, Power divisor.
?
Well, I simply took the example code for 32 Volt / 2 Amp calibration and changed the Calibration register value from 0x1000 to 0x4000.

My Fluke is measuring current to a halogen light bulb at 1400 mW and the breakout board is reporting 1370 mW.

I did not change any other constants or divisors. Seems to 0.025 Ohm 1% resistor is working.

Thanks to all for comments and help. A learning experience!

User avatar
mapax
 
Posts: 1
Joined: Sun May 19, 2013 3:47 am

Re: INA219 python library

Post by mapax »

How do you read the output of the example python library?
I can get some values out, but the Shunt gives me just few mV.
assumed it should give me correct voltage readings from the load? In this case it should give the same voltage as the Bus..
By python library I mean the git code in: https://github.com/scottjw/subfact_pi_ina219

Here is the output, with the stock adafruit ina219 board.

pi@raspberrypi ~/git/subfact_pi_ina219 $ sudo python ina219_example.py
Shunt : -2.520 mV
Bus : 3.200 V
Current : -26.000 mA

User avatar
marks3
 
Posts: 52
Joined: Fri Oct 12, 2012 1:07 pm

Re: INA219 python library

Post by marks3 »

MapaX wrote:How do you read the output of the example python library?
I can get some values out, but the Shunt gives me just few mV.
assumed it should give me correct voltage readings from the load? In this case it should give the same voltage as the Bus..
By python library I mean the git code in: https://github.com/scottjw/subfact_pi_ina219

Here is the output, with the stock adafruit ina219 board.

pi@raspberrypi ~/git/subfact_pi_ina219 $ sudo python ina219_example.py
Shunt : -2.520 mV
Bus : 3.200 V
Current : -26.000 mA
I think you are getting a reading of 252 decimal which is near to 255 and so I am guessing that you have no load connected yet.

I see -26 as the current when I have no load. It is essentially a value of 260 which is a rounding error.

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

Re: INA219 python library

Post by faraday »

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 3980 times
Heres are the fixes:

A twos complement function:

Code: Select all

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

User avatar
marks3
 
Posts: 52
Joined: Fri Oct 12, 2012 1:07 pm

Re: INA219 python library

Post by marks3 »

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.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”