Arduino Examples
With the library installed, let's take a closer look at some of the examples included in it. Note, all but one (Example 3) of the examples work for either the INA228 or INA237 so you'll need to comment/uncomment the sensor declaration at the beginning of each example:
// Uncomment the sensor you're using
SfeINA228ArdI2C myINA;
// SfeINA237ArdI2C myINA;
Example 1 - Basic Readings
The first example demonstrates how to set up either the INA228 or INA237 to report current, bus voltage, power and IC temperature. Open "Example01_BasicReadings" from the SparkFun INA2XX library, adjust the sensor definition (if needed) to the correct version and then select the Board and Port. With everything selected click the "Upload" button. Once the code finishes compiling and uploading, open the serial monitor with the baud set to 115200 and you should see some initialization messages print and then the code reports measurements for bus voltage (volts), current (amps), power (watts) and temperature (°C) every 500 ms.
Example 2 - Alert Configuration
The second example shows how to configure and use the Alert pin to trigger on a bus overvoltage or shunt overvoltage/overcurrent threshold that stays active until the alert diagnostic register is read.
The example defaults to use GPIO 2 as the alert pin so adjust this line if you need to use a different pin that is interrupt-capable:
const int alertPin = 2; // Connect the ALERT pin to this GPIO
The code sets the bus overvoltage threshold to 15V and the overvoltage/overcurrent threshold to 5A. You can adjust these settings by changing these lines:
const float busOverVoltage_mV = 14000.0f; // 14 V bus overvoltage
const float shuntOverVoltage_mV = 75.0f; // 75 mV across the 15 mOhm shunt = ~5 A
Example 3 - Power and Energy (INA228 Only)
Example 3 shows how to use the INA228's built-in energy and charge accumulation capability. These features automatically sum energy (in Joules) and charge (in Coulombs) over time. The example initializes the sensor with normal settings and calibration values and then clears the energy and charge accumulator registers to reset any stored values.
After resetting the energy and charge registers, the code prints out values for power (Watts), energy (Joules and Watt/hours), charge (mAh) and the elapsed time. Lastly, if either register overflows, the code will optionally reset the accumulator registers and start over.
Example 5 - Custom Shunt Resistor
Example 5 demonstrates how to configure the INA2XX for a custom shunt resistor value other than the default 15 mΩ resistor. The example defaults to assume the use of a 100 mΩ shunt resistor to give a simple example of how the new resistor value changes the range of current measurement. With a 100 mΩ resistor, current measurement over the default range is ~1.64 A and in the reduced range is ~0.41 A. The code calculates the max current with a 100 mΩ shunt resistor using this formula:
Default range (+/-163.84 mV): max I = 163.84 mV / 100 mOhm = 1.64 A
Reduced range (+/-40.96 mV): max I = 40.96 mV / 100 mOhm = 0.41 A
The code defaults to use the reduced range to provide greater resolution at the cost of a smaller current range. To use the default range, change this line to "false":
const bool REDUCED_ADC_RANGE = true;
When using a different value than the default shunt resistor, it's important to calculate the max current measured on both default and reduced ranges and then set the max current the circuit will draw. Adjust the following line to the max current of your circuit if necessary:
const float MAX_CURRENT_A = 0.4f;
After initializing the sensor and performing calibration, the code prints out the new configuration settings to verify over the serial monitor. If anything looks off, double check the configuration settings and re-upload. After printing out the configuration settings, the code then prints out bus voltage (V), shunt voltage (mV), current (A) and power (W) every 500ms.