It's easy to use the INA3221 with Python or CircuitPython, and the Adafruit_CircuitPython_INA3221 module. This module allows you to easily write Python code to read voltage and current draw from the three channels on the breakout.
You can use this driver with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.
You may find that using DC jacks with terminal blocks to interface between your power supply and load (device) will make interfacing with the INA3221 a little easier.
CircuitPython Microcontroller Wiring
First wire up the breakout to your board exactly as follows. The following is the breakout wired to a Feather RP2040 using the STEMMA connector with a power supply and device (load) connected to channel 1. You can use the same power supply and load wiring for the additional two channels (power supply to + and load to -):
-
Board STEMMA 3V to breakout VIN (red wire)
-
Board STEMMA GND to breakout GND (black wire)
-
Board STEMMA SCL to breakout SCL (yellow wire)
- Board STEMMA SDA to breakout SDA (blue wire)
- Power supply GND to load GND (black wire)
- Power supply positive to breakout 1+ (red wire)
- Load positive to breakout 1- (orange wire)
The following is the breakout wired to a Feather RP2040 using a solderless breadboard:
-
Board 3V to breakout VIN (red wire)
-
Board GND to breakout GND (black wire)
-
Board SCL to breakout SCL (yellow wire)
- Board SDA to breakout SDA (blue wire)
- Power supply GND to load GND (black wire)
- Power supply positive to breakout 1+ (red wire)
- Load positive to breakout 1- (orange wire)
Python Computer Wiring
Since there are dozens of Linux computers/boards you can use, we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported.
Here's the Raspberry Pi wired with I2C using the STEMMA connector with a power supply and device (load) connected to channel 1. You can use the same power supply and load wiring for the additional two channels (power supply to + and load to -):
-
Pi 3V to breakout VIN (red wire)
-
Pi GND to breakout GND (black wire)
-
Pi SCL to breakout SCL (yellow wire)
- Pi SDA to breakout SDA (blue wire)
- Power supply GND to load GND (black wire)
- Power supply positive to breakout 1+ (red wire)
- Load positive to breakout 1- (orange wire)
Here's the Raspberry Pi wired with I2C using a solderless breadboard:
-
Pi 3V to breakout VIN (red wire)
-
Pi GND to breakout GND (black wire)
-
Pi SCL to breakout SCL (yellow wire)
- Pi SDA to breakout SDA (blue wire)
- Power supply GND to load GND (black wire)
- Power supply positive to breakout 1+ (red wire)
- Load positive to breakout 1- (orange wire)
Python Installation of INA3221 Library
You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!
Once that's done, from your command line run the following command:
pip3 install adafruit-circuitpython-ina3221
If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!
CircuitPython Usage
To use with CircuitPython, you need to first install the Adafruit_CircuitPython_INA3221 library, and its dependencies, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following folder and file:
- adafruit_bus_device/
- adafruit_ina3221.mpy
Python Usage
Once you have the library pip3 installed on your computer, copy or download the following example to your computer, and run the following, replacing code.py with whatever you named the file:
python3 code.py
Example Code
If running CircuitPython: Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
If running Python: The console output will appear wherever you are running Python.
# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import time
import board
from adafruit_ina3221 import INA3221
i2c = board.I2C()
ina = INA3221(i2c, enable=[0, 1, 2])
while True:
for i in range(3):
bus_voltage = ina[i].bus_voltage
shunt_voltage = ina[i].shunt_voltage
current = ina[i].current
print(f"Channel {i + 1}:")
print(f" Bus Voltage: {bus_voltage:.6f} V")
print(f" Shunt Voltage: {shunt_voltage:.6f} mV")
print(f" Current: {current:.6f} mA")
print("-" * 30)
time.sleep(2)
The INA3221 is instantiated over I2C. Then in the loop, the voltage and current readings for all three channels are printed to the serial monitor every two seconds. In the screenshot below, a QT Py CH32V203 running a NeoPixel swirl example was connected to channel 1 and a 9V guitar pedal was connected to channel 2. Nothing was connected to channel 3.
Page last edited January 22, 2025
Text editor powered by tinymce.