INA236 HAL Based Library 1.0.1
This library is a software library that works with the INA236 current, voltage, and power monitor chip. This library provides a convenient and efficient way to access the I2C interfaces of the chip, allowing developers to easily integrate this power meter into their systems.
Loading...
Searching...
No Matches
README

Doxygen Action pages-build-deployment

INA236 HAL Based Library

The INA236 device is a 16-bit digital current monitor with an I2C/SMBus-compatible interface that is compliant with a wide range of digital bus voltages such as 1.2 V, 1.8 V, 3.3 V, and 5.0 V. The device monitors the voltage across an external sense resistor and reports values for current, bus voltage, and power. (Click for more info)

This library is a software library that works with the INA236 current, voltage, and power monitor chip. This library provides a convenient and efficient way to access the I2C interfaces of the chip, allowing developers to easily integrate this power meter into their systems.

The library is designed to be easy to use and provides a simple, intuitive API for accessing the I2C interfaces of the INA236. It includes a range of functions for performing common I2C operations, such as sending and receiving data, querying the status of the chip, reading the measured parameters, and configuring the INA236 settings.

With this library, developers can quickly and easily integrate the INA236 into their systems, enabling them to take full advantage of the chip's capabilities.

Key Features

  • Easy-to-use API for accessing the I2C interfaces of the INA236
  • Support for common I2C operations, such as sending and receiving data, querying the status of the chip, reading the measured parameters, and configuring the INA236 settings
  • Full feature library

Documentations

The full documents are available here

Schematic Symbol and Footprint

Footprint and schematic symbols are available in my Altium library.

Donate

Is it helpfull?

Buy me a Coffee

Getting Started

Quick Start

  1. Download the library source from the latest release
  2. Copy ina236.c and ina236.h file to your project directory and add them to your IDE if necessary.
  3. Inclued the library into your project:
    #include "ina236.h"
  4. Create an object (instanse) from INA236 struct with desired name:
    INA236 ina236;
    Class (struct) that stores variables for interacting with INA236.
    Definition ina236.h:81
  5. Initialize the chip:
    INA236_Status INA236_init(INA236 *self, uint8_t I2C_ADDR, I2C_HandleTypeDef *hi2c, float ShuntResistor, ADCRange adc_range, NumSamples numer_of_adc_samples, ConvTime vbus_conversion_time, ConvTime vshunt_conversion_time, Mode mode)
    Initialize the INA236 with the given config.
    Definition ina236.c:101
    @ MODE_CONTINUOUS_BOTH_SHUNT_BUS
    Definition ina236.h:69
    @ CTIME_1100us
    Definition ina236.h:68
    @ CTIME_140us
    Definition ina236.h:68
    @ RANGE_20_48mV
    Definition ina236.h:66
    @ NADC_16
    Definition ina236.h:67
    Each argument is described on the doc page.
  6. Now you can call INA236_readAll function to read the meassured data:
    INA236_readAll(&ina236);
    shunt_voltage = ina236.ShuntVoltage;
    bus_voltage = ina236.BusVoltage;
    current = ina236.Current;
    power = ina236.Power;
    void INA236_readAll(INA236 *self)
    Read all of the measured values: Shunt voltage, bus voltage, power, and current. Then store the value...
    Definition ina236.c:538
    float BusVoltage
    Definition ina236.h:103
    float ShuntVoltage
    Definition ina236.h:102
    float Current
    Definition ina236.h:105
    float Power
    Definition ina236.h:104

Here is the whole code:

#include "ina236.h"
INA236 ina236;
float shunt_voltage, bus_voltage, current, power;
INA236_readAll(&ina236);
shunt_voltage = ina236.ShuntVoltage;
bus_voltage = ina236.BusVoltage;
current = ina236.Current;
power = ina236.Power;
}

If you want to use UART or virtual USB COM port on youe microcontroller, it is recommended to use this print function:

// Print setting -------------------
#define DEBUG_ENABLE 1
#define USB_DEBUG 0
#define DEBUG_UART (&huart1)
// ---------------------------------
#if DEBUG_ENABLE
#include "stdarg.h"
#include "string.h"
#include "stdlib.h"
#if USB_DEBUG
#include "usbd_cdc_if.h"
#endif
#endif
void DEBUG(const char* _str, ...){
#if DEBUG_ENABLE
va_list args;
va_start(args, _str);
char buffer[150];
memset(buffer, 0, 150);
int buffer_size = vsprintf(buffer, _str, args);
#if USB_DEBUG
CDC_Transmit_FS((uint8_t*) buffer, buffer_size);
#else
HAL_UART_Transmit(DEBUG_UART, (uint8_t*)buffer, buffer_size, 5000);
#endif
#endif
}

By applying the above trick, you can simply use this one to see the variables on the serial terminal:

#include "ina236.h"
INA236 ina236;
INA236_readAll(&ina236);
DEBUG("Shunt Voltage: %.3fmV \t Bus Voltage: %.2fV \t Current: %.2fA \t Power: %.2fW\r\n", ina236.ShuntVoltage, ina236.BusVoltage, ina236.Current, ina236.Power);
}
else{
DEBUG("----- INA236 init failed -----\r\n");
}

Advanced Options

Using Alert

INA236 can assert an alert on several situations like convertion ready, over power, over current, bus over voltage, bus under voltage, etc. To initialize alert functionality, use INA236_alert_init function:

INA236_Status INA236_alert_init(INA236 *self, AlertOn alert_on, AlertPolarity alert_polarity, AlertLatch alert_latch, AlertConvReady alert_conv_ready, float alert_limit)
Initialize the alert functionality of INA236 with the given configurations.
Definition ina236.c:176
@ ALERT_SHUNT_OVER_LIMIT
Definition ina236.h:71
@ ALERT_TRANSPARENT
Definition ina236.h:73
@ ALERT_ACTIVE_LOW
Definition ina236.h:72
@ ALERT_CONV_DISABLE
Definition ina236.h:74

Each argument is described on the doc page.

** NOTE1 ** If you choose ALERT_LATCHED for alert latch mode, you have to reset the alert pin by calling INA236_resetAlert function after each alert assertion. (see more)

** NOTE2 ** If you enabled convertion ready alert as well as limit reach functions (like shunt over voltage etc), you have to distinguish the alert source bt calling INA236_getAlertSource function. (see more)

** NOTE3 ** The alert pin is open-drain. So don not forget to add a pull-up resistor on this pin.

Read Parameters Individually

You can read each parameter individually instead of INA236_readAll by calling each of these functions:

  • INA236_getShuntVoltage(&ina236); to read shunt voltage (in mV)
  • INA236_getBusVoltage(&ina236); to read bus voltage (in V)
  • INA236_getPower(&ina236); to read power (in W)
  • INA236_getCurrent(&ina236); to read current (in A)

Example:

#include "ina236.h"
INA236 ina236;
float shunt_voltage, bus_voltage, current, power;
shunt_voltage = INA236_getShuntVoltage(&ina236);
bus_voltage = INA236_getBusVoltage(&ina236);
current = INA236_getCurrent(&ina236);;
power = INA236_getPower(&ina236);;
}
float INA236_getCurrent(INA236 *self)
Read the current from INA236.
Definition ina236.c:551
float INA236_getPower(INA236 *self)
Read the power from INA236.
Definition ina236.c:587
float INA236_getBusVoltage(INA236 *self)
Read the bus voltage from INA236.
Definition ina236.c:563
float INA236_getShuntVoltage(INA236 *self)
Read the shunt voltage from INA236.
Definition ina236.c:575

Soft Reset

You can send a reset command to all of the INA236 chips on the same bus by calling INA236_SoftResetAll function. (see more)

Change Settings On The Fly

You can change each of the configurations on the fly using these functions:

  • INA236_setADCRange to change the ADC full scale range (see more)
  • INA236_setNumberOfADCSamples to change the number of averaging ADC samples (see more)
  • INA236_setVBusConversionTime to change the conversion period of VBus (see more)
  • INA236_setVShuntConversionTime to change the conversion period of VBus (see more)
  • INA236_setMode to change the operating mode (see more)

Getting Manufacturer and Device ID

If you want to get the manufacturer or device ID, you can use these functions:

For example:

printf("Manufacturer ID is 0x%4X \r\n", INA236_getManID(&ina236));
printf(" Device ID is 0x%3X \r\n", INA236_getDevID(&ina236));
uint16_t INA236_getDevID(INA236 *self)
Get the device ID.
Definition ina236.c:527
uint16_t INA236_getManID(INA236 *self)
Get the manufacturer ID.
Definition ina236.c:516

Get Internal Errors

INA236 can also give the state of internal modules like CPU and memory. By calling INA236_getErrors function you can see if there is any error or not. (see more)