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.
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
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_sensorCreate 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.
| 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. |
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
ina219.INA219().configure(voltage_range=None, gain_mv=None, bus_adc=None, shunt_adc=None, mode=None, shunt_ohms=None, max_current_a=None) -> boolConfigure ADC mode, voltage range, gain, measurement mode, and calibration values.
| 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
ina219.INA219().calibrate(shunt_ohms=None, max_current_a=None) -> boolRecalculate and write this monitor's calibration register.
| 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
ina219.INA219().read() -> dictRead bus voltage, shunt voltage, current, and power.
ina219.INA219().bus_voltage() -> float
ina219.INA219().bus_voltage() -> floatRead bus voltage in volts.
ina219.INA219().shunt_voltage() -> float
ina219.INA219().shunt_voltage() -> floatRead shunt voltage in volts.
ina219.INA219().current() -> float
ina219.INA219().current() -> floatRead current in amps.
ina219.INA219().power() -> float
ina219.INA219().power() -> floatRead power in watts.
ina219.INA219().sleep() -> bool
ina219.INA219().sleep() -> boolPut this monitor into power-down mode.
ina219.INA219().wake() -> bool
ina219.INA219().wake() -> boolRestore this monitor's active configuration after sleep.
ina219.INA219().reset() -> bool
ina219.INA219().reset() -> boolReset this monitor and restore calibration/configuration.
ina219.INA219().info() -> dict
ina219.INA219().info() -> dictReturn I2C settings, calibration, and scaling info for this monitor.
ina219.INA219().close() -> bool
ina219.INA219().close() -> boolRelease this INA219 handle.
ina219.INA219().Example
ina219.INA219().Examplemonitor = 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
ina219.begin(sda: int, scl: int, address: int = None, frequency: int = None, port: int = None, shunt_ohms: float = None, max_current_a: float = None) -> boolLegacy: 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.
| 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. |
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
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) -> boolConfigure INA219 ADC and measurement range settings.
| 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. |
bool True when the calibration and config registers are written.
ina219.calibrate(shunt_ohms: float = None, max_current_a: float = None) -> bool
ina219.calibrate(shunt_ohms: float = None, max_current_a: float = None) -> boolRecalculate and write the INA219 calibration register.
| 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. |
bool True when the calibration register is valid and written.
ina219.read() -> dict
ina219.read() -> dictRead voltage, current, and power.
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
ina219.bus_voltage() -> floatRead bus voltage.
float Float bus voltage in volts.
ina219.shunt_voltage() -> float
ina219.shunt_voltage() -> floatRead shunt voltage.
float Float shunt voltage in volts.
ina219.current() -> float
ina219.current() -> floatRead current.
float Float current in amps.
ina219.power() -> float
ina219.power() -> floatRead power.
float Float power in watts.
ina219.sleep() -> bool
ina219.sleep() -> boolPut the INA219 into power-down mode.
bool True when the power-down config is written.
ina219.wake() -> bool
ina219.wake() -> boolRestore the active INA219 config after sleep.
bool True when calibration and config are written.
ina219.reset() -> bool
ina219.reset() -> boolReset the INA219 and restore calibration/config.
bool True when reset and reconfiguration succeed.
ina219.info() -> dict
ina219.info() -> dictReturn INA219 module state.
dict Dict with initialization state, I2C settings, voltage/gain/ADC config, mode, calibration value, shunt resistor, max current, and scaling.