Skip to content

Commit

Permalink
fix #68, gain for ADS1x13 (#69)
Browse files Browse the repository at this point in the history
- fix #68, gain bugs ADS1x13
- add unit test for ADS1x13
- update GitHub/actions to v4
- removed depreciated **getLastValue()**
- add multiplexer section to readme.md
  • Loading branch information
RobTillaart authored Mar 4, 2024
1 parent 7ad1382 commit 0974529
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 15 deletions.
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:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/arduino_test_runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/jsoncheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
36 changes: 34 additions & 2 deletions ADS1X15.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}


Expand Down Expand Up @@ -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
}


Expand Down
16 changes: 10 additions & 6 deletions ADS1X15.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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...
Expand Down Expand Up @@ -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();


Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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();
};


Expand Down Expand Up @@ -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();
};


Expand Down
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions examples/ADS1113_getMaxVoltage/ADS1113_getMaxVoltage.ino
Original file line number Diff line number Diff line change
@@ -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 --
3 changes: 2 additions & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Syntax Colouring Map For ADS1X15

# Data types (KEYWORD1)
ADS1X13 KEYWORD1
ADS1X15 KEYWORD1
ADS1013 KEYWORD1
ADS1014 KEYWORD1
ADS1015 KEYWORD1
ADS1015 KEYWORD1
Expand Down
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/ADS1X15"
},
"version": "0.4.1",
"version": "0.4.2",
"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=ADS1X15
version=0.4.1
version=0.4.2
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for ADS1015 - I2C 12 bit ADC and ADS1115 I2C 16 bit ADC
Expand Down
44 changes: 44 additions & 0 deletions test/unit_test_001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 0974529

Please sign in to comment.