Skip to content

Commit c88483d

Browse files
committed
Copy INA219 files from subfact_pi_ina219 repo
these came from: https://github.com/scottjw/subfact_pi_ina219.git commit 0c6f5965874d006b93f04bee95f8f892866d5705 Original authors: Author: Scott J. Williamson <scott@sjwilliamson.ca> Author: Alexandre Scieux <scieux.alexandre@gmail.com> Signed-off-by: Matt Gumbel <mkg@protonmail.ch>
1 parent 9ff733d commit c88483d

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

Adafruit_INA219/Subfact_ina219.py

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
#!/usr/bin/python
2+
3+
import time
4+
import smbus
5+
from Adafruit_I2C import Adafruit_I2C
6+
7+
# ===========================================================================
8+
# INA219 Class
9+
# ===========================================================================
10+
11+
class INA219:
12+
i2c = None
13+
14+
# ===========================================================================
15+
# I2C ADDRESS/BITS
16+
# ==========================================================================
17+
__INA219_ADDRESS = 0x40 # 1000000 (A0+A1=GND)
18+
__INA219_READ = 0x01
19+
# ===========================================================================
20+
21+
# ===========================================================================
22+
# CONFIG REGISTER (R/W)
23+
# ===========================================================================
24+
__INA219_REG_CONFIG = 0x00
25+
# ===========================================================================
26+
__INA219_CONFIG_RESET = 0x8000 # Reset Bit
27+
__INA219_CONFIG_BVOLTAGERANGE_MASK = 0x2000 # Bus Voltage Range Mask
28+
__INA219_CONFIG_BVOLTAGERANGE_16V = 0x0000 # 0-16V Range
29+
__INA219_CONFIG_BVOLTAGERANGE_32V = 0x2000 # 0-32V Range
30+
31+
__INA219_CONFIG_GAIN_MASK = 0x1800 # Gain Mask
32+
__INA219_CONFIG_GAIN_1_40MV = 0x0000 # Gain 1, 40mV Range
33+
__INA219_CONFIG_GAIN_2_80MV = 0x0800 # Gain 2, 80mV Range
34+
__INA219_CONFIG_GAIN_4_160MV = 0x1000 # Gain 4, 160mV Range
35+
__INA219_CONFIG_GAIN_8_320MV = 0x1800 # Gain 8, 320mV Range
36+
37+
__INA219_CONFIG_BADCRES_MASK = 0x0780 # Bus ADC Resolution Mask
38+
__INA219_CONFIG_BADCRES_9BIT = 0x0080 # 9-bit bus res = 0..511
39+
__INA219_CONFIG_BADCRES_10BIT = 0x0100 # 10-bit bus res = 0..1023
40+
__INA219_CONFIG_BADCRES_11BIT = 0x0200 # 11-bit bus res = 0..2047
41+
__INA219_CONFIG_BADCRES_12BIT = 0x0400 # 12-bit bus res = 0..4097
42+
43+
__INA219_CONFIG_SADCRES_MASK = 0x0078 # Shunt ADC Resolution and Averaging Mask
44+
__INA219_CONFIG_SADCRES_9BIT_1S_84US = 0x0000 # 1 x 9-bit shunt sample
45+
__INA219_CONFIG_SADCRES_10BIT_1S_148US = 0x0008 # 1 x 10-bit shunt sample
46+
__INA219_CONFIG_SADCRES_11BIT_1S_276US = 0x0010 # 1 x 11-bit shunt sample
47+
__INA219_CONFIG_SADCRES_12BIT_1S_532US = 0x0018 # 1 x 12-bit shunt sample
48+
__INA219_CONFIG_SADCRES_12BIT_2S_1060US = 0x0048 # 2 x 12-bit shunt samples averaged together
49+
__INA219_CONFIG_SADCRES_12BIT_4S_2130US = 0x0050 # 4 x 12-bit shunt samples averaged together
50+
__INA219_CONFIG_SADCRES_12BIT_8S_4260US = 0x0058 # 8 x 12-bit shunt samples averaged together
51+
__INA219_CONFIG_SADCRES_12BIT_16S_8510US = 0x0060 # 16 x 12-bit shunt samples averaged together
52+
__INA219_CONFIG_SADCRES_12BIT_32S_17MS = 0x0068 # 32 x 12-bit shunt samples averaged together
53+
__INA219_CONFIG_SADCRES_12BIT_64S_34MS = 0x0070 # 64 x 12-bit shunt samples averaged together
54+
__INA219_CONFIG_SADCRES_12BIT_128S_69MS = 0x0078 # 128 x 12-bit shunt samples averaged together
55+
56+
__INA219_CONFIG_MODE_MASK = 0x0007 # Operating Mode Mask
57+
__INA219_CONFIG_MODE_POWERDOWN = 0x0000
58+
__INA219_CONFIG_MODE_SVOLT_TRIGGERED = 0x0001
59+
__INA219_CONFIG_MODE_BVOLT_TRIGGERED = 0x0002
60+
__INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED = 0x0003
61+
__INA219_CONFIG_MODE_ADCOFF = 0x0004
62+
__INA219_CONFIG_MODE_SVOLT_CONTINUOUS = 0x0005
63+
__INA219_CONFIG_MODE_BVOLT_CONTINUOUS = 0x0006
64+
__INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS = 0x0007
65+
# ===========================================================================
66+
67+
# ===========================================================================
68+
# SHUNT VOLTAGE REGISTER (R)
69+
# ===========================================================================
70+
__INA219_REG_SHUNTVOLTAGE = 0x01
71+
# ===========================================================================
72+
73+
# ===========================================================================
74+
# BUS VOLTAGE REGISTER (R)
75+
# ===========================================================================
76+
__INA219_REG_BUSVOLTAGE = 0x02
77+
# ===========================================================================
78+
79+
# ===========================================================================
80+
# POWER REGISTER (R)
81+
# ===========================================================================
82+
__INA219_REG_POWER = 0x03
83+
# ===========================================================================
84+
85+
# ==========================================================================
86+
# CURRENT REGISTER (R)
87+
# ===========================================================================
88+
__INA219_REG_CURRENT = 0x04
89+
# ===========================================================================
90+
91+
# ===========================================================================
92+
# CALIBRATION REGISTER (R/W)
93+
# ===========================================================================
94+
__INA219_REG_CALIBRATION = 0x05
95+
# ===========================================================================
96+
97+
# Constructor
98+
def __init__(self, address=0x40, debug=False):
99+
self.i2c = Adafruit_I2C(address, debug=False)
100+
self.address = address
101+
self.debug = debug
102+
103+
self.ina219SetCalibration_32V_2A()
104+
105+
def twosToInt(self, val, len):
106+
# Convert twos compliment to integer
107+
108+
if(val & (1 << len - 1)):
109+
val = val - (1<<len)
110+
111+
return val
112+
113+
def ina219SetCalibration_32V_2A(self):
114+
self.ina219_currentDivider_mA = 10 # Current LSB = 100uA per bit (1000/100 = 10)
115+
self.ina219_powerDivider_mW = 2 # Power LSB = 1mW per bit (2/1)
116+
117+
# Set Calibration register to 'Cal' calculated above
118+
bytes = [(0x1000 >> 8) & 0xFF, 0x1000 & 0xFF]
119+
self.i2c.writeList(self.__INA219_REG_CALIBRATION, bytes)
120+
121+
# Set Config register to take into account the settings above
122+
config = self.__INA219_CONFIG_BVOLTAGERANGE_32V | \
123+
self.__INA219_CONFIG_GAIN_8_320MV | \
124+
self.__INA219_CONFIG_BADCRES_12BIT | \
125+
self.__INA219_CONFIG_SADCRES_12BIT_1S_532US | \
126+
self.__INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS
127+
128+
bytes = [(config >> 8) & 0xFF, config & 0xFF]
129+
self.i2c.writeList(self.__INA219_REG_CONFIG, bytes)
130+
131+
def getBusVoltage_raw(self):
132+
result = self.i2c.readU16(self.__INA219_REG_BUSVOLTAGE)
133+
134+
# Shift to the right 3 to drop CNVR and OVF and multiply by LSB
135+
return (result >> 3) * 4
136+
137+
def getShuntVoltage_raw(self):
138+
result = self.i2c.readList(self.__INA219_REG_SHUNTVOLTAGE,2)
139+
if (result[0] >> 7 == 1):
140+
testint = (result[0]*256 + result[1])
141+
othernew = self.twosToInt(testint, 16)
142+
return othernew
143+
else:
144+
return (result[0] << 8) | (result[1])
145+
146+
def getCurrent_raw(self):
147+
result = self.i2c.readList(self.__INA219_REG_CURRENT,2)
148+
if (result[0] >> 7 == 1):
149+
testint = (result[0]*256 + result[1])
150+
othernew = self.twosToInt(testint, 16)
151+
return othernew
152+
else:
153+
return (result[0] << 8) | (result[1])
154+
155+
def getPower_raw(self):
156+
result = self.i2c.readList(self.__INA219_REG_POWER,2)
157+
if (result[0] >> 7 == 1):
158+
testint = (result[0]*256 + result[1])
159+
othernew = self.twosToInt(testint, 16)
160+
return othernew
161+
else:
162+
return (result[0] << 8) | (result[1])
163+
164+
def getShuntVoltage_mV(self):
165+
value = self.getShuntVoltage_raw()
166+
return value * 0.01
167+
168+
def getBusVoltage_V(self):
169+
value = self.getBusVoltage_raw()
170+
return value * 0.001
171+
172+
def getCurrent_mA(self):
173+
valueDec = self.getCurrent_raw()
174+
valueDec /= self.ina219_currentDivider_mA
175+
return valueDec
176+
177+
def getPower_mW(self):
178+
valueDec = self.getPower_raw()
179+
valueDec /= self.ina219_powerDivider_mW
180+
return valueDec

Adafruit_INA219/ina219_cacti.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/python
2+
3+
from Subfact_ina219 import INA219
4+
5+
ina = INA219()
6+
result = ina.getBusVoltage_V()
7+
8+
print "shunt:%.3f bus:%.3f current:%d power:%d" % ( ina.getShuntVoltage_mV(), ina.getBusVoltage_V(), ina.getCurrent_mA(), ina.getPower_mW() )

Adafruit_INA219/ina219_example.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/python
2+
3+
from Subfact_ina219 import INA219
4+
import time
5+
6+
ina = INA219()
7+
8+
while True:
9+
result = ina.getBusVoltage_V()
10+
print "{} Shunt: {} mV, Bus: {} V, Current: {} mA".format(time.time(), ina.getShuntVoltage_mV(), ina.getBusVoltage_V(), ina.getCurrent_mA())

0 commit comments

Comments
 (0)