MojoScale Studio Docs
API Reference

ina219

INA219 current, voltage, and power monitor module.

Studio Docs Sensors & I/O

import ina219

The ina219 module reads TI INA219-compatible high-side current monitors over I2C. It handles the calibration register for common breakout boards and returns bus voltage, shunt voltage, current, and power in practical units.

Quick example

import ina219

monitor = ina219.INA219(sda=21, scl=22, address=0x40)

reading = monitor.read()
print(reading["bus_voltage_v"])
print(reading["current_ma"])
print(reading["power_mw"])

monitor.calibrate(shunt_ohms=0.1, max_current_a=2.0)

importina219

INA219 current, voltage, and power monitor module.

The ina219 module reads TI INA219-compatible high-side current monitors over I2C. It handles the calibration register for common breakout boards and returns bus voltage, shunt voltage, current, and power in practical units.

API 26 available

ina219.INA219(sda: int, scl: int, address: int = None, frequency: int = None, port: int = None, shunt_ohms: float = None, max_current_a: float = None) -> ina219_sensor

Create and initialize an independent INA219 current monitor.

Use one INA219 object for each physical monitor. Multiple monitors can share one I2C bus when each board has a unique address.

Parameters
Name Type Pass as Required Description
sda int positional or keyword Yes GPIO pin used for I2C SDA.
scl int positional or keyword Yes GPIO pin used for I2C SCL.
address int positional or keyword No Optional I2C address. Defaults to 0x40.
frequency int positional or keyword No Optional I2C bus speed in hertz. Defaults to 100000.
port int positional or keyword No Optional ESP-IDF I2C port number. Defaults to 0.
shunt_ohms float positional or keyword No Optional shunt resistor value in ohms. Defaults to 0.1.
max_current_a float positional or keyword No Optional expected full-scale current in amps. Defaults to 3.2.
Returns

ina219_sensor ina219_sensor object.

ina219.INA219().configure(voltage_range=None, gain_mv=None, bus_adc=None, shunt_adc=None, mode=None, shunt_ohms=None, max_current_a=None) -> bool

Configure ADC mode, voltage range, gain, measurement mode, and calibration values.

Parameters
Name Type Pass as Required Description
voltage_range int positional or keyword No Optional value. Defaults to None.
gain_mv int positional or keyword No Optional value. Defaults to None.
bus_adc str positional or keyword No Optional value. Defaults to None.
shunt_adc str positional or keyword No Optional value. Defaults to None.
mode str positional or keyword No Optional value. Defaults to None.
shunt_ohms float positional or keyword No Optional value. Defaults to None.
max_current_a float positional or keyword No Optional value. Defaults to None.

ina219.INA219().calibrate(shunt_ohms=None, max_current_a=None) -> bool

Recalculate and write this monitor's calibration register.

Parameters
Name Type Pass as Required Description
shunt_ohms float positional or keyword No Optional value. Defaults to None.
max_current_a float positional or keyword No Optional value. Defaults to None.

ina219.INA219().read() -> dict

Read bus voltage, shunt voltage, current, and power.

ina219.INA219().bus_voltage() -> float

Read bus voltage in volts.

ina219.INA219().shunt_voltage() -> float

Read shunt voltage in volts.

ina219.INA219().current() -> float

Read current in amps.

ina219.INA219().power() -> float

Read power in watts.

ina219.INA219().sleep() -> bool

Put this monitor into power-down mode.

ina219.INA219().wake() -> bool

Restore this monitor's active configuration after sleep.

ina219.INA219().reset() -> bool

Reset this monitor and restore calibration/configuration.

ina219.INA219().info() -> dict

Return I2C settings, calibration, and scaling info for this monitor.

ina219.INA219().close() -> bool

Release this INA219 handle.

ina219.INA219().Example

monitor = ina219.INA219(sda=21, scl=22) print(monitor.current())

ina219.begin(sda: int, scl: int, address: int = None, frequency: int = None, port: int = None, shunt_ohms: float = None, max_current_a: float = None) -> bool

Legacy: initialize the module-level default INA219 sensor.

Prefer ``monitor = ina219.INA219(...)`` for new projects. The legacy wrapper keeps older singleton-style scripts working. The default calibration assumes a common 0.1 ohm shunt resistor and up to 3.2 A full-scale current. Use ``calibrate()`` or the optional calibration arguments when your breakout uses a different shunt.

Parameters
Name Type Pass as Required Description
sda int positional or keyword Yes GPIO pin used for I2C SDA.
scl int positional or keyword Yes GPIO pin used for I2C SCL.
address int positional or keyword No Optional I2C address. Defaults to 0x40.
frequency int positional or keyword No Optional I2C bus speed in hertz. Defaults to 100000.
port int positional or keyword No Optional ESP-IDF I2C port number. Defaults to 0.
shunt_ohms float positional or keyword No Optional shunt resistor value in ohms. Defaults to 0.1.
max_current_a float positional or keyword No Optional expected full-scale current in amps. Defaults to 3.2.
Returns

bool True when the sensor acknowledges reset, calibration, and config writes.

ina219.configure(voltage_range: int = None, gain_mv: int = None, bus_adc: str = None, shunt_adc: str = None, mode: str = None, shunt_ohms: float = None, max_current_a: float = None) -> bool

Configure INA219 ADC and measurement range settings.

Parameters
Name Type Pass as Required Description
voltage_range int positional or keyword No Optional bus voltage range. Use 16 or 32. Defaults to 32.
gain_mv int positional or keyword No Optional shunt voltage gain range in millivolts. Use 40, 80, 160, or 320. Defaults to 320.
bus_adc str positional or keyword No Optional bus ADC mode. Use "9bit", "10bit", "11bit", "12bit", or averaged modes such as "12bit_8" and "12bit_128". Defaults to "12bit".
shunt_adc str positional or keyword No Optional shunt ADC mode. Uses the same names as bus_adc. Defaults to "12bit".
mode str positional or keyword No Optional measurement mode. Use "continuous", "triggered", "shunt_continuous", "bus_continuous", "shunt_triggered", "bus_triggered", "power_down", or "adc_off". Defaults to "continuous".
shunt_ohms float positional or keyword No Optional shunt resistor value in ohms.
max_current_a float positional or keyword No Optional expected full-scale current in amps.
Returns

bool True when the calibration and config registers are written.

ina219.calibrate(shunt_ohms: float = None, max_current_a: float = None) -> bool

Recalculate and write the INA219 calibration register.

Parameters
Name Type Pass as Required Description
shunt_ohms float positional or keyword No Optional shunt resistor value in ohms. Defaults to the current module setting.
max_current_a float positional or keyword No Optional expected full-scale current in amps. Defaults to the current module setting.
Returns

bool True when the calibration register is valid and written.

ina219.read() -> dict

Read voltage, current, and power.

Returns

dict Dict with ``ok``, ``bus_voltage_v``, ``shunt_voltage_v``, ``shunt_voltage_mv``, ``current_a``, ``current_ma``, ``power_w``, ``power_mw``, raw register values, ``conversion_ready``, ``overflow``, ``calibration``, ``shunt_ohms``, ``current_lsb_a``, and ``power_lsb_w`` fields.

ina219.bus_voltage() -> float

Read bus voltage.

Returns

float Float bus voltage in volts.

ina219.shunt_voltage() -> float

Read shunt voltage.

Returns

float Float shunt voltage in volts.

ina219.current() -> float

Read current.

Returns

float Float current in amps.

ina219.power() -> float

Read power.

Returns

float Float power in watts.

ina219.sleep() -> bool

Put the INA219 into power-down mode.

Returns

bool True when the power-down config is written.

ina219.wake() -> bool

Restore the active INA219 config after sleep.

Returns

bool True when calibration and config are written.

ina219.reset() -> bool

Reset the INA219 and restore calibration/config.

Returns

bool True when reset and reconfiguration succeed.

ina219.info() -> dict

Return INA219 module state.

Returns

dict Dict with initialization state, I2C settings, voltage/gain/ADC config, mode, calibration value, shunt resistor, max current, and scaling.