Introduction

Documentation Status Discord Build Status

CircuitPython driver for the INA219 current sensor.

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.

Usage Example

see example

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

Building locally

To build this library locally you’ll need to install the circuitpython-build-tools package.

python3 -m venv .env
source .env/bin/activate
pip install circuitpython-build-tools

Once installed, make sure you are in the virtual environment:

source .env/bin/activate

Then run the build:

circuitpython-build-bundles --filename_prefix adafruit-circuitpython-ina219 --library_location .

Sphinx documentation

Sphinx is used to build the documentation based on rST files and comments in the code. First, install dependencies (feel free to reuse the virtual environment from above):

python3 -m venv .env
source .env/bin/activate
pip install Sphinx sphinx-rtd-theme

Now, once you have the virtual environment activated:

cd docs
sphinx-build -E -W -b html . _build/html

This will output the documentation to docs/_build/html. Open the index.html in your browser to view them. It will also (due to -W) error out on any warning like Travis will. This is a good way to locally verify it will pass.

Table of Contents

Simple test

Ensure your device works with this simple test.

examples/ina219_simpletest.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Sample code and test for adafruit_in219"""

import time
import board
from adafruit_ina219 import ADCResolution, BusVoltageRange, INA219


i2c_bus = board.I2C()

ina219 = INA219(i2c_bus)

print("ina219 test")

# display some of the advanced field (just to test)
print("Config register:")
print("  bus_voltage_range:    0x%1X" % ina219.bus_voltage_range)
print("  gain:                 0x%1X" % ina219.gain)
print("  bus_adc_resolution:   0x%1X" % ina219.bus_adc_resolution)
print("  shunt_adc_resolution: 0x%1X" % ina219.shunt_adc_resolution)
print("  mode:                 0x%1X" % ina219.mode)
print("")

# optional : change configuration to use 32 samples averaging for both bus voltage and shunt voltage
ina219.bus_adc_resolution = ADCResolution.ADCRES_12BIT_32S
ina219.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_32S
# optional : change voltage range to 16V
ina219.bus_voltage_range = BusVoltageRange.RANGE_16V

# measure and display loop
while True:
    bus_voltage = ina219.bus_voltage        # voltage on V- (load side)
    shunt_voltage = ina219.shunt_voltage    # voltage between V+ and V- across the shunt
    current = ina219.current                # current in mA

    # INA219 measure bus voltage on the load side. So PSU voltage = bus_voltage + shunt_voltage
    print("PSU Voltage:   {:6.3f} V".format(bus_voltage + shunt_voltage))
    print("Shunt Voltage: {:9.6f} V".format(shunt_voltage))
    print("Load Voltage:  {:6.3f} V".format(bus_voltage))
    print("Current:       {:9.6f} A".format(current/1000))
    print("")

    time.sleep(2)

adafruit_ina219

CircuitPython driver for the INA219 current sensor.

  • Author(s): Dean Miller

Implementation Notes

Hardware:

Software and Dependencies:

class adafruit_ina219.ADCResolution[source]

Constants for bus_adc_resolution or shunt_adc_resolution

class adafruit_ina219.BusVoltageRange[source]

Constants for bus_voltage_range

class adafruit_ina219.Gain[source]

Constants for gain

class adafruit_ina219.INA219(i2c_bus, addr=64)[source]

Driver for the INA219 current sensor

bus_voltage

The bus voltage (between V- and GND) in Volts

calibration

Calibration register (cached value)

current

The current through the shunt resistor in milliamps.

power

The power through the load in Watt.

set_calibration_16V_400mA()[source]

Configures to INA219 to be able to measure up to 16V and 400mA of current. Counter overflow occurs at 1.6A.

Note

These calculations assume a 0.1 ohm shunt resistor is present

set_calibration_32V_1A()[source]

Configures to INA219 to be able to measure up to 32V and 1A of current. Counter overflow occurs at 1.3A.

Note

These calculations assume a 0.1 ohm shunt resistor is present

set_calibration_32V_2A()[source]

Configures to INA219 to be able to measure up to 32V and 2A of current. Counter overflow occurs at 3.2A.

..note :: These calculations assume a 0.1 shunt ohm resistor is present

shunt_voltage

The shunt voltage (between V+ and V-) in Volts (so +-.327V)

class adafruit_ina219.Mode[source]

Constants for mode

Indices and tables