Skip to content

Commit 2080f8a

Browse files
Suke0811ducky64
andauthored
Add: Ina219 current/voltage sensor over I2C (#381)
- [x] Hardware to be tested, at least it seems to be alive so far --------- Co-authored-by: Richard Lin <[email protected]>
1 parent 8e58392 commit 2080f8a

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

edg/parts/Ina219.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from edg import *
2+
from edg.parts.JlcPart import JlcPart
3+
4+
5+
class Ina219_Device(InternalSubcircuit, JlcPart, FootprintBlock, Block):
6+
@init_in_parent
7+
def __init__(self):
8+
super().__init__()
9+
10+
self.vs = self.Port(VoltageSink(
11+
voltage_limits=(3, 5.5) * Volt,
12+
current_draw=(6 * uAmp, 1 * mAmp)
13+
))
14+
self.gnd = self.Port(Ground())
15+
16+
self.i2c = self.Port(I2cTarget(DigitalBidir.empty(), addresses=[0x40]))
17+
self.i2c.sda.init_from(DigitalBidir.from_supply(
18+
self.gnd, self.vs,
19+
voltage_limit_abs=(-0.3, 6.0) * Volt,
20+
input_threshold_factor=(0.3, 0.7)
21+
))
22+
self.i2c.scl.init_from(DigitalSink.from_supply(
23+
self.gnd, self.vs,
24+
voltage_limit_tolerance=(-0.3, 0.3) * Volt,
25+
input_threshold_factor=(0.3, 0.7)
26+
))
27+
28+
self.in_pos = self.Port(AnalogSink(voltage_limits=(-0.3, 26) * Volt))
29+
self.in_neg = self.Port(AnalogSink(voltage_limits=(-0.3, 26) * Volt))
30+
31+
def contents(self):
32+
super().contents()
33+
self.footprint(
34+
'U', 'Package_TO_SOT_SMD:SOT-23-8',
35+
{
36+
'1': self.in_pos,
37+
'2': self.in_neg,
38+
'3': self.gnd,
39+
'4': self.vs,
40+
'5': self.i2c.scl,
41+
'6': self.i2c.sda,
42+
'7': self.gnd, # TODO: make this address selectable, sa0 pin
43+
'8': self.gnd # TODO: make this address selectable, sa1 pin
44+
},
45+
mfr='Texas Instruments', part='INA219AIDCNR',
46+
datasheet='https://www.ti.com/lit/ds/symlink/ina219.pdf'
47+
)
48+
self.assign(self.lcsc_part, "C87469")
49+
self.assign(self.actual_basic_part, False)
50+
51+
52+
class Ina219(CurrentSensor, Block):
53+
"""Current/voltage/power monitor with I2C interface"""
54+
55+
@init_in_parent
56+
def __init__(self, shunt_resistor: RangeLike = 2.0 * mOhm(tol=0.05)):
57+
super().__init__()
58+
self.ic = self.Block(Ina219_Device())
59+
self.pwr = self.Export(self.ic.vs, [Power])
60+
self.gnd = self.Export(self.ic.gnd, [Common])
61+
self.i2c = self.Export(self.ic.i2c)
62+
63+
self.vs_cap = self.Block(DecouplingCapacitor(0.1 * uFarad(tol=0.2))).connected(self.gnd, self.pwr)
64+
65+
self.Rs = self.Block(CurrentSenseResistor(
66+
resistance=shunt_resistor,
67+
))
68+
69+
self.sense_pos = self.Export(self.Rs.pwr_in)
70+
self.sense_neg = self.Export(self.Rs.pwr_out)
71+
72+
def contents(self):
73+
super().contents()
74+
self.connect(self.Rs.sense_in, self.ic.in_pos)
75+
self.connect(self.Rs.sense_out, self.ic.in_neg)

edg/parts/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
from .CanTransceiver_Iso1050 import Iso1050dub
131131
from .CanTransceiver_Sn65hvd230 import Sn65hvd230
132132
from .CurrentSense_Ad8418 import Ad8418a
133+
from .Ina219 import Ina219
133134
from .BatteryProtector_S8261A import S8261A
134135
from .BatteryCharger_Mcp73831 import Mcp73831
135136
from .Distance_Vl53l0x import Vl53l0xBase, Vl53l0x, Vl53l0xConnector, Vl53l0xArray

0 commit comments

Comments
 (0)