From 09745296d0c13a66a063f97702f58edefcb98625 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Mon, 4 Mar 2024 20:39:08 +0100 Subject: [PATCH] fix #68, gain for ADS1x13 (#69) - fix #68, gain bugs ADS1x13 - add unit test for ADS1x13 - update GitHub/actions to v4 - removed depreciated **getLastValue()** - add multiplexer section to readme.md --- .github/workflows/arduino-lint.yml | 2 +- .github/workflows/arduino_test_runner.yml | 2 +- .github/workflows/jsoncheck.yml | 2 +- ADS1X15.cpp | 36 +++++++++++++- ADS1X15.h | 16 +++--- CHANGELOG.md | 9 +++- README.md | 18 +++++++ .../ADS1113_getMaxVoltage.ino | 49 +++++++++++++++++++ keywords.txt | 3 +- library.json | 2 +- library.properties | 2 +- test/unit_test_001.cpp | 44 +++++++++++++++++ 12 files changed, 170 insertions(+), 15 deletions(-) create mode 100644 examples/ADS1113_getMaxVoltage/ADS1113_getMaxVoltage.ino diff --git a/.github/workflows/arduino-lint.yml b/.github/workflows/arduino-lint.yml index 8a26f14..870a176 100644 --- a/.github/workflows/arduino-lint.yml +++ b/.github/workflows/arduino-lint.yml @@ -6,7 +6,7 @@ jobs: lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: arduino/arduino-lint-action@v1 with: library-manager: update diff --git a/.github/workflows/arduino_test_runner.yml b/.github/workflows/arduino_test_runner.yml index fadfa90..5506eb6 100644 --- a/.github/workflows/arduino_test_runner.yml +++ b/.github/workflows/arduino_test_runner.yml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: 2.6 diff --git a/.github/workflows/jsoncheck.yml b/.github/workflows/jsoncheck.yml index 37a1129..beb8829 100644 --- a/.github/workflows/jsoncheck.yml +++ b/.github/workflows/jsoncheck.yml @@ -10,7 +10,7 @@ jobs: test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: json-syntax-check uses: limitusus/json-syntax-check@v1 with: diff --git a/ADS1X15.cpp b/ADS1X15.cpp index 4167bdb..f2713ff 100644 --- a/ADS1X15.cpp +++ b/ADS1X15.cpp @@ -1,7 +1,7 @@ // // FILE: ADS1X15.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.4.1 +// VERSION: 0.4.2 // DATE: 2013-03-24 // PURPOSE: Arduino library for ADS1015 and ADS1115 // URL: https://github.com/RobTillaart/ADS1X15 @@ -193,7 +193,7 @@ float ADS1X15::toVoltage(int16_t value) if (value == 0) return 0; float volts = getMaxVoltage(); - if (volts < 0) return volts; + if (volts < 0) return volts; // propagate error volts *= value; if (_config & ADS_CONF_RES_16) @@ -530,6 +530,22 @@ ADS1013::ADS1013(uint8_t address, TwoWire *wire) _conversionDelay = ADS1015_CONVERSION_DELAY; _bitShift = 4; _maxPorts = 1; + _gain = ADS1X15_PGA_2_048V; // fixed value +} + + +// ADS1x13 has no gain so set default. +// Table 8. Config Register Field Descriptions +void ADS1013::setGain(uint8_t gain) +{ + _gain = gain; // keep compiler happy. + _gain = ADS1X15_PGA_2_048V; // fixed value +} + + +uint8_t ADS1013::getGain() +{ + return 2; // fixed value } @@ -623,6 +639,22 @@ ADS1113::ADS1113(uint8_t address, TwoWire *wire) _conversionDelay = ADS1115_CONVERSION_DELAY; _bitShift = 0; _maxPorts = 1; + _gain = ADS1X15_PGA_2_048V; // fixed value +} + + +// ADS1x13 has no gain so set default. +// Table 8. Config Register Field Descriptions +void ADS1113::setGain(uint8_t gain) +{ + _gain = gain; // keep compiler happy. + _gain = ADS1X15_PGA_2_048V; // fixed value +} + + +uint8_t ADS1113::getGain() +{ + return 2; // fixed value } diff --git a/ADS1X15.h b/ADS1X15.h index 8fd29ef..b1ab5c5 100644 --- a/ADS1X15.h +++ b/ADS1X15.h @@ -2,7 +2,7 @@ // // FILE: ADS1X15.h // AUTHOR: Rob Tillaart -// VERSION: 0.4.1 +// VERSION: 0.4.2 // DATE: 2013-03-24 // PURPOSE: Arduino library for ADS1015 and ADS1115 // URL: https://github.com/RobTillaart/ADS1X15 @@ -12,7 +12,7 @@ #include "Arduino.h" #include "Wire.h" -#define ADS1X15_LIB_VERSION (F("0.4.1")) +#define ADS1X15_LIB_VERSION (F("0.4.2")) // allow compile time default address // address in { 0x48, 0x49, 0x4A, 0x4B }, no test... @@ -72,8 +72,8 @@ class ADS1X15 int16_t readADC_Differential_0_1(); // used by continuous mode and async mode. - [[deprecated("Use getValue() instead")]] - int16_t getLastValue() { return getValue(); }; // will be obsolete in the future 0.4.0 + // [[deprecated("Use getValue() instead")]] + // int16_t getLastValue() { return getValue(); }; // will be obsolete in the future 0.4.0 int16_t getValue(); @@ -86,7 +86,7 @@ class ADS1X15 bool isReady(); - // returns a pin 0x0[0..3] or + // returns a pin 0x0[0..3] or // a differential "mode" 0x[pin second][pin first] or // 0xFF (no request / invalid request) uint8_t lastRequest(); @@ -164,7 +164,7 @@ class ADS1X15 uint8_t _compLatch; uint8_t _compQueConvert; - // variable to track the last pin requested, + // variable to track the last pin requested, // to allow for round robin query of // pins based on this state == if no last request then == 0xFFFF. uint16_t _lastRequest; @@ -188,6 +188,8 @@ class ADS1013 : public ADS1X15 { public: ADS1013(uint8_t Address = ADS1015_ADDRESS, TwoWire *wire = &Wire); + void setGain(uint8_t gain); + uint8_t getGain(); }; @@ -217,6 +219,8 @@ class ADS1113 : public ADS1X15 { public: ADS1113(uint8_t address = ADS1115_ADDRESS, TwoWire *wire = &Wire); + void setGain(uint8_t gain); + uint8_t getGain(); }; diff --git a/CHANGELOG.md b/CHANGELOG.md index ebfa260..9c8aaab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,11 +5,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.4.2] - 2024-03-04 +- fix #68, gain bugs ADS1x13 +- add unit test for ADS1x13 +- update GitHub/actions to v4 +- removed depreciated **getLastValue()** +- add multiplexer section to readme.md + + ## [0.4.1] - 2024-01-02 - fix some typos - minor edits - ## [0.4.0] - 2023-12-06 - refactor API, begin() - update readme.md diff --git a/README.md b/README.md index 2aaed0c..de03652 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,24 @@ is connected to: | SCL | 0x4B | | +#### I2C multiplexing + +Sometimes you need to control more devices than possible with the default +address range the device provides. +This is possible with an I2C multiplexer e.g. TCA9548 which creates up +to eight channels (think of it as I2C subnets) which can use the complete +address range of the device. + +Drawback of using a multiplexer is that it takes more administration in +your code e.g. which device is on which channel. +This will slow down the access, which must be taken into account when +deciding which devices are on which channel. +Also note that switching between channels will slow down other devices +too if they are behind the multiplexer. + +- https://github.com/RobTillaart/TCA9548 + + ## Interface ```cpp diff --git a/examples/ADS1113_getMaxVoltage/ADS1113_getMaxVoltage.ino b/examples/ADS1113_getMaxVoltage/ADS1113_getMaxVoltage.ino new file mode 100644 index 0000000..23515c8 --- /dev/null +++ b/examples/ADS1113_getMaxVoltage/ADS1113_getMaxVoltage.ino @@ -0,0 +1,49 @@ +// +// FILE: ADS1113_getMaxVoltage.ino +// AUTHOR: Rob.Tillaart +// PURPOSE: read analog inputs - straightforward. +// URL: https://github.com/RobTillaart/ADS1X15 + +// test for issue #68 behaviour ADS1113 / ADS1013 +// +// connect 1 potmeter per port. +// +// GND ---[ x ]------ 5V +// | +// +// measure at x (connect to AIN0). + + +#include "ADS1X15.h" + +ADS1113 ADS(0x48); + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + Serial.print("ADS1X15_LIB_VERSION: "); + Serial.println(ADS1X15_LIB_VERSION); + + Wire.begin(); + ADS.begin(); + + for (int g = 0; g < 8; g++) + { + ADS.setGain(g); + Serial.print(g); + Serial.print('\t'); + Serial.print(ADS.getGain()); // should all print 2 + Serial.print('\t'); + Serial.println(ADS.getMaxVoltage(), 3); // should all print 2.048 + } +} + + +void loop() +{ +} + + +// -- END OF FILE -- diff --git a/keywords.txt b/keywords.txt index 3613e70..570007a 100644 --- a/keywords.txt +++ b/keywords.txt @@ -1,7 +1,8 @@ # Syntax Colouring Map For ADS1X15 # Data types (KEYWORD1) -ADS1X13 KEYWORD1 +ADS1X15 KEYWORD1 +ADS1013 KEYWORD1 ADS1014 KEYWORD1 ADS1015 KEYWORD1 ADS1015 KEYWORD1 diff --git a/library.json b/library.json index 2ce5b65..92c0793 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/ADS1X15" }, - "version": "0.4.1", + "version": "0.4.2", "license": "MIT", "frameworks": "*", "platforms": "*", diff --git a/library.properties b/library.properties index 38a4753..c460e14 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ADS1X15 -version=0.4.1 +version=0.4.2 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for ADS1015 - I2C 12 bit ADC and ADS1115 I2C 16 bit ADC diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp index 879dc47..f8118fc 100644 --- a/test/unit_test_001.cpp +++ b/test/unit_test_001.cpp @@ -79,6 +79,30 @@ unittest(test_gain) } +// For issue #68, #2 +unittest(test_gain_ADS1113) +{ + ADS1113 ADS(0x48); + + Wire.begin(); + + assertTrue(ADS.begin()); + + assertEqual(2, ADS.getGain()); + int gains[6] = { 0,1,2,4,8,16 }; + for (int i = 0; i < 6; i++) + { + ADS.setGain(gains[i]); + assertEqual(2, ADS.getGain()); + assertEqualFloat(2.048, ADS.getMaxVoltage(), 0.001); + } + + ADS.setGain(42); + assertEqual(2, ADS.getGain()); + assertEqualFloat(2.048, ADS.getMaxVoltage(), 0.001); +} + + unittest(test_Voltage) { ADS1115 ADS(0x48); @@ -93,6 +117,26 @@ unittest(test_Voltage) float delta = abs(6.144 - volts); assertMoreOrEqual(0.001, delta); + ADS.setGain(1); + volts = ADS.getMaxVoltage(); + delta = abs(4.096 - volts); + assertMoreOrEqual(0.001, delta); + + ADS.setGain(2); + volts = ADS.getMaxVoltage(); + delta = abs(2.048 - volts); + assertMoreOrEqual(0.001, delta); + + ADS.setGain(4); + volts = ADS.getMaxVoltage(); + delta = abs(1.024 - volts); + assertMoreOrEqual(0.001, delta); + + ADS.setGain(8); + volts = ADS.getMaxVoltage(); + delta = abs(0.512 - volts); + assertMoreOrEqual(0.001, delta); + ADS.setGain(16); volts = ADS.getMaxVoltage(); delta = abs(0.256 - volts);