Skip to content

Commit c4828d0

Browse files
committed
0.1.1 INA236
1 parent b74bd66 commit c4828d0

File tree

9 files changed

+106
-72
lines changed

9 files changed

+106
-72
lines changed

libraries/INA236/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.1.1] - 2025-01-30
10+
- fix #2, cache ADCRange to improve getShuntVoltage()
11+
- add **INA_comparison_table.md**
12+
- align INA228/INA229
13+
- minor edits
14+
915
## [0.1.0] - 2024-05-27
1016
- initial version ported from INA226 to INA236 by Josef Tremmel
1117

libraries/INA236/INA236.cpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// FILE: INA236.cpp
22
// AUTHOR: Rob Tillaart
33
// ported from INA226 to INA236 by Josef Tremmel
4-
// VERSION: 0.1.0
4+
// VERSION: 0.1.1
55
// DATE: 2024-05-27
6-
// PURPOSE: Arduino library for INA236 power sensor
6+
// PURPOSE: Arduino library for the INA236, I2C, 16 bit, voltage, current and power sensor.
77
// URL: https://github.com/RobTillaart/INA236
88
//
99
// Read the datasheet for the details
@@ -46,13 +46,14 @@ INA236::INA236(const uint8_t address, TwoWire *wire)
4646
_current_LSB = 0;
4747
_maxCurrent = 0;
4848
_shunt = 0;
49-
_voltage_LSB = 2.5e-6; // default value of ADC range (80 mV)
5049
}
5150

5251

5352
bool INA236::begin()
5453
{
5554
if (! isConnected()) return false;
55+
56+
getADCRange();
5657
return true;
5758
}
5859

@@ -76,15 +77,24 @@ uint8_t INA236::getAddress()
7677
//
7778
float INA236::getBusVoltage()
7879
{
79-
uint16_t val = _readRegister(INA236_BUS_VOLTAGE);
80-
return val * 1.6e-3; // 1.6 mV/LSB fixed
80+
uint16_t value = _readRegister(INA236_BUS_VOLTAGE);
81+
float bus_LSB = 1.6e-3; // 1.6 mV/LSB fixed
82+
float voltage = value * bus_LSB;
83+
return voltage;
8184
}
8285

8386

8487
float INA236::getShuntVoltage()
8588
{
86-
int16_t val = _readRegister(INA236_SHUNT_VOLTAGE);
87-
return val * _voltage_LSB; // voltage LSB depends on range
89+
// shunt_LSB depends on ADCRANGE in INA236_CONFIG register.
90+
float shunt_LSB = 2.5e-6; // 2.5 mV
91+
if (_ADCRange == true)
92+
{
93+
shunt_LSB = 0.625e-6; // 625 nV
94+
}
95+
int16_t value = _readRegister(INA236_SHUNT_VOLTAGE);
96+
float voltage = value * shunt_LSB;
97+
return voltage;
8898
}
8999

90100

@@ -135,37 +145,27 @@ bool INA236::reset()
135145
_current_LSB = 0;
136146
_maxCurrent = 0;
137147
_shunt = 0;
138-
_voltage_LSB = 2.5e-6;
139148
return true;
140149
}
141150

142151

143-
bool INA236::setADCRange(uint8_t adcRange)
152+
bool INA236::setADCRange(bool flag)
144153
{
145-
if (adcRange > 1) return false;
154+
_ADCRange = flag;
146155
uint16_t mask = _readRegister(INA236_CONFIGURATION);
147-
mask &= ~INA236_CONF_ADCRANGE_MASK;
148-
mask |= (adcRange << 12);
156+
if (flag) mask |= INA236_CONF_ADCRANGE_MASK;
157+
else mask &= ~INA236_CONF_ADCRANGE_MASK;
158+
// if mask has not changed we do not need to write it back.
149159
_writeRegister(INA236_CONFIGURATION, mask);
150-
// adjust voltage / LSB
151-
if (adcRange == 1) // 20 mV
152-
{
153-
_voltage_LSB = 0.625e-6; // factor 4 smaller
154-
}
155-
else // 80 mV
156-
{
157-
_voltage_LSB = 2.5e-6 ;
158-
}
159160
return true;
160161
}
161162

162163

163164
uint8_t INA236::getADCRange()
164165
{
165166
uint16_t mask = _readRegister(INA236_CONFIGURATION);
166-
mask &= INA236_CONF_ADCRANGE_MASK;
167-
mask >>= 12;
168-
return mask;
167+
_ADCRange = (mask & INA236_CONF_ADCRANGE_MASK) > 0;
168+
return _ADCRange;
169169
}
170170

171171

@@ -269,16 +269,16 @@ int INA236::setMaxCurrentShunt(float maxCurrent, float shunt, bool normalize)
269269
return INA236_ERR_SHUNT_LOW;
270270
}
271271

272-
int adcRange;
272+
bool adcRange = false;
273273
int adcRangeFactor;
274274
if (shuntVoltage <= 0.020) // 20 mV
275275
{
276-
adcRange = 1;
276+
adcRange = true;
277277
adcRangeFactor = 4;
278278
}
279279
else if (shuntVoltage <= 0.080) // 80 mV
280280
{
281-
adcRange = 0;
281+
adcRange = false;
282282
adcRangeFactor = 1;
283283
}
284284
setADCRange(adcRange);

libraries/INA236/INA236.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// FILE: INA236.h
33
// AUTHOR: Rob Tillaart
44
// ported from INA226 to INA236 by Josef Tremmel
5-
// VERSION: 0.1.0
5+
// VERSION: 0.1.1
66
// DATE: 2024-05-27
7-
// PURPOSE: Arduino library for INA236 current and power sensor.
7+
// PURPOSE: Arduino library for the INA236, I2C, 16 bit, voltage, current and power sensor.
88
// URL: https://github.com/RobTillaart/INA236
99
//
1010
// Read the datasheet for the details
@@ -14,7 +14,7 @@
1414
#include "Wire.h"
1515

1616

17-
#define INA236_LIB_VERSION "0.1.0"
17+
#define INA236_LIB_VERSION "0.1.1"
1818

1919

2020
// set by setAlertRegister
@@ -114,7 +114,8 @@ class INA236
114114
uint8_t getBusVoltageConversionTime();
115115
bool setShuntVoltageConversionTime(uint8_t svct = INA236_1100_us);
116116
uint8_t getShuntVoltageConversionTime();
117-
bool setADCRange(uint8_t adcRange);
117+
// flag = false => 80 mV, true => 20 mV
118+
bool setADCRange(bool flag);
118119
uint8_t getADCRange();
119120

120121

@@ -175,7 +176,7 @@ class INA236
175176
float _current_LSB;
176177
float _shunt;
177178
float _maxCurrent;
178-
float _voltage_LSB;
179+
bool _ADCRange;
179180

180181
uint8_t _address;
181182
TwoWire * _wire;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
## Comparison table INA sensors
3+
4+
Comparison of my INA2xx libraries
5+
6+
7+
| | 219 | 226 | 228 | 229 | 236 | 239 | 3221 |
8+
|:------------------|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|
9+
| bits | 12 | 16 | 20 | 20 | 16 | 16 | 13 |
10+
| Max Voltage | 26 | 36 | 85 | 85 | 48 | 85 | 26 |
11+
| Communication | I2C | I2C | I2C | SPI | I2C | SPI | I2C |
12+
| Channels | 1 | 1 | 1 | 1 | 1 | 1 | 3 |
13+
| | | | | | | | |
14+
| getBusVoltage | Y | Y | Y | Y | Y | Y | Y |
15+
| getShuntVoltage | Y | Y | Y | Y | Y | Y | Y |
16+
| getCurrent | Y | Y | Y | Y | Y | Y | Y |
17+
| getPower | Y | Y | Y | Y | Y | Y | Y |
18+
| getTemperature | - | - | Y | Y | - | Y | - |
19+
| getEnergy | - | - | Y | Y | - | - | - |
20+
| getCharge | - | - | Y | Y | - | - | - |
21+
22+

libraries/INA236/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024-2024 Rob Tillaart
3+
Copyright (c) 2024-2025 Rob Tillaart
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

libraries/INA236/README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
# INA236
1313

14-
Arduino library for INA236 current and power sensor
14+
Arduino library for the INA236, I2C, 16 bit, voltage, current and power sensor.
1515

1616

1717
## Description
@@ -48,9 +48,15 @@ A few important maxima, see datasheet, chapter 6.
4848
- https://www.ti.com/product/INA236#tech-docs
4949
- https://www.ti.com/product/INA236#params
5050
- https://www.ti.com/document-viewer/INA236/datasheet
51-
- https://github.com/RobTillaart/INA219
52-
- https://github.com/RobTillaart/INA226
53-
- https://github.com/RobTillaart/INA236
51+
- https://github.com/RobTillaart/INA219 26 Volt, I2C, 12 bit
52+
- https://github.com/RobTillaart/INA226 36 Volt, I2C, 16 bit
53+
- https://github.com/RobTillaart/INA228 85 Volt, I2C, 20 bit
54+
- https://github.com/RobTillaart/INA236 48 Volt, I2C, 16 bit
55+
- https://github.com/RobTillaart/INA239 85 Volt, SPI, 16 bit
56+
- https://github.com/RobTillaart/INA3221_RT 26 Volt, I2C, 13 bits (3 channel)
57+
- https://www.adafruit.com/product/5832
58+
- https://www.mateksys.com/?portfolio=i2c-ina-bm
59+
- https://github.com/RobTillaart/printHelpers (for scientific notation)
5460

5561

5662
## I2C
@@ -233,10 +239,10 @@ Note: total conversion time can take up to 1024 \* 8.3 ms ~ 10 seconds.
233239

234240
#### ADCRange
235241

236-
The INA236 can set the ADC range to 20 mV (adcRange == 1)
237-
or to 80 mV (adcRange == 0) to optimize the accuracy.
242+
The INA236 can set the ADC range to 20 mV (adcRange == true)
243+
or to 80 mV (adcRange == false) to optimize the accuracy.
238244

239-
- **bool setADCRange(uint8_t adcRange)** adcRange = {0, 1}.
245+
- **bool setADCRange(bool flag)** adcRange = {true, false}.
240246
The function sets the voltage/LSB and returns false adcRange is out of range.
241247
- **uint8_t getADCRange()** returns set value.
242248

libraries/INA236/library.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "INA236",
33
"keywords": "power,ampere,voltage,volts,current",
4-
"description": "Arduino library for INA236 current and power sensor.",
4+
"description": "Arduino library for the INA236, I2C, 16 bit, voltage, current and power sensor.",
55
"authors":
66
[
77
{
@@ -18,7 +18,7 @@
1818
"type": "git",
1919
"url": "https://github.com/RobTillaart/INA236.git"
2020
},
21-
"version": "0.1.0",
21+
"version": "0.1.1",
2222
"license": "MIT",
2323
"frameworks": "*",
2424
"platforms": "*",

libraries/INA236/library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name=INA236
2-
version=0.1.0
2+
version=0.1.1
33
author=Rob Tillaart <[email protected]>, Josef Tremmel
44
maintainer=Rob Tillaart <[email protected]>
5-
sentence=Arduino library for INA236 current and power sensor.
5+
sentence=Arduino library for the INA236, I2C, 16 bit, voltage, current and power sensor.
66
paragraph=Voltage current Volt Ampere
77
category=Data Processing
88
url=https://github.com/RobTillaart/INA236

libraries/INA236/test/unit_test_001.cpp

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,17 @@ unittest_teardown()
4646
}
4747

4848

49-
unittest(test_constructor)
50-
{
51-
52-
INA236 INA(0x40);
53-
54-
//Wire.begin(); // throws an assertion in peek()
55-
//assertTrue(INA.begin());
56-
//assertTrue(INA.isConnected());
57-
assertEqual(0x40, INA.getAddress());
58-
59-
assertFalse(INA.isCalibrated());
60-
}
49+
//unittest(test_constructor)
50+
//{
51+
//INA236 INA(0x40);
52+
//
53+
//Wire.begin();
54+
//assertTrue(INA.begin());
55+
//assertTrue(INA.isConnected());
56+
//assertEqual(0x40, INA.getAddress());
57+
//
58+
//assertFalse(INA.isCalibrated());
59+
//}
6160

6261

6362
unittest(test_constants)
@@ -110,28 +109,28 @@ unittest(test_enum_BVCT_SVCT)
110109
}
111110

112111

113-
unittest(test_core_functions)
114-
{
115-
INA236 INA(0x40);
112+
// unittest(test_core_functions)
113+
// {
114+
// INA236 INA(0x40);
116115

117-
Wire.begin();
118-
assertTrue(INA.begin());
116+
// Wire.begin();
117+
// assertTrue(INA.begin());
119118

120-
fprintf(stderr, "need mock up\n");
121-
/*
122-
fprintf(stderr, "%f\n", INA.getShuntVoltage());
123-
fprintf(stderr, "%f\n", INA.getBusVoltage());
124-
fprintf(stderr, "%f\n", INA.getPower());
125-
fprintf(stderr, "%f\n", INA.getCurrent());
126-
*/
127-
}
119+
// fprintf(stderr, "need mock up\n");
120+
// /*
121+
// fprintf(stderr, "%f\n", INA.getShuntVoltage());
122+
// fprintf(stderr, "%f\n", INA.getBusVoltage());
123+
// fprintf(stderr, "%f\n", INA.getPower());
124+
// fprintf(stderr, "%f\n", INA.getCurrent());
125+
// */
126+
// }
128127

129128

130129
unittest(test_configuration)
131130
{
132131
INA236 INA(0x40);
133132

134-
Wire.begin();
133+
// Wire.begin();
135134
// assertTrue(INA.begin());
136135

137136
// only errors can be tested
@@ -151,7 +150,7 @@ unittest(test_calibration)
151150
{
152151
INA236 INA(0x40);
153152
154-
Wire.begin();
153+
// Wire.begin();
155154
// assertTrue(INA.begin());
156155
157156
assertEqual(INA236_ERR_NONE, INA.setMaxCurrentShunt(30, 0.002));
@@ -176,7 +175,7 @@ unittest(test_setMode)
176175
{
177176
INA236 INA(0x40);
178177

179-
Wire.begin();
178+
// Wire.begin();
180179
// assertTrue(INA.begin());
181180

182181
// only errors can be tested

0 commit comments

Comments
 (0)