Skip to content

Commit

Permalink
fix #4, cache ADCrange to improve getShuntVoltage() (#5)
Browse files Browse the repository at this point in the history
- fix #4, cache ADCrange to improve **getShuntVoltage()**
- add INA_comparison_table.md
- minor edits
  • Loading branch information
RobTillaart authored Feb 3, 2025
1 parent 1a2d83d commit 6365d8b
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 32 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.1.1] - 2025-01-30
- fix #4, cache ADCrange to improve **getShuntVoltage()**
- add INA_comparison_table.md
- minor edits


## [0.1.0] - 2024-12-05
- initial version, based upon INA228 stripped

Expand Down
50 changes: 26 additions & 24 deletions INA239.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// FILE: INA239.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.1
// DATE: 2024-12-05
// PURPOSE: Arduino library for the INA239, SPI, 16 bit, voltage, current and power sensor.
// URL: https://github.com/RobTillaart/INA239
Expand Down Expand Up @@ -99,6 +99,8 @@ bool INA239::begin()
digitalWrite(_dataOut, LOW);
digitalWrite(_clock, LOW);
}

getADCRange();
return true;
}

Expand All @@ -107,7 +109,7 @@ bool INA239::begin()
//
// CORE FUNCTIONS
//
// PAGE 22 DONE
// PAGE 22
float INA239::getBusVoltage()
{
// always positive, remove reserved bits.
Expand All @@ -117,12 +119,12 @@ float INA239::getBusVoltage()
return voltage;
}

// PAGE 22 DONE
// PAGE 22
float INA239::getShuntVoltage()
{
// shunt_LSB depends on ADCRANGE in INA239_CONFIG register.
float shunt_LSB = 5e-6; // 5.0 uV/LSB
if (getADCRange() == 1)
if (_ADCRange == true)
{
shunt_LSB = 1.25e-6; // 1.25 uV/LSB
}
Expand All @@ -138,15 +140,15 @@ float INA239::getShuntVoltage()
return voltage;
}

// PAGE 23 + 8.1.2 DONE
// PAGE 23 + 8.1.2
float INA239::getCurrent()
{
int16_t value = _readRegister(INA239_CURRENT, 2);
float current = value * _current_LSB;
return current;
}

// PAGE 23 + 8.1.2 DONE
// PAGE 23 + 8.1.2
float INA239::getPower()
{
uint32_t value = _readRegister(INA239_POWER, 3);
Expand All @@ -167,15 +169,15 @@ float INA239::getTemperature()
//
// CONFIG REGISTER 0
//
// PAGE 20 DONE
// PAGE 20
void INA239::reset()
{
uint16_t value = _readRegister(INA239_CONFIG, 2);
value |= INA239_CFG_RST;
_writeRegister(INA239_CONFIG, value);
}

// PAGE 20 DONE
// PAGE 20
void INA239::setConversionDelay(uint8_t steps)
{
uint16_t value = _readRegister(INA239_CONFIG, 2);
Expand All @@ -190,27 +192,31 @@ uint8_t INA239::getConversionDelay()
return (value >> 6) & 0xFF;
}

// PAGE 20 DONE
// PAGE 20
void INA239::setADCRange(bool flag)
{
// if (flag == _ADCRange) return;
_ADCRange = flag;
uint16_t value = _readRegister(INA239_CONFIG, 2);
value &= ~INA239_CFG_ADCRANGE;
if (flag) value |= INA239_CFG_ADCRANGE;
else value &= ~INA239_CFG_ADCRANGE;
// if value has not changed we do not need to write it back.
_writeRegister(INA239_CONFIG, value);
}

bool INA239::getADCRange()
{
uint16_t value = _readRegister(INA239_CONFIG, 2);
return (value & INA239_CFG_ADCRANGE) > 0;
_ADCRange = (value & INA239_CFG_ADCRANGE) > 0;
return _ADCRange;
}


////////////////////////////////////////////////////////
//
// CONFIG ADC REGISTER 1
//
// PAGE 21 + 22 DONE
// PAGE 21 + 22
bool INA239::setMode(uint8_t mode)
{
if (mode > 0x0F) return false;
Expand Down Expand Up @@ -296,19 +302,19 @@ uint8_t INA239::getAverage()
//
// SHUNT CALIBRATION REGISTER 2
//
// PAGE 28 8.1.2
// PAGE 28 + 8.1.2
int INA239::setMaxCurrentShunt(float maxCurrent, float shunt)
{
// Shunt can be really small
if (shunt < 0.0001) return -2; // error code
if (shunt < 0.0001) return -2; // TODO error code
_maxCurrent = maxCurrent;
_shunt = shunt;
_current_LSB = _maxCurrent * 3.0517578125e-5; // pow(2, -15);

// PAGE 31 (8.1.2)
float shunt_cal = 819.2e6 * _current_LSB * _shunt;
// depends on ADCRANGE in INA239_CONFIG register.
if (getADCRange() == 1)
if (_ADCRange == true)
{
shunt_cal *= 4;
}
Expand Down Expand Up @@ -339,7 +345,7 @@ float INA239::getCurrentLSB()
//
// DIAGNOSE ALERT REGISTER 11
//
// PAGE 23 DONE
// PAGE 23
void INA239::setDiagnoseAlert(uint16_t flags)
{
_writeRegister(INA239_DIAG_ALERT, flags);
Expand Down Expand Up @@ -452,14 +458,14 @@ uint16_t INA239::getTemperatureOverLimitTH()

void INA239::setPowerOverLimitTH(uint16_t threshold)
{
// P 26
// PAGE 26
// Conversion factor: 256 × Power LSB.
_writeRegister(INA239_POWER_LIMIT, threshold);
}

uint16_t INA239::getPowerOverLimitTH()
{
// P 26
// PAGE 26
// Conversion factor: 256 × Power LSB.
return _readRegister(INA239_POWER_LIMIT, 2);
}
Expand All @@ -469,6 +475,7 @@ uint16_t INA239::getPowerOverLimitTH()
//
// MANUFACTURER and ID REGISTER 3E/3F
//
// PAGE 26
uint16_t INA239::getManufacturer()
{
uint16_t value = _readRegister(INA239_MANUFACTURER, 2);
Expand Down Expand Up @@ -514,7 +521,7 @@ bool INA239::usesHWSPI()

////////////////////////////////////////////////////////
//
// SHOULD BE PROTECTED
// PRIVATE
//
uint32_t INA239::_readRegister(uint8_t reg, uint8_t bytes) // bytes = 2 or 3.
{
Expand Down Expand Up @@ -576,11 +583,6 @@ uint16_t INA239::_writeRegister(uint8_t reg, uint16_t value)
}


/////////////////////////////////////////////////////////////////////////////
//
// PROTECTED
//

uint8_t INA239::swSPI_transfer(uint8_t value)
{
uint8_t rv = 0;
Expand Down
7 changes: 4 additions & 3 deletions INA239.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
// FILE: INA239.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.1
// DATE: 2024-12-05
// PURPOSE: Arduino library for the INA239, SPI, 16 bit, voltage, current and power sensor.
// URL: https://github.com/RobTillaart/INA239
Expand All @@ -13,7 +13,7 @@
#include "SPI.h"


#define INA239_LIB_VERSION (F("0.1.0"))
#define INA239_LIB_VERSION (F("0.1.1"))


#ifndef __SPI_CLASS__
Expand Down Expand Up @@ -231,14 +231,15 @@ class INA239
bool usesHWSPI();


protected:
private:
// max 4 bytes
uint32_t _readRegister(uint8_t reg, uint8_t bytes);
uint16_t _writeRegister(uint8_t reg, uint16_t value);

float _current_LSB;
float _shunt;
float _maxCurrent;
bool _ADCRange;

uint8_t _dataIn = 255;
uint8_t _dataOut= 255;
Expand Down
22 changes: 22 additions & 0 deletions INA_comparison_table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

## Comparison table INA sensors

Comparison of my INA2xx libraries


| | 219 | 226 | 228 | 229 | 236 | 239 | 3221 |
|:------------------|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|
| bits | 12 | 16 | 20 | 20 | 16 | 16 | 13 |
| Max Voltage | 26 | 36 | 85 | 85 | 48 | 85 | 26 |
| Communication | I2C | I2C | I2C | SPI | I2C | SPI | I2C |
| Channels | 1 | 1 | 1 | 1 | 1 | 1 | 3 |
| | | | | | | | |
| getBusVoltage | Y | Y | Y | Y | Y | Y | Y |
| getShuntVoltage | Y | Y | Y | Y | Y | Y | Y |
| getCurrent | Y | Y | Y | Y | Y | Y | Y |
| getPower | Y | Y | Y | Y | Y | Y | Y |
| getTemperature | - | - | Y | Y | - | Y | - |
| getEnergy | - | - | Y | Y | - | - | - |
| getCharge | - | - | Y | Y | - | - | - |


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-2024 Rob Tillaart
Copyright (c) 2024-2025 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ works up to 85 Volts, which is more than twice the 36 volt of the INA226.
The INA239 has a build in temperature sensor (±1°C) to be used for
monitoring and temperature compensation.

The INA239 also provides an alert line, to generate an interrupt
The INA239 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.
Expand Down
2 changes: 1 addition & 1 deletion examples/INA239_demo/INA239_demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "INA239.h"

// select, dataIn, dataOut, clock == SOFTWARE SPI
// INA239 INA(5, 6, 7, &SPI);
// INA239 INA(5, 6, 7, 8);

// select, &SPI === HW SPI
INA239 INA(5, &SPI);
Expand Down
2 changes: 2 additions & 0 deletions examples/INA239_performance/performance_0.1.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ BOARD: Arduino UNO
IDE: 1.8.19


INA239_LIB_VERSION: 0.1.0


========================================================
Speed: 1000000
Expand Down
48 changes: 48 additions & 0 deletions examples/INA239_performance/performance_0.1.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
BOARD: Arduino UNO
IDE: 1.8.19


INA239_LIB_VERSION: 0.1.1


========================================================
Speed: 1000000

test_core
getBusVoltage: 60
getShuntVoltage: 60
getCurrent: 56
getPower: 80
getTemperature: 56

========================================================
Speed: 2000000

test_core
getBusVoltage: 48
getShuntVoltage: 48
getCurrent: 40
getPower: 64
getTemperature: 48

========================================================
Speed: 4000000

test_core
getBusVoltage: 44
getShuntVoltage: 44
getCurrent: 40
getPower: 64
getTemperature: 44

========================================================
Speed: 8000000

test_core
getBusVoltage: 44
getShuntVoltage: 40
getCurrent: 32
getPower: 52
getTemperature: 40

Done
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/INA239.git"
},
"version": "0.1.0",
"version": "0.1.1",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=INA239
version=0.1.0
version=0.1.1
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for the INA239, SPI, 16 bit, voltage, current and power sensor.
Expand Down

0 comments on commit 6365d8b

Please sign in to comment.