From ad88d90c9b6a01076c786ac7b26788869c4ea911 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Tue, 15 Mar 2022 15:37:34 +0100 Subject: [PATCH] =?UTF-8?q?add=20default=20parameter=20readADC()=20request?= =?UTF-8?q?ADC,=20created=20changelog.MD,=20upd=E2=80=A6=20(#41)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add default parameter readADC() requestADC(), * created changelog.MD, * update readme.md * add examples for the ADS1114 --- ADS1X15.cpp | 23 +--- ADS1X15.h | 16 ++- CHANGELOG.md | 124 ++++++++++++++++++ README.md | 14 +- examples/ADS_1114_four/ADS_1114_four.ino | 110 ++++++++++++++++ .../ADS_1114_two_continuous.ino | 119 +++++++++++++++++ .../ADS_continuous_8_channel.ino | 6 +- library.json | 2 +- library.properties | 2 +- 9 files changed, 384 insertions(+), 32 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 examples/ADS_1114_four/ADS_1114_four.ino create mode 100644 examples/ADS_1114_two_continuous/ADS_1114_two_continuous.ino diff --git a/ADS1X15.cpp b/ADS1X15.cpp index e8e34e8..f08d059 100644 --- a/ADS1X15.cpp +++ b/ADS1X15.cpp @@ -1,31 +1,10 @@ // // FILE: ADS1X15.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.3.5 +// VERSION: 0.3.6 // DATE: 2013-03-24 // PUPROSE: Arduino library for ADS1015 and ADS1115 // URL: https://github.com/RobTillaart/ADS1X15 -// -// HISTORY: -// 0.0.0 2013-03-24 initial version -// 0.0.1 2013-03-24 first working version -// 0.1.0 2017-07-31 removed pre 1.0 support; added getVoltage -// 0.2.0 2020-04-08 initial release; refactor ad fundum; -// 0.2.1 2020-08-15 fix issue 2 gain; refactor -// 0.2.2 2020-08-18 add begin(sda, scl) for ESP32 -// 0.2.3 2020-08-20 add comparator code + async mode -// 0.2.4 2020-08-26 check readme.md and minor fixes -// 0.2.5 2020-08-26 add missing readADC_Differential_X_X() -// 0.2.6 2020-09-01 fix #12 - fix getMaxVoltage + minor refactor -// 0.2.7 2020-09-27 redo readRegister() + getValue() + getError() -// 0.3.0 2021-03-29 add Wire parameter to constructors. -// 0.3.1 2021-04-25 #22, add get/setClock() for Wire speed + reset() -// 0.3.2 2021-10-07 fix build-CI; update readme + add new examples -// 0.3.3 2021-10-17 update build-CI (esp32), readme.md, keywords.txt -// 0.3.4 2021-12-11 update library.json, license, minor edits incl layout) -// add unit test constants. -// 0.3.5 2022-01-21 fix #36 support for Nano Every - #include "ADS1X15.h" diff --git a/ADS1X15.h b/ADS1X15.h index d545467..c3a3b17 100644 --- a/ADS1X15.h +++ b/ADS1X15.h @@ -2,7 +2,7 @@ // // FILE: ADS1X15.H // AUTHOR: Rob Tillaart -// VERSION: 0.3.5 +// VERSION: 0.3.6 // DATE: 2013-03-24 // PUPROSE: 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.3.5")) +#define ADS1X15_LIB_VERSION (F("0.3.6")) // allow compile time default address // address in { 0x48, 0x49, 0x4A, 0x4B }, no test... @@ -67,7 +67,7 @@ class ADS1X15 void setDataRate(uint8_t dataRate = 4); // invalid values are mapped on 4 (default) uint8_t getDataRate(); // actual speed depends on device - int16_t readADC(uint8_t pin); + int16_t readADC(uint8_t pin = 0); int16_t readADC_Differential_0_1(); // used by continuous mode and async mode. @@ -78,7 +78,7 @@ class ADS1X15 // ASYNC INTERFACE // requestADC(pin) -> isBusy() or isReady() -> getValue(); // see examples - void requestADC(uint8_t pin); + void requestADC(uint8_t pin = 0); void requestADC_Differential_0_1(); bool isBusy(); bool isReady(); @@ -163,6 +163,7 @@ class ADS1X15 uint32_t _clockSpeed = 0; }; + /////////////////////////////////////////////////////////////////////////// // // Derived classes from ADS1X15 @@ -173,12 +174,14 @@ class ADS1013 : public ADS1X15 ADS1013(uint8_t Address = ADS1015_ADDRESS, TwoWire *wire = &Wire); }; + class ADS1014 : public ADS1X15 { public: ADS1014(uint8_t Address = ADS1015_ADDRESS, TwoWire *wire = &Wire); }; + class ADS1015 : public ADS1X15 { public: @@ -193,6 +196,7 @@ class ADS1015 : public ADS1X15 void requestADC_Differential_2_3(); }; + /////////////////////////////////////////////////////////////////////////// // // Derived classes from ADS1X15 @@ -203,12 +207,14 @@ class ADS1113 : public ADS1X15 ADS1113(uint8_t address = ADS1115_ADDRESS, TwoWire *wire = &Wire); }; + class ADS1114 : public ADS1X15 { public: ADS1114(uint8_t address = ADS1115_ADDRESS, TwoWire *wire = &Wire); }; + class ADS1115 : public ADS1X15 { public: @@ -223,4 +229,6 @@ class ADS1115 : public ADS1X15 void requestADC_Differential_2_3(); }; + // --- END OF FILE --- + diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..7be3716 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,124 @@ +# Change Log +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/) +and this project adheres to [Semantic Versioning](http://semver.org/). + +## [0.3.6] - 2022-03-10 + +### Added +- CHANGELOG.md: moved history in ADS1x15.cpp to this file. +- add default parameters for single channel devices. + - **readADC(uint8_t pin = 0);** + - **requestADC(uint8_t pin = 0);** +- two examples for the **ADS1114** (single channel devices) +- update readme.md. + +### Changed + +### Fixed + +## [0.3.5] - 2022-01-21 + +### Added + +### Changed + +### Fixed +- fix #36 support for Nano Every + +## [0.3.4] - 2021-12-11 + +### Added +- add unit test constants. + +### Changed +- update library.json, license, +- minor edits incl layout + +### Fixed + +## [0.3.3] - 2021-10-17 + +### Added + +### Changed +- update build-CI (esp32), readme.md, keywords.txt + +### Fixed + +## [0.3.2] - 2021-10-07 + +### Added +- added examples + +### Changed +- update readme + +### Fixed +- fix build-CI; + +## [0.3.1] - 2021-04-25 + +### Added +- add get/setClock() for Wire speed +- reset() + +### Changed + +### Fixed +- issue #22 + +## [0.3.0] - 2021-03-29 + +### Added +- add Wire parameter to constructors. + +### Changed + +### Fixed + + +## OLDER versions + +### 0.2.7 - 2020-09-27 +- redo readRegister() +- getValue() +- getError() + +### [0.2.6] - 2020-09-01 +- fix #12 +- fix getMaxVoltage +- refactor + +### [0.2.5] - 2020-08-26 +- add missing readADC_Differential_X_X() + +### [0.2.4] - 2020-08-26 +- check readme.md and minor fixes + +### [0.2.3] - 2020-08-20 +- add comparator code +- add async mode + +### [0.2.2] - 2020-08-18 +- add begin(sda, scl) for ESP32 + +### [0.2.1] - 2020-08-15 +- fix issue #2 gain + +### [0.2.0] - 2020-04-08 +- initial release; +- refactor ad fundum; + +### [0.1.0] - 2017-07-31 +- removed pre 1.0 support; added getVoltage + +### [0.0.1] - 2013-03-24 +- first working version + +### [0.0.0] - 2013-03-24 +- initial version + + + diff --git a/README.md b/README.md index 9dd742f..0a2568d 100644 --- a/README.md +++ b/README.md @@ -5,12 +5,14 @@ [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/ADS1X15/blob/master/LICENSE) [![GitHub release](https://img.shields.io/github/release/RobTillaart/ADS1X15.svg?maxAge=3600)](https://github.com/RobTillaart/ADS1X15/releases) + # ADS1X15 Arduino library for I2C ADC ADS1015, ADS1115, and similar. For using I2C ADC with Raspberry pi or other SBC with Linux OS, you can check similar library [here](https://github.com/chandrawi/ADS1x15-ADC). + ## Description This library should work for the devices mentioned below, @@ -172,18 +174,24 @@ Data rate in samples per second, based on datasheet is described on table below. Reading the ADC is very straightforward, the **readADC()** function handles all in one call. Under the hood it uses the asynchronous calls. -- **int16_t readADC(uint8_t pin)** normal ADC functionality, pin = 0..3. +- **int16_t readADC(uint8_t pin = 0)** normal ADC functionality, pin = 0..3. If the pin number is out of range, this function will return 0. +Default pin = 0 as this is convenient for 1 channel devices. ```cpp -// read ADC in pin 0 +// read ADC in pin 2 +ADS.readADC(2); + +// read ADC in pin 0 - two ways +ADS.readADC(); ADS.readADC(0); ``` See [examples](https://github.com/RobTillaart/ADS1X15/blob/master/examples/ADS_minimum/ADS_minimum.ino). To read the ADC in an asynchronous way (e.g. to minimize blocking) you need call three functions: -- **void requestADC(uint8_t pin)** Start the conversion. pin = 0..3. +- **void requestADC(uint8_t pin = 0)** Start the conversion. pin = 0..3. +Default pin = 0 as this is convenient for 1 channel devices. - **bool isBusy()** Is the conversion not ready yet? Works only in SINGLE mode! - **bool isReady()** Is the conversion ready? Works only in SINGLE mode! (= wrapper around **isBusy()** ) - **int16_t getValue()** Read the result of the conversion. diff --git a/examples/ADS_1114_four/ADS_1114_four.ino b/examples/ADS_1114_four/ADS_1114_four.ino new file mode 100644 index 0000000..8b8be5f --- /dev/null +++ b/examples/ADS_1114_four/ADS_1114_four.ino @@ -0,0 +1,110 @@ +// +// FILE: ADS_1114_four.ino +// AUTHOR: Rob.Tillaart +// PURPOSE: demo reading four ADS1114 modules in parallel +// URL: https://github.com/RobTillaart/ADS1X15 + + +// Note all IO with the sensors are guarded by an isConnected() +// this is max robust, in non critical application one may either +// cache the value or only verify it in setup (least robust). +// Less robust may cause the application to hang - watchdog reset ? + + +#include "ADS1X15.h" + + +ADS1114 ADS[4]; +uint16_t val[4]; + +uint32_t last = 0, now = 0; + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + Serial.print("ADS1X15_LIB_VERSION: "); + Serial.println(ADS1X15_LIB_VERSION); + + for (uint8_t i = 0; i < 4; i++) + { + uint8_t address = 0x48 + i; + ADS[i] = ADS1114(address); + + Serial.print(address, HEX); + Serial.print(" "); + Serial.println(ADS[i].begin() ? "connected" : "not connected"); + + ADS[i].setDataRate(4); // 7 is fastest, but more noise + } + ADS_request_all(); +} + + +void loop() +{ + // Serial.println(__FUNCTION__); + // wait until all is read... + while(ADS_read_all()); + + // we have all values, so process (print) them + ADS_print_all(); + + delay(1000); // wait a second, comment this line for more samples. + ADS_request_all(); +} + + +void ADS_request_all() +{ + // Serial.println(__FUNCTION__); + for (int i = 0; i < 4; i++) + { + if (ADS[i].isConnected()) ADS[i].requestADC(0); + delayMicroseconds(200); // get them evenly spaced in time ... + } +} + + +bool ADS_read_all() +{ + // Serial.println(__FUNCTION__); + for (int i = 0; i < 4; i++) + { + if (ADS[i].isConnected() && ADS[i].isBusy()) return true; + } + // Serial.print("IDX:\t"); + // Serial.println(idx); + for (int i = 0; i < 4; i++) + { + if (ADS[i].isConnected()) + { + val[i] = ADS[i].getValue(); + } + } + ADS_request_all(); + return false; +} + + +void ADS_print_all() +{ + // Serial.println(__FUNCTION__); + // print duration since last print. + now = millis(); + Serial.print(now - last); + last = now; + Serial.println(); + + // PRINT ALL VALUES + for (int i = 0; i < 4; i++) + { + Serial.print(val[i]); + Serial.print("\t"); + } + Serial.println(); +} + + +// -- END OF FILE -- diff --git a/examples/ADS_1114_two_continuous/ADS_1114_two_continuous.ino b/examples/ADS_1114_two_continuous/ADS_1114_two_continuous.ino new file mode 100644 index 0000000..c050156 --- /dev/null +++ b/examples/ADS_1114_two_continuous/ADS_1114_two_continuous.ino @@ -0,0 +1,119 @@ +// +// FILE: ADS_1114_two_continuous.ino +// AUTHOR: Rob.Tillaart +// PURPOSE: demo reading four ADS1114 modules in parallel +// URL: https://github.com/RobTillaart/ADS1X15 + + +#include "ADS1X15.h" + + +ADS1114 ADS_1(0x49); +ADS1114 ADS_2(0x48); + + +// two interrupt flags +volatile bool RDY_1 = false; +volatile bool RDY_2 = false; + +int16_t val_1 = 0; +int16_t val_2 = 0; + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + Serial.print("ADS1X15_LIB_VERSION: "); + Serial.println(ADS1X15_LIB_VERSION); + + + // SETUP FIRST ADS1114 + ADS_1.begin(); + ADS_1.setGain(0); // 0 == 6.144 volt, default + ADS_1.setDataRate(7); // 7 == highest + + // SET ALERT RDY PIN + ADS_1.setComparatorThresholdHigh(0x8000); + ADS_1.setComparatorThresholdLow(0x0000); + ADS_1.setComparatorQueConvert(0); + + // SET INTERRUPT HANDLER TO CATCH CONVERSION READY + pinMode(2, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(2), adsReady_1, RISING); + + ADS_1.setMode(0); // 0 == continuous mode + ADS_1.readADC(); // 0 == default channel, trigger first read + + + // SETUP SECOND ADS1114 + ADS_2.begin(); + ADS_2.setGain(0); // 0 == 6.144 volt, default + ADS_2.setDataRate(7); // 7 == highest + + // SET ALERT RDY PIN + ADS_2.setComparatorThresholdHigh(0x8000); + ADS_2.setComparatorThresholdLow(0x0000); + ADS_2.setComparatorQueConvert(0); + + // SET INTERRUPT HANDLER TO CATCH CONVERSION READY + pinMode(3, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(3), adsReady_2, RISING); + + ADS_2.setMode(0); // 0 == continuous mode + ADS_2.readADC(); // 0 == default channel, trigger first read +} + + +void loop() +{ + if (handleConversion() == true) + { + Serial.print('\t'); + Serial.print(val_1); + Serial.print('\t'); + Serial.print(val_2); + Serial.println(); + } +} + + +// catch interrupt and set flag device 1 +void adsReady_1() +{ + RDY_1 = true; +} + +// catch interrupt and set flag device 1 +void adsReady_2() +{ + RDY_2 = true; +} + + +// handle conversions that are ready +bool handleConversion() +{ + bool rv = false; + if (RDY_1) + { + // save the last value + val_1 = ADS_1.getValue(); + ADS_1.readADC(0); + RDY_1 = false; + rv = true; + } + if (RDY_2) + { + // save the last value + val_2 = ADS_2.getValue(); + ADS_2.readADC(0); + RDY_2 = false; + rv = true; + } + return rv; +} + + +// -- END OF FILE -- + diff --git a/examples/ADS_continuous_8_channel/ADS_continuous_8_channel.ino b/examples/ADS_continuous_8_channel/ADS_continuous_8_channel.ino index f586c17..f4624e5 100644 --- a/examples/ADS_continuous_8_channel/ADS_continuous_8_channel.ino +++ b/examples/ADS_continuous_8_channel/ADS_continuous_8_channel.ino @@ -110,8 +110,9 @@ void adsReady_2() // handle conversions that are ready -void handleConversion() +bool handleConversion() { + bool rv = false; if (RDY_1) { // save the last value @@ -121,6 +122,7 @@ void handleConversion() if (channel_1 >= 4) channel_1 = 0; ADS_1.readADC(channel_1); RDY_1 = false; + rv = true; } if (RDY_2) { @@ -131,7 +133,9 @@ void handleConversion() if (channel_2 >= 4) channel_2 = 0; ADS_2.readADC(channel_2); RDY_2 = false; + rv = true; } + return rv; } diff --git a/library.json b/library.json index 25ac6d9..d30d09c 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/ADS1X15" }, - "version": "0.3.5", + "version": "0.3.6", "license": "MIT", "frameworks": "*", "platforms": "*", diff --git a/library.properties b/library.properties index a94dfaa..63dff0d 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ADS1X15 -version=0.3.5 +version=0.3.6 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for ADS1015 - I2C 12 bit ADC and ADS1115 I2C 16 bit ADC