INA219 Demo Program V1.1 voltage above 15.0V is unreadable
Hello,
I was using the demo program in spin as below:
What is happening is:
Notice that th bus voltage and the load voltage are good, and they match my voltmeter, all is well
INA219 Current Sensor Demo
Shunt Voltage: 0.01 mV
Bus Voltage: 14.52 V
Current: 0.12 mA
Load Voltage: 14.52 V
Count: 1320
when i go above about 15.00 volts I get this: (the voltage off my meter is 17.03V)
INA219 Current Sensor Demo
Shunt Voltage: 0.02 mV
Bus Voltage: 2147000 V
Current: 0.32 mA
Load Voltage: 2147000 V
Count: 1775
I looked at float to string and int to float and don't see anything obvious.
Here is the program, I made no changes to it, it seemed to work fine, except for this one item:
regards
Jeff
I was using the demo program in spin as below:
What is happening is:
Notice that th bus voltage and the load voltage are good, and they match my voltmeter, all is well
INA219 Current Sensor Demo
Shunt Voltage: 0.01 mV
Bus Voltage: 14.52 V
Current: 0.12 mA
Load Voltage: 14.52 V
Count: 1320
when i go above about 15.00 volts I get this: (the voltage off my meter is 17.03V)
INA219 Current Sensor Demo
Shunt Voltage: 0.02 mV
Bus Voltage: 2147000 V
Current: 0.32 mA
Load Voltage: 2147000 V
Count: 1775
I looked at float to string and int to float and don't see anything obvious.
Here is the program, I made no changes to it, it seemed to work fine, except for this one item:
{{
┌──────────────────────────────────────────┐
│ INA219 Demo Program V1.1 │
│ Author: Nick Ernst │
│ Copyright (c) 2013 Parallax Inc │
│ See end of file for terms of use. │
└──────────────────────────────────────────┘
V1.1 Updates (9/16/2013):
-Changed to "I2C" object from "Basic I2C Driver".
-The "Current" register is re-calibrated each time through the loop
to ensure you get a usable reading in case of a brownout or reset.
This program starts off by checking and ensuring that the INA219 is connected to the appropriate pins for the
demo program to function. If you have connected the pins as required, the program will calibrate the INA219, and
then begin running through the demo showing you bus voltage, shunt voltage, current etc. If the connections are not
correct, ie backwards, the demo will tell you to check your connections. If you get this message, ensure that you
are using the correct pins, and that you have power and ground connected correctly. Fix any errors and then try the
program again.
To see readings from the INA219, you will need to connect a module/device to the sensor to measure the load.
If you use a separate power supply for your unit under test, ensure that you have a common ground
connection or you will not see any readings from your board.
If you want to use the sensor in your own application, you can cut any of the Private Methods that you require
and paste them into your program. The methods were written so that you can grab those by themselves and incorporate
as easily as possible into you project.
******************** Connection Diagram ********************
┌──────────────┐
3.3v ┤Vcc │
Gnd ┤Gnd Vin- ├ Connect to positive terminal of the power supply for the circuit under test.
P0 ┤SCL │
P1 ┤SDA │
N/C ┤Vin- Vin+ ├ Connect to the positive terminal/lead of the load.
N/C ┤Vin+ │
└──────────────┘
******************** Example Connection ********************
To measure the voltage and current of an LED, connect an LED
as shown below, and a power source. Ensure that you connect
the negative lead of the power source to GND as well, or your
readings will be off, and a little strange!
┌──────────────┐
3.3v ┤Vcc │
Gnd ┤Gnd Vin- ├─ ─── 9VDC (Positive lead from a 9v battery or similar source)
P0 ┤SCL │
P1 ┤SDA │
N/C ┤Vin- Vin+ ├───────── Gnd
N/C ┤Vin+ │ 470
└──────────────┘ ohm
}}
CON
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
SCLpin = 0
SDApin = 1
'------I2C Address-----------------
INA219_Address = %1000_0000
INA219_Read = %0000_0001
'------Config Registers (R/W)------
Reg_Config = %0000_0000
Config_Reset = %1000_0000_0000_0000 '$8000
Config_BVoltageRange_Mask = %0010_0000_0000_0000 '$2000
Config_BVoltageRange_16V = %0000_0000 '$0000
Config_BVoltageRange_32V = %0010_0000_0000_0000 '$2000
Config_Gain_Mask = %0001_1000_0000_0000 '$1800
Config_Gain_1_40mV = %0000_0000 '$0000
Config_Gain_2_80mV = %0000_1000_0000_0000 '$0800
Config_Gain_4_160mV = %0001_0000_0000_0000 '$1000
Config_Gain_8_320mV = %0001_1000_0000_0000 '$1800
Config_Badcres_Mask = %0111_1000_0000 '$0780
Config_Badcres_9Bit = %1000_0000 '$0080
Config_Badcres_10Bit = %0001_0000_0000 '$0100
Config_Badcres_11Bit = %0010_0000_0000 '$0200
Config_Badcres_12Bit = %0100_0000_0000 '$0400
Sadcres_Mask = %0111_1000 '$0078
Sadcres_9Bit_1s_84us = %0000_0000 '$0000
Sadcres_10Bit_1s_148us = %0000_1000 '$0008
Sadcres_11Bit_1s_276us = %0001_0000 '$0010
Sadcres_12Bit_1s_532us = %0001_1000 '$0018
Sadcres_12Bit_2s_1060us = %0100_1000 '$0048
Sadcres_12Bit_4s_2130us = %0101_0000 '$0050
Sadcres_12Bit_8s_4260us = %0101_1000 '$0058
Sadcres_12Bit_16s_8510us = %0110_0000 '$0060
Sadcres_12Bit_32s_17ms = %0110_1000 '$0068
Sadcres_12Bit_64s_34ms = %0111_0000 '$0070
Sadcres_12Bit_128s_69ms = %0111_1000 '$0078
Mode_Mask = %0000_0111 '$0007
Mode_Powerdown = %0000_0000 '$0000
Mode_SVolt_Triggered = %0000_0001 '$0001
Mode_BVolt_Triggered = %0000_0010 '$0002
Mode_SandBVolt_Triggered = %0000_0011 '$0003
Mode_ADCOff = %0000_0100 '$0004
Mode_SVolt_Continuous = %0000_0101 '$0005
Mode_BVolt_Continuous = %0000_0110 '$0006
Mode_SandBVolt_Continuous = %0000_0111 '$0007
INA219_Reg_ShuntVoltage = %0000_0001 '$01
INA219_Reg_BusVoltage = %0000_0010 '$02
INA219_Reg_Power = %0000_0011 '$03
INA219_Reg_Current = %0000_0100 '$04
INA219_Reg_Calibration = %0000_0101 '$05
OBJ
I2C : "i2c"
PST : "Parallax Serial Terminal"
Math : "FloatMath"
FS : "FloatString"
VAR
Long ina219_CurrentDivider_mA
Long ina219_powerDivider_mW
Long ShuntVoltage
Long BusVoltage
Long Current_mA
Long LoadVoltage
Word config
Long Count
PUB Setup
I2C.Init(SDApin, SCLpin, 1)
FS.SetPrecision(4)
waitcnt(clkfreq*2+cnt) 'Gives you a 2 second period to open PST before the program begins
PST.Start(9600)
PST.Clear
If i2c.devicepresent(INA219_Address) == true 'Checks to make sure the sensor is connected
PST.str(string("INA219 Present....."))
waitcnt(clkfreq+cnt)
PST.str(string("Calibrating",13))
waitcnt(clkfreq+cnt)
Calibrate
PST.str(string("Calibration Complete"))
waitcnt(clkfreq*2+cnt)
else
PST.str(string("INA219 Not Present.... :(",13)) 'If you get this message, check your connections
PST.str(string(" Check your connections ")) 'against the connection diagram above
repeat
PST.Clear
repeat
PST.Home
PST.str(string("INA219 Current Sensor Demo",13,13))
ShuntVoltage := Get_Shunt_Voltage
PST.str(string("Shunt Voltage: "))
PST.str(FS.FloatToString(ShuntVoltage))
PST.str(string(" mV ",13))
BusVoltage := Get_Bus_Voltage
PST.str(string("Bus Voltage: "))
PST.str(FS.FloatToString(BusVoltage))
PST.str(string(" V ",13))
Current_mA := GetCurrent_mA
PST.str(string("Current: "))
PST.str(FS.FloatToString(Current_mA))
' PST.dec(Current_mA)
PST.str(string(" mA ",13))
LoadVoltage := Bus_and_Shunt_Voltage
PST.str(string("Load Voltage: "))
PST.str(FS.FloatToString(LoadVoltage))
PST.str(string(" V ",13,13))
PST.str(string("Count: "))
PST.dec(Count)
PST.str(string(" "))
Count++
PRI Get_Shunt_Voltage | shunt_voltage
shunt_voltage := I2C.Read(INA219_Address, INA219_Reg_ShuntVoltage, 8, 16)
~~shunt_voltage
shunt_voltage := Math.FFloat(shunt_voltage)
shunt_voltage := Math.FMul(shunt_voltage,0.01)
return shunt_voltage
PRI Get_Bus_Voltage | bus_voltage
bus_voltage := I2C.Read(INA219_Address, INA219_Reg_BusVoltage, 8, 16)
~~bus_voltage
bus_voltage := ((bus_voltage >> 3)*4)
bus_voltage := Math.FFloat(bus_voltage)
bus_voltage := Math.FMul(bus_voltage, 0.001)
return bus_voltage
PRI Bus_and_Shunt_Voltage | bsv, a
a := Math.FDiv(ShuntVoltage, 1000.0)
bsv := Math.FAdd(BusVoltage, a)
return bsv
PRI GetCurrent_mA | current
I2C.WriteWord(INA219_Address, INA219_Reg_Calibration, %0010_1000_0000_0000, 8)
current := I2C.Read(INA219_Address, INA219_Reg_Current, 8, 16)
~~current
current := Math.FFloat(current)
current := Math.FDiv(current, ina219_CurrentDivider_mA)
' current /= 25.0
return current
PRI Calibrate
ina219_CurrentDivider_mA := 25.0
ina219_powerDivider_mW := 1
I2C.WriteWord(INA219_Address, INA219_Reg_Calibration, %0010_1000_0000_0000, 8)
config := Config_BVoltageRange_32V|Config_Gain_8_320mV|Config_Badcres_12Bit|Sadcres_12Bit_1s_532us|Mode_SandBVolt_Continuous
I2C.WriteWord(INA219_Address, Reg_Config, config, 8)
{{
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ TERMS OF USE: MIT License │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation │
│files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, │
│modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software│
│is furnished to do so, subject to the following conditions: │
│ │
│The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.│
│ │
│THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE │
│WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR │
│COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │
│ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
}}
regards
Jeff

Comments
I was looking at the config table
this table looks incorrect
Config_Badcres_Mask = %0111_1000_0000 '$0780 'correct
Config_Badcres_9Bit = %1000_0000 '$0000 should be %0000_0000
Config_Badcres_10Bit = %0001_0000_0000 '$0080 should be %1000_0000
Config_Badcres_11Bit = %0010_0000_0000 '$0100 should be %0001_0000_0000
Config_Badcres_12Bit = %0100_0000_0000 '$0180 should be %0001_1000_0000
so the config is loading %0100_0000_0000, but is seems that all the other values are at default, which in this case would be %0001_1000_0000 bits 7,8,9,10
anyway, I'll try this tomorrow.
regards
Jeff
I tested the above changes, still not working correctly.
regards
Jeff
the error point is 16.1 volts, after that you get this:
INA219 Current Sensor Demo
Shunt Voltage: 0.02 mV
Bus Voltage: 2147000 V
Current: 0.32 mA
Load Voltage: 2147000 V
Count: 1775
I'm stil not sure where the error is
Math.Floating point
Math_to_string
I2C
or the config data of the INA219
regards
Jeff
Does it get multiplied by 1000 in there to put it into mV while on the integer side? Perhaps multiply it by 100 instead and shift it again later
I changed these two values and run it again,
PRI Get_Bus_Voltage | bus_voltage (subroutine)
' bus_voltage := Math.FMul(bus_voltage, 0.001)
bus_voltage := Math.FMul(bus_voltage, 1.0)
PRI Bus_and_Shunt_Voltage | bsv, a (subroutine)
'a := Math.FDiv(ShuntVoltage, 1000.0)
a := Math.FDiv(ShuntVoltage, 1.0)
results: above 16.1 Volts
INA219 Current Sensor Demo
Shunt Voltage: 0.02 mV
Bus Voltage: 2150000000 V
Current: 0.141 mA
Load Voltage: 2150000000 V
Count: 1312
normal up to 16.1 volts (with the adjusted parms above
INA219 Current Sensor Demo
Shunt Voltage: 0.03 mV
Bus Voltage: 15800 V
Current: 0.196 mA
Load Voltage: 15800 V
Count: 2011
regards
Jeff
Config_BVoltageRange_16V = $0000
Config_BVoltageRange_32V = $2000
I see that your program is setting it, but maybe there is something screwy in the i2c. Look at the raw values coming back from
bus_voltage := I2C.Read(INA219_Address, INA219_Reg_BusVoltage, 8, 16)
as it passes thru 16V.
The default at power up is 32Vfs, 320mV shunt. Maybe try it without setting the configuration at all in your program.
Config_BVoltageRange_16V = $0000
Config_BVoltageRange_32V = $2000
I see that your program is setting it, but maybe there is something screwy in the i2c. Look at the raw values coming back from
bus_voltage := I2C.Read(INA219_Address, INA219_Reg_BusVoltage, 8, 16)
as it passes thru 16V.
The default at power up is 32Vfs, 320mV shunt. Maybe try it without setting the configuration at all in your program.[/QUOTE]
yes, I tried that before (a few days ago) , using hex 0000 instead ofhex 2000, and it blocks anything above 16, so there are no errors but you can't go above 16, but I will check the raw value on the return of
bus_voltage := I2C.Read(INA219_Address, INA219_Reg_BusVoltage, 8, 16)
regards
Jeff
I am getting back with my results
in order to get voltages above 16, I changed this (Get_Bus_Voltage ) on the demo code
now you can get voltages up to 32, although my supply is only 30v dc.
regards
Jeff