Skip to content

STM32 library for the Texas Instruments INA3221 I2C 3-Channel Power Monitor IC

Notifications You must be signed in to change notification settings

vtx22/STM32-INA3221

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

STM32-INA3221

STM32 library for the Texas Instruments INA3221 I2C 3-Channel Power Monitor IC

Usage Example

Simple Code

#include "INA3221.hpp"

// Create INA3221 object with I2C interface and I2C address
INA3221 ina(&hi2c1, 0x40);

// Specify shunt resistance for each channel that you want to use
ina.set_shunt_resistance(INA3221_CHANNEL::CHANNEL_1, 10e-3);  // 10 mOhm shunt resistor for channel 1

// Simple loop to read bus voltage and offset corrected current
while(true)
{
  float bus_voltage = ina.get_bus_voltage(INA3221_CHANNEL::CHANNEL_1);
  float current = ina.get_current_corrected(INA3221_CHANNEL::CHANNEL_1);

  HAL_Delay(100);
}

Build

Copy the INA3221.cpp, INA3221.hpp, INA3221_reg.hpp, I2C.cpp and I2C.hpp to your source and include directory.

To build, specify a build flag for your STM32 MCU. For a STM32F1XXX for example, use -D STM32F1.

In the Cube IDE, paste the flag in the Preprocessor tab in the C/C++ build settings under Project > Properties

API

Constructor

Construct an INA3221 sensor object using the hi2c interface and the I2C address (e.g. 0x40)

INA3221(I2C_HandleTypeDef *hi2c, uint8_t address)

Set Shunt resistors

To specify the shunt resistors for each channel, use

void set_shunt_resistor(INA3221_CHANNEL channel, float resistance);

Set Filter resistors

In some usecases, the INA current sense lines IN+ and IN- are connected to an input filtering network consisting of two filter resitors and one filter capacitor. If such a filter is used, it is necessary to specify the filter resistors. These values are used for current offset correction.

INA3221 input channel filtering

Each channel filter resistance can be set independently. The default value is 0 Ohms.

void set_filter_resistor(INA3221_CHANNEL channel, float resistance);

Bus voltage

The bus voltage is the high side power supply voltage that is connected to the IN+ pin of the given channel. The maximum allowed voltage is 26 V. The measurement resolution is 8 mV.

float INA3221::get_bus_voltage(INA3221_CHANNEL channel);

Shunt voltage

The shunt voltage is the measured voltage drop across the shunt resistor. The shunt voltage resolution is 40 uV.

float INA3221::get_shunt_voltage_raw(INA3221_CHANNEL channel);

The raw shunt voltage can have a relevant offset, especially if filter resistors are used or if the measured current is small. For offset corrected readings, use

float INA3221::get_shunt_voltage_corrected(INA3221_CHANNEL channel);

ℹ️ Getting the corrected shunt voltage takes slightly longer because the bus voltage is read for offset correction.

To get the estimated shunt voltage offset that is used for shunt voltage offset correction, use

float get_shunt_voltage_offset(INA3221_CHANNEL channel);

⚠️ The estimated shunt voltage offset is slightly different from the one used for corrected current calculation.


Current

The current measurement is calculated using the shunt resistance and the measured shunt voltage.

float INA3221::get_current_raw(INA3221_CHANNEL channel);

The raw current can have a relevant offset, especially if filter resistors are used or if the measured current is small. For offset corrected readings, use

float INA3221::get_current_corrected(INA3221_CHANNEL channel);

ℹ️ Getting the corrected current takes slightly longer because the bus voltage is read for offset correction.


Releases

No releases published

Packages

No packages published

Languages