Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/arduino-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v5
- uses: actions/checkout@v6
- uses: arduino/arduino-lint-action@v2
with:
library-manager: update
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/arduino_test_runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ jobs:
runTest:
runs-on: ubuntu-latest
timeout-minutes: 20

steps:
- uses: actions/checkout@v5
- uses: actions/checkout@v6
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/jsoncheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v5
- uses: actions/checkout@v6
- name: json-syntax-check
uses: limitusus/json-syntax-check@v2
with:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.4.1] - 2026-01-10
- update GitHub actions
- minor edits

## [0.4.0] - 2025-08-20
- Fix #30, getKiloWattHour() (thanks to Eerth)
- update GitHub actions
Expand Down
30 changes: 21 additions & 9 deletions INA228.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// FILE: INA228.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.4.0
// VERSION: 0.4.1
// DATE: 2024-05-09
// PURPOSE: Arduino library for the INA228, I2C, 20 bit, voltage, current and power sensor.
// URL: https://github.com/RobTillaart/INA228
Expand Down Expand Up @@ -363,7 +363,8 @@ uint8_t INA228::getAverage()
int INA228::setMaxCurrentShunt(float maxCurrent, float shunt)
{
// Shunt can be really small
if (shunt < 0.0001) return -2; // TODO error code
if (shunt < 0.0001) return -2; // TODO error code
if (maxCurrent < 0.0) return -3; // TODO error code
_maxCurrent = maxCurrent;
_shunt = shunt;
_current_LSB = _maxCurrent * 1.9073486328125e-6; // pow(2, -19);
Expand All @@ -375,8 +376,7 @@ int INA228::setMaxCurrentShunt(float maxCurrent, float shunt)
{
shunt_cal *= 4;
}
// shunt_cal must be written to REGISTER.
// work in progress PR #7
// shunt_cal must be written to its REGISTER.
_writeRegister(INA228_SHUNT_CAL, shunt_cal);

return 0;
Expand Down Expand Up @@ -466,35 +466,47 @@ uint16_t INA228::getDiagnoseAlertBit(uint8_t bit)
//
// THRESHOLD AND LIMIT REGISTERS 12-17
//
// TODO - API ?

// TODO (sync INA228)
// - API ?
// - return bool for setters
// - float voltage interface instead of uint16_t? breaking!
void INA228::setShuntOvervoltageTH(uint16_t threshold)
{
// TODO ADCRANGE DEPENDENT
// Conversion Factor: 5 μV/LSB when ADCRANGE = 0
// 1.25 μV/LSB when ADCRANGE = 1.
// float LSB = 5.0e-6;
// if (_ADCRange == 1) LSB = 1.25e-6;
_writeRegister(INA228_SOVL, threshold);
}

uint16_t INA228::getShuntOvervoltageTH()
{
// TODO ADCRANGE DEPENDENT
// float LSB = 5.0e-6;
// if (_ADCRange == 1) LSB = 1.25e-6;
return _readRegister(INA228_SOVL, 2);
}

void INA228::setShuntUndervoltageTH(uint16_t threshold)
{
// TODO ADCRANGE DEPENDENT
// float LSB = 5.0e-6;
// if (_ADCRange == 1) LSB = 1.25e-6;
_writeRegister(INA228_SUVL, threshold);
}

uint16_t INA228::getShuntUndervoltageTH()
{
// TODO ADCRANGE DEPENDENT
// float LSB = 5.0e-6;
// if (_ADCRange == 1) LSB = 1.25e-6;
return _readRegister(INA228_SUVL, 2);
}

void INA228::setBusOvervoltageTH(uint16_t threshold)
{
if (threshold > 0x7FFF) return;
if (threshold > 0x7FFF) return; // false;
//float LSB = 3.125e-3; // 3.125 mV/LSB.
_writeRegister(INA228_BOVL, threshold);
}
Expand All @@ -520,13 +532,13 @@ uint16_t INA228::getBusUndervoltageTH()

void INA228::setTemperatureOverLimitTH(uint16_t threshold)
{
//float LSB = 7.8125e-3; // milliCelsius
//float LSB = 7.8125e-3; // milli degrees Celsius
_writeRegister(INA228_TEMP_LIMIT, threshold);
}

uint16_t INA228::getTemperatureOverLimitTH()
{
//float LSB = 7.8125e-3; // milliCelsius
//float LSB = 7.8125e-3; // milli degrees Celsius
return _readRegister(INA228_TEMP_LIMIT, 2);
}

Expand Down
14 changes: 7 additions & 7 deletions INA228.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
// FILE: INA228.h
// AUTHOR: Rob Tillaart
// VERSION: 0.4.0
// VERSION: 0.4.1
// DATE: 2024-05-09
// PURPOSE: Arduino library for the INA228, I2C, 20 bit, voltage, current and power sensor.
// URL: https://github.com/RobTillaart/INA228
Expand All @@ -16,7 +16,7 @@
#include "Wire.h"


#define INA228_LIB_VERSION (F("0.4.0"))
#define INA228_LIB_VERSION (F("0.4.1"))


// for setMode() and getMode()
Expand Down Expand Up @@ -116,7 +116,7 @@ class INA228

// raw integer interface.
int32_t getShuntVoltageRAW();

// SHUNT CURRENT
float getCurrent(); // Ampere
float getAmpere() { return getCurrent(); };
Expand Down Expand Up @@ -191,12 +191,12 @@ class INA228
// read datasheet for details. use with care.
// maxCurrent <= 204, (in fact no limit)
// shunt >= 0.0001.
// returns _current_LSB;
// returns error code => 0 == OK;
int setMaxCurrentShunt(float maxCurrent, float shunt);
bool isCalibrated() { return _current_LSB != 0.0; };
bool isCalibrated() { return _current_LSB > 0.0; };
float getMaxCurrent();
float getShunt();
float getCurrentLSB();
float getCurrentLSB(); // <= 0.0 means not calibrated.

//
// SHUNT TEMPERATURE COEFFICIENT REGISTER 3
Expand Down Expand Up @@ -243,7 +243,7 @@ class INA228
// MANUFACTURER and ID REGISTER 3E and 3F
//
// typical value
uint16_t getManufacturer(); // 0x5449
uint16_t getManufacturer(); // 0x5449 ("TI" in ASCII)
uint16_t getDieID(); // 0x0228
uint16_t getRevision(); // 0x0001

Expand Down
2 changes: 1 addition & 1 deletion INA_comparison_table.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Comparison of my INA2xx libraries
|:------------------|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|
| bits | 12 | 16 | 20 | 20 | 16 | 16 | 16 | 16 | 13 |
| Max Voltage | 26 | 36 | 85 | 85 | 48 | 85 | 85 | 36 | 26 |
| Communication | I2C | I2C | I2C | SPI | I2C | SPI | SPI | I2C | I2C |
| Communication | I2C | I2C | I2C | SPI | I2C | I2C | SPI | I2C | I2C |
| Channels | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 3 |
| | | | | | | | | | |
| getBusVoltage | Y | Y | Y | Y | Y | Y | Y | Y | Y |
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024-2025 Rob Tillaart
Copyright (c) 2024-2026 Rob Tillaart

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The INA228 also provides an **ALERT** line, to generate an interrupt
in case a predefined threshold has been met.
This can be an under- or over-voltage, temperature or power limit.
The library does not handle these interrupts.
The alert / limits part of the library's API still needs a redesign.

The library is limited tested and verified with hardware.

Expand Down Expand Up @@ -259,7 +260,7 @@ This value is always positive.

### SHUNT VOLTAGE

- **float getShuntVoltage()** idem, Returns value in volts.
- **float getShuntVoltage()** idem. Returns value in volts.
Note the value can be positive or negative as the INA228 is bidirectional.
- **float getShuntVolt()**
- **float getShuntMilliVolt()**
Expand All @@ -274,10 +275,6 @@ Note this value can be positive or negative as the INA228 is bidirectional.
- **float getMilliAmpere()**
- **float getMicroAmpere()**

### TEMPERATURE

- **float getTemperature()** returns the temperature in Celsius.

### POWER

- **float getPower()** returns the current x BusVoltage in Watt.
Expand All @@ -286,6 +283,10 @@ Note this value can be positive or negative as the INA228 is bidirectional.
- **float getMicroWatt()**
- **float getKiloWatt()**

### TEMPERATURE

- **float getTemperature()** returns the temperature in Celsius.

### ENERGY

See page 13++, page 32, 8.1.2
Expand Down Expand Up @@ -332,7 +333,7 @@ Read datasheet for details, section 7.6.1.1, page 22
- **uint8_t getConversionDelay()** return set value.
- **void setTemperatureCompensation(bool on)** see Shunt temperature coefficient below.
- **bool getTemperatureCompensation()** return set value.
- **bool setADCRange(bool flag)** flag = false => 164 mV, true => 41 mV
- **bool setADCRange(bool flag)** flag = false => ~163.84 mV, true => ~40.96 mV
Since 0.3.1 setADCRange() calls setMaxCurrentShunt() to update the internal LSB values.
- **bool getADCRange()** return set value.

Expand Down Expand Up @@ -389,6 +390,8 @@ Read datasheet for details, section 7.6.1.2, page 22++
| INA228_4120_us | 7 |


### ADC Average

- **bool setAverage(uint8_t avg = INA228_1_SAMPLE)**
- **uint8_t getAverage()** return set value.

Expand Down Expand Up @@ -416,6 +419,7 @@ depends on breakout used, See section above.
The shunt should be 0.0001 Ω and up.
- returns 0 if OK.
- returns -2 if shunt < 0.0001 Ohm. ( Mateksys == 0.0002 Ω )
- returns -3 if maxCurrent < 0.0.
- **bool isCalibrated()** is valid calibration value. The currentLSB > 0.
- **float getMaxCurrent()** return set value.
- **float getShunt()** return set value.
Expand Down Expand Up @@ -504,6 +508,8 @@ might be changed / extended in the future.
- test and verify.
- DiagnoseAlertBit functions
- redo API (0.2.0)
- keep in sync with INA226 where possible.
- keep in sync with INA238 where possible.

#### Should

Expand Down
2 changes: 1 addition & 1 deletion examples/INA228_demo/INA228_demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ INA228 INA(0x40);
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.println();
Serial.println(__FILE__);
Serial.print("INA228_LIB_VERSION: ");
Serial.println(INA228_LIB_VERSION);
Serial.println();
Expand Down
28 changes: 28 additions & 0 deletions examples/INA228_demo_Wire1/.arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:

packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
# - uno
# - due
# - zero
# - leonardo
# - m4
- esp32
# - esp8266
# - mega2560
- rpipico

55 changes: 55 additions & 0 deletions examples/INA228_demo_Wire1/INA228_demo_Wire1.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// FILE: INA228_demo_Wire1.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo core functions
// URL: https://github.com/RobTillaart/INA228


#include "INA228.h"


INA228 INA(0x40, &Wire1);


void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.print("INA228_LIB_VERSION: ");
Serial.println(INA228_LIB_VERSION);
Serial.println();

Wire1.begin();
if (!INA.begin() )
{
Serial.println("Could not connect. Fix and Reboot");
while(1);
}

INA.setMaxCurrentShunt(10, 0.015);

}


void loop()
{
Serial.println("\nVBUS\tVSHUNT\tCURRENT\tPOWER\tTEMP");
for (int i = 0; i < 20; i++)
{
Serial.print(INA.getBusVoltage(), 3);
Serial.print("\t");
Serial.print(INA.getShuntMilliVolt(), 3);
Serial.print("\t");
Serial.print(INA.getMilliAmpere(), 3);
Serial.print("\t");
Serial.print(INA.getMilliWatt(), 3);
Serial.print("\t");
Serial.print(INA.getTemperature(), 3);
Serial.println();
delay(1000);
}
}


// -- END OF FILE --
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ INA228 INA(0x40);
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.println();
Serial.println(__FILE__);
Serial.print("INA228_LIB_VERSION: ");
Serial.println(INA228_LIB_VERSION);
Serial.println();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ INA228 INA2(0x41);

void setup() {
Serial.begin(115200);
Serial.println(__FILE__);
Serial.println();
Serial.println(__FILE__);
Serial.print("INA228_LIB_VERSION: ");
Serial.println(INA228_LIB_VERSION);
Serial.println();
Expand Down
Loading