Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Ina219 current/voltage sensor over I2C #381

Merged
merged 7 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions edg/parts/Ina219.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from edg import *
from edg.parts.JlcPart import JlcPart


class Ina219_Device(InternalSubcircuit, JlcPart, FootprintBlock, Block):
@init_in_parent
def __init__(self):
super().__init__()

self.vs = self.Port(VoltageSink(
voltage_limits=(3, 5.5) * Volt,
current_draw=(6 * uAmp, 1 * mAmp)
))
self.gnd = self.Port(Ground())

self.i2c = self.Port(I2cTarget(DigitalBidir.empty(), addresses=[0x40]))
self.i2c.sda.init_from(DigitalBidir.from_supply(
self.gnd, self.vs,
voltage_limit_abs=(-0.3, 6.0) * Volt,
input_threshold_factor=(0.3, 0.7)
))
self.i2c.scl.init_from(DigitalSink.from_supply(
self.gnd, self.vs,
voltage_limit_tolerance=(-0.3, 0.3) * Volt,
input_threshold_factor=(0.3, 0.7)
))

self.in_pos = self.Port(AnalogSink(voltage_limits=(-0.3, 26) * Volt))
self.in_neg = self.Port(AnalogSink(voltage_limits=(-0.3, 26) * Volt))

def contents(self):
super().contents()
self.footprint(
'U', 'Package_TO_SOT_SMD:SOT-23-8',
{
'1': self.in_pos,
'2': self.in_neg,
'3': self.gnd,
'4': self.vs,
'5': self.i2c.scl,
'6': self.i2c.sda,
'7': self.gnd, # TODO: make this address selectable, sa0 pin
'8': self.gnd # TODO: make this address selectable, sa1 pin
},
mfr='Texas Instruments', part='INA219AIDCNR',
datasheet='https://www.ti.com/lit/ds/symlink/ina219.pdf'
)
self.assign(self.lcsc_part, "C87469")
Suke0811 marked this conversation as resolved.
Show resolved Hide resolved
self.assign(self.actual_basic_part, False)


class Ina219(CurrentSensor, Block):
"""Current/voltage/power monitor with I2C interface"""

@init_in_parent
def __init__(self, shunt_resistor: RangeLike = 2.0 * mOhm(tol=0.05)):
super().__init__()
self.ic = self.Block(Ina219_Device())
self.pwr = self.Export(self.ic.vs, [Power])
self.gnd = self.Export(self.ic.gnd, [Common])
self.i2c = self.Export(self.ic.i2c)

self.vs_cap = self.Block(DecouplingCapacitor(0.1 * uFarad(tol=0.2))).connected(self.gnd, self.pwr)

self.Rs = self.Block(CurrentSenseResistor(
resistance=shunt_resistor,
))

self.sense_pos = self.Export(self.Rs.pwr_in)
self.sense_neg = self.Export(self.Rs.pwr_out)

def contents(self):
super().contents()
self.connect(self.Rs.sense_in, self.ic.in_pos)
self.connect(self.Rs.sense_out, self.ic.in_neg)
1 change: 1 addition & 0 deletions edg/parts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
from .CanTransceiver_Iso1050 import Iso1050dub
from .CanTransceiver_Sn65hvd230 import Sn65hvd230
from .CurrentSense_Ad8418 import Ad8418a
from .Ina219 import Ina219
from .BatteryProtector_S8261A import S8261A
from .BatteryCharger_Mcp73831 import Mcp73831
from .Distance_Vl53l0x import Vl53l0xBase, Vl53l0x, Vl53l0xConnector, Vl53l0xArray
Expand Down
Loading