diff --git a/ADS1X15.cpp b/ADS1X15.cpp index 7047512..f772e26 100644 --- a/ADS1X15.cpp +++ b/ADS1X15.cpp @@ -1,7 +1,7 @@ // // FILE: ADS1X15.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.3.13 +// VERSION: 0.4.0 // DATE: 2013-03-24 // PUPROSE: Arduino library for ADS1015 and ADS1115 // URL: https://github.com/RobTillaart/ADS1X15 @@ -140,33 +140,8 @@ void ADS1X15::reset() } -#if defined (ESP8266) || defined(ESP32) - -bool ADS1X15::begin(int sda, int scl) -{ - _wire->begin(sda, scl); - if ((_address < 0x48) || (_address > 0x4B)) return false; - if (! isConnected()) return false; - return true; -} - -#elif defined (ARDUINO_ARCH_RP2040) && !defined(__MBED__) - -bool ADS1X15::begin(int sda, int scl) -{ - _wire->setSDA(sda); - _wire->setSCL(scl); - _wire->begin(); - if ((_address < 0x48) || (_address > 0x4B)) return false; - if (! isConnected()) return false; - return true; -} - -#endif - bool ADS1X15::begin() { - _wire->begin(); if ((_address < 0x48) || (_address > 0x4B)) return false; if (! isConnected()) return false; return true; diff --git a/ADS1X15.h b/ADS1X15.h index 5019926..4ac801d 100644 --- a/ADS1X15.h +++ b/ADS1X15.h @@ -2,7 +2,7 @@ // // FILE: ADS1X15.h // AUTHOR: Rob Tillaart -// VERSION: 0.3.13 +// VERSION: 0.4.0 // 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.13")) +#define ADS1X15_LIB_VERSION (F("0.4.0")) // allow compile time default address // address in { 0x48, 0x49, 0x4A, 0x4B }, no test... @@ -36,12 +36,6 @@ class ADS1X15 public: void reset(); -#if defined (ESP8266) || defined(ESP32) - bool begin(int sda, int scl); -#elif defined (ARDUINO_ARCH_RP2040) && !defined(__MBED__) - bool begin(int sda, int scl); -#endif - bool begin(); bool isConnected(); diff --git a/CHANGELOG.md b/CHANGELOG.md index c40a794..0473f7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,17 +5,22 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.4.0] - 2023-12-06 +- refactor API, begin() +- update readme.md +- update examples + +---- + ## [0.3.13] - 2023-09-20 - fix #61 ESP32 begin() - ## [0.3.12] - 2023-09-11 - update and add examples - add **getLastRequest()** to track last type of measurement. - update readme.md - minor edits. - ## [0.3.11] - 2023-08-31 - update readme.md - move code from .h to .cpp diff --git a/README.md b/README.md index c7fa548..2aaed0c 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,15 @@ interesting from functionality point of view as these can also do differential measurements. +#### 0.4.0 Breaking change + +Version 0.4.0 introduced a breaking change. +You cannot set the pins in **begin()** any more. +This reduces the dependency of processor dependent Wire implementations. +The user has to call **Wire.begin()** and can optionally set the Wire pins +before calling **begin()**. + + #### Related - https://github.com/RobTillaart/MCP_ADC (10 & 12 bit ADC, SPI, fast) @@ -445,27 +454,14 @@ mean something different see - Comparator Mode above or datasheet. - **int16_t getComparatorThresholdHigh()** reads value from device. -## RP2040 specific - -- **bool begin(int sda, int scl)** begin communication with the ADC. -It has the parameter for selecting on which pins the communication should happen. -Check RP2040 Pinout for compatible pins. -If, "Wire1" is used, you need to add "&Wire1" in the constructor. - - ## Future ideas & improvements #### Must - Improve documentation (always) - #### Should -- investigate of remove the begin(sda, scl) versions - as the responsibility for the Wire configuration - should not be in this library. - #### Could diff --git a/examples/ADS_1114_four/ADS_1114_four.ino b/examples/ADS_1114_four/ADS_1114_four.ino index 3c4ff15..5fb7da2 100644 --- a/examples/ADS_1114_four/ADS_1114_four.ino +++ b/examples/ADS_1114_four/ADS_1114_four.ino @@ -27,6 +27,8 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); + Wire.begin(); + for (uint8_t i = 0; i < 4; i++) { uint8_t address = 0x48 + i; @@ -36,7 +38,8 @@ void setup() Serial.print(" "); Serial.println(ADS[i].begin() ? "connected" : "not connected"); - ADS[i].setDataRate(4); // 0 = slow 4 = medium 7 = fast, but more noise + // 0 = slow 4 = medium 7 = fast, but more noise + ADS[i].setDataRate(4); } ADS_request_all(); } @@ -44,14 +47,14 @@ void setup() void loop() { - // Serial.println(__FUNCTION__); - // wait until all is read... + // Serial.println(__FUNCTION__); + // wait until all is read... while(ADS_read_all()); - // we have all values, so process (print) them + // we have all values, so process (print) them ADS_print_all(); - delay(1000); // wait a second, comment this line for more samples. + delay(1000); // wait a second, comment this line for more samples. ADS_request_all(); } @@ -107,4 +110,4 @@ void ADS_print_all() } -// -- END OF FILE -- +// -- 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 index fff0ee4..b30dab1 100644 --- a/examples/ADS_1114_two_continuous/ADS_1114_two_continuous.ino +++ b/examples/ADS_1114_two_continuous/ADS_1114_two_continuous.ino @@ -27,18 +27,19 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); + Wire.begin(); - // SETUP FIRST ADS1114 + // SETUP FIRST ADS1114 ADS_1.begin(); ADS_1.setGain(0); // 0 == 6.144 volt, default ADS_1.setDataRate(7); // 0 = slow 4 = medium 7 = fast - // SET ALERT RDY PIN + // SET ALERT RDY PIN ADS_1.setComparatorThresholdHigh(0x8000); ADS_1.setComparatorThresholdLow(0x0000); ADS_1.setComparatorQueConvert(0); - // SET INTERRUPT HANDLER TO CATCH CONVERSION READY + // SET INTERRUPT HANDLER TO CATCH CONVERSION READY pinMode(2, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), adsReady_1, RISING); @@ -46,17 +47,17 @@ void setup() ADS_1.readADC(); // 0 == default channel, trigger first read - // SETUP SECOND ADS1114 + // 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 + // SET ALERT RDY PIN ADS_2.setComparatorThresholdHigh(0x8000); ADS_2.setComparatorThresholdLow(0x0000); ADS_2.setComparatorQueConvert(0); - // SET INTERRUPT HANDLER TO CATCH CONVERSION READY + // SET INTERRUPT HANDLER TO CATCH CONVERSION READY pinMode(3, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(3), adsReady_2, RISING); @@ -78,26 +79,26 @@ void loop() } -// catch interrupt and set flag device 1 +// catch interrupt and set flag device 1 void adsReady_1() { RDY_1 = true; } -// catch interrupt and set flag device 1 +// catch interrupt and set flag device 1 void adsReady_2() { RDY_2 = true; } -// handle conversions that are ready +// handle conversions that are ready bool handleConversion() { bool rv = false; if (RDY_1) { - // save the last value + // save the last value val_1 = ADS_1.getValue(); ADS_1.readADC(0); RDY_1 = false; @@ -105,7 +106,7 @@ bool handleConversion() } if (RDY_2) { - // save the last value + // save the last value val_2 = ADS_2.getValue(); ADS_2.readADC(0); RDY_2 = false; @@ -115,5 +116,5 @@ bool handleConversion() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/ADS_RP2040_WIRE1/ADS_RP2040_WIRE1.ino b/examples/ADS_RP2040_WIRE1/ADS_RP2040_WIRE1.ino index ba9763b..174e2f2 100644 --- a/examples/ADS_RP2040_WIRE1/ADS_RP2040_WIRE1.ino +++ b/examples/ADS_RP2040_WIRE1/ADS_RP2040_WIRE1.ino @@ -4,13 +4,13 @@ // PURPOSE: read analog input // URL: https://github.com/RobTillaart/ADS1X15 -// test -// connect 1 potmeter +// test +// connect 1 potentiometer // -// GND ---[ x ]------ 5V -// | +// GND ---[ x ]------ 5V +// | // -// measure at x (connect to AIN0). +// measure at x (connect to AIN0). #include "ADS1X15.h" @@ -32,19 +32,24 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); - ADS.begin(26, 27); // SDA (Pin 26), SCL(Pin 27) - ADS.setGain(0); // 6.144 volt - ADS.setDataRate(7); // 0 = slow 4 = medium 7 = fast - ADS.setMode(0); // continuous mode - ADS.readADC(0); // first read to trigger + // SDA (Pin 26), SCL(Pin 27) + Wire1.begin(); + Wire1.setSDA(26); + Wire1.setSCL(27); + + ADS.begin(); + ADS.setGain(0); // 6.144 volt + ADS.setDataRate(7); // 0 = slow 4 = medium 7 = fast + ADS.setMode(0); // continuous mode + ADS.readADC(0); // first read to trigger } void loop() { Serial.println(ADS.getValue()); - // optional other code here + // optional other code here } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/ADS_async_16_channel/ADS_async_16_channel.ino b/examples/ADS_async_16_channel/ADS_async_16_channel.ino index da96742..1c3234b 100644 --- a/examples/ADS_async_16_channel/ADS_async_16_channel.ino +++ b/examples/ADS_async_16_channel/ADS_async_16_channel.ino @@ -1,9 +1,7 @@ // // FILE: ADS_async_16_channel.ino // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 // PURPOSE: demo reading four ADS1115 modules in parallel -// DATE: 2021-07-06 // URL: https://github.com/RobTillaart/ADS1X15 @@ -30,6 +28,8 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); + Wire.begin(); + for (uint8_t i = 0; i < 4; i++) { uint8_t address = 0x48 + i; @@ -39,7 +39,8 @@ void setup() Serial.print(" "); Serial.println(ADS[i].begin() ? "connected" : "not connected"); - ADS[i].setDataRate(4); // 0 = slow 4 = medium 7 = fast, but more noise + // 0 = slow 4 = medium 7 = fast, but more noise + ADS[i].setDataRate(4); } ADS_request_all(); } @@ -47,14 +48,14 @@ void setup() void loop() { - // Serial.println(__FUNCTION__); - // wait until all is read... + // Serial.println(__FUNCTION__); + // wait until all is read... while (ADS_read_all()); - // we have all values + // we have all values ADS_print_all(); - delay(1000); // wait a second. + delay(1000); // wait a second. ADS_request_all(); } @@ -75,8 +76,8 @@ bool ADS_read_all() { if (ADS[i].isConnected() && ADS[i].isBusy()) return true; } - // Serial.print("IDX:\t"); - // Serial.println(idx); + // Serial.print("IDX:\t"); + // Serial.println(idx); for (int i = 0; i < 4; i++) { if (ADS[i].isConnected()) @@ -97,14 +98,14 @@ bool ADS_read_all() void ADS_print_all() { - // Serial.println(__FUNCTION__); - // TIMESTAMP + // Serial.println(__FUNCTION__); + // TIMESTAMP now = millis(); Serial.print(now - last); last = now; Serial.println(); - // PRINT ALL VALUES + // PRINT ALL VALUES for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) @@ -118,5 +119,5 @@ void ADS_print_all() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/ADS_async_8_channel/ADS_async_8_channel.ino b/examples/ADS_async_8_channel/ADS_async_8_channel.ino index 143a053..67b7cbe 100644 --- a/examples/ADS_async_8_channel/ADS_async_8_channel.ino +++ b/examples/ADS_async_8_channel/ADS_async_8_channel.ino @@ -2,7 +2,6 @@ // FILE: ADS_async_8_channel.ino // AUTHOR: Rob Tillaart // PURPOSE: demo reading two ADS1115 modules in parallel -// DATE: 2021-07-05 // URL: https://github.com/RobTillaart/ADS1X15 @@ -36,6 +35,8 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); + Wire.begin(); + ADS0.begin(); ADS1.begin(); // ADS2.begin(); @@ -46,7 +47,8 @@ void setup() // Serial.println(ADS2.isConnected()); // Serial.println(ADS3.isConnected()); - ADS0.setDataRate(4); // 0 = slow 4 = medium 7 = fast but more noise + // 0 = slow 4 = medium 7 = fast but more noise + ADS0.setDataRate(4); ADS1.setDataRate(4); // ADS2.setDataRate(4); // ADS3.setDataRate(4); @@ -58,13 +60,14 @@ void setup() void loop() { - // wait until all is read... + // wait until all is read... while (ADS_read_all()); - // we have all 8 values + // we have all 8 values ADS_print_all(); - delay(1000); // wait a second. + // wait a second. + delay(1000); ADS_request_all(); } @@ -106,13 +109,13 @@ void ADS_print_all() Serial.println(now - lastTime); lastTime = now; - // PRINT ALL VALUES OF ADC0 + // PRINT ALL VALUES OF ADC0 for (int i = 0; i < 4; i++) { Serial.print(val0[i]); Serial.print("\t"); } - // PRINT ALL VALUES OF ADC1 + // PRINT ALL VALUES OF ADC1 for (int i = 0; i < 4; i++) { Serial.print(val1[i]); @@ -135,5 +138,5 @@ void ADS_print_all() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/ADS_async_differential/ADS_async_differential.ino b/examples/ADS_async_differential/ADS_async_differential.ino index ed4c3f8..f5cfdd7 100644 --- a/examples/ADS_async_differential/ADS_async_differential.ino +++ b/examples/ADS_async_differential/ADS_async_differential.ino @@ -1,7 +1,6 @@ // // FILE: ADS_async_differential.ino // AUTHOR: Rob.Tillaart -// VERSION: 0.1.0 // PURPOSE: read multiple differential continuously // URL: https://github.com/RobTillaart/ADS1X15 @@ -40,15 +39,17 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); + Wire.begin(); + ADS.begin(); - ADS.setGain(0); // 6.144 volt - ADS.setDataRate(4); // 0 = slow 4 = medium 7 = fast + ADS.setGain(0); // 6.144 volt + ADS.setDataRate(4); // 0 = slow 4 = medium 7 = fast // single shot mode ADS.setMode(1); - // start with first pair + // start with first pair pair = 01; - // trigger first read + // trigger first read ADS.requestADC_Differential_0_1(); } @@ -69,7 +70,7 @@ void loop() } -// can be changed to hold other differentials reads too. +// can be changed to hold other differentials reads too. bool handleConversion() { if (ADS.isReady()) @@ -82,18 +83,18 @@ bool handleConversion() return false; // only one done } - // last of series to check + // last of series to check if (pair == 23) { val_23 = ADS.getValue(); pair = 01; ADS.requestADC_Differential_0_1(); - return true; // both are updated + return true; // both are updated } } - return false; // default not all read + return false; // default not all read } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/ADS_continuous/ADS_continuous.ino b/examples/ADS_continuous/ADS_continuous.ino index 78a4b43..71b85cd 100644 --- a/examples/ADS_continuous/ADS_continuous.ino +++ b/examples/ADS_continuous/ADS_continuous.ino @@ -40,6 +40,8 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); + Wire.begin(); + ADS.begin(); ADS.setGain(0); // 6.144 volt ADS.setDataRate(7); // 0 = slow 4 = medium 7 = fast diff --git a/examples/ADS_continuous_3_channel/ADS_continuous_3_channel.ino b/examples/ADS_continuous_3_channel/ADS_continuous_3_channel.ino index 4ea3e79..c8a81f2 100644 --- a/examples/ADS_continuous_3_channel/ADS_continuous_3_channel.ino +++ b/examples/ADS_continuous_3_channel/ADS_continuous_3_channel.ino @@ -52,18 +52,19 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); + Wire.begin(); Wire.setClock(400000); ADS.begin(); - ADS.setGain(0); // 6.144 volt - ADS.setDataRate(7); // 0 = slow 4 = medium 7 = fast + ADS.setGain(0); // 6.144 volt + ADS.setDataRate(7); // 0 = slow 4 = medium 7 = fast - // SET ALERT RDY PIN + // SET ALERT RDY PIN ADS.setComparatorThresholdHigh(0x8000); ADS.setComparatorThresholdLow(0x0000); ADS.setComparatorQueConvert(0); - // SET INTERRUPT HANDLER TO CATCH CONVERSION READY + // SET INTERRUPT HANDLER TO CATCH CONVERSION READY pinMode(2, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), adsReady, RISING); @@ -81,7 +82,7 @@ void loop() { SPS++; val[channel] = ADS.getValue(); - // request next channel asap + // request next channel asap channel++; if (channel >= 3) channel = 0; ADS.requestADC(channel); diff --git a/examples/ADS_continuous_4_channel/ADS_continuous_4_channel.ino b/examples/ADS_continuous_4_channel/ADS_continuous_4_channel.ino index 84d9e89..f95281d 100644 --- a/examples/ADS_continuous_4_channel/ADS_continuous_4_channel.ino +++ b/examples/ADS_continuous_4_channel/ADS_continuous_4_channel.ino @@ -41,24 +41,26 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); + Wire.begin(); + pinMode(2, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), adsReady, RISING); ADS.begin(); - ADS.setGain(0); // 6.144 volt - ADS.setDataRate(7); // 0 = slow 4 = medium 7 = fast + ADS.setGain(0); // 6.144 volt + ADS.setDataRate(7); // 0 = slow 4 = medium 7 = fast - // SET ALERT RDY PIN + // SET ALERT RDY PIN ADS.setComparatorThresholdHigh(0x8000); ADS.setComparatorThresholdLow(0x0000); ADS.setComparatorQueConvert(0); - // SET INTERRUPT HANDLER TO CATCH CONVERSION READY + // SET INTERRUPT HANDLER TO CATCH CONVERSION READY pinMode(2, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), adsReady, RISING); - ADS.setMode(0); // continuous mode - ADS.readADC(channel); // trigger first read + ADS.setMode(0); // continuous mode + ADS.readADC(channel); // trigger first read } @@ -89,9 +91,9 @@ void handleConversion() { if (RDY) { - // save the value + // save the value val[channel] = ADS.getValue(); - // request next channel + // request next channel channel++; if (channel >= 4) channel = 0; ADS.readADC(channel); @@ -100,5 +102,5 @@ void handleConversion() } -// -- END OF FILE -- +// -- 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 a7c0f40..5bea470 100644 --- a/examples/ADS_continuous_8_channel/ADS_continuous_8_channel.ino +++ b/examples/ADS_continuous_8_channel/ADS_continuous_8_channel.ino @@ -21,7 +21,7 @@ #include "ADS1X15.h" -// adjust addresses if needed +// adjust addresses if needed ADS1115 ADS_1(0x49); ADS1115 ADS_2(0x48); @@ -29,8 +29,8 @@ ADS1115 ADS_2(0x48); volatile bool RDY_1 = false; volatile bool RDY_2 = false; -uint8_t channel_1 = 0; // channel from device 1 -uint8_t channel_2 = 0; // channel from device 2 +uint8_t channel_1 = 0; // channel from device 1 +uint8_t channel_2 = 0; // channel from device 2 // array to hold the data. int16_t val[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; @@ -43,40 +43,42 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); - // SETUP FIRST ADS1115 + Wire.begin(); + + // SETUP FIRST ADS1115 ADS_1.begin(); - ADS_1.setGain(0); // 6.144 volt - ADS_1.setDataRate(7); // 0 = slow 4 = medium 7 = fast + ADS_1.setGain(0); // 6.144 volt + ADS_1.setDataRate(7); // 0 = slow 4 = medium 7 = fast - // SET ALERT RDY PIN + // SET ALERT RDY PIN ADS_1.setComparatorThresholdHigh(0x8000); ADS_1.setComparatorThresholdLow(0x0000); ADS_1.setComparatorQueConvert(0); - // SET INTERRUPT HANDLER TO CATCH CONVERSION READY + // SET INTERRUPT HANDLER TO CATCH CONVERSION READY pinMode(2, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), adsReady_1, RISING); - ADS_1.setMode(0); // continuous mode - ADS_1.readADC(channel_1); // trigger first read + ADS_1.setMode(0); // continuous mode + ADS_1.readADC(channel_1); // trigger first read - // SETUP SECOND ADS1115 + // SETUP SECOND ADS1115 ADS_2.begin(); - ADS_2.setGain(0); // 6.144 volt + ADS_2.setGain(0); // 6.144 volt ADS_2.setDataRate(7); - // SET ALERT RDY PIN + // SET ALERT RDY PIN ADS_2.setComparatorThresholdHigh(0x8000); ADS_2.setComparatorThresholdLow(0x0000); ADS_2.setComparatorQueConvert(0); - // SET INTERRUPT HANDLER TO CATCH CONVERSION READY + // SET INTERRUPT HANDLER TO CATCH CONVERSION READY pinMode(3, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(3), adsReady_2, RISING); - ADS_2.setMode(0); // continuous mode - ADS_2.readADC(channel_2); // trigger first read + ADS_2.setMode(0); // continuous mode + ADS_2.readADC(channel_2); // trigger first read } @@ -95,28 +97,28 @@ void loop() } -// catch interrupt and set flag device 1 +// catch interrupt and set flag device 1 void adsReady_1() { RDY_1 = true; } -// catch interrupt and set flag device 1 +// catch interrupt and set flag device 1 void adsReady_2() { RDY_2 = true; } -// handle conversions that are ready +// handle conversions that are ready bool handleConversion() { bool rv = false; if (RDY_1) { - // save the last value + // save the last value val[channel_1] = ADS_1.getValue(); - // request next channel + // request next channel channel_1++; if (channel_1 >= 4) channel_1 = 0; ADS_1.readADC(channel_1); @@ -125,9 +127,9 @@ bool handleConversion() } if (RDY_2) { - // save the last value + // save the last value val[4 + channel_2] = ADS_2.getValue(); - // request next channel + // request next channel channel_2++; if (channel_2 >= 4) channel_2 = 0; ADS_2.readADC(channel_2); @@ -138,5 +140,5 @@ bool handleConversion() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/ADS_continuous_differential/ADS_continuous_differential.ino b/examples/ADS_continuous_differential/ADS_continuous_differential.ino index 8954434..d19968b 100644 --- a/examples/ADS_continuous_differential/ADS_continuous_differential.ino +++ b/examples/ADS_continuous_differential/ADS_continuous_differential.ino @@ -28,11 +28,11 @@ ADS1115 ADS(0x48); -// interrupt flag +// interrupt flag volatile bool RDY = false; -// which pair to use for differential +// which pair to use for differential uint8_t pair = 01; -// two values to hold differential measurements. +// two values to hold differential measurements. int16_t val_01 = 0; int16_t val_23 = 0; @@ -44,29 +44,31 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); - // SET INTERRUPT HANDLER TO CATCH CONVERSION READY + Wire.begin(); + + // SET INTERRUPT HANDLER TO CATCH CONVERSION READY pinMode(2, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), adsReady, RISING); ADS.begin(); Serial.print("connected: "); Serial.println(ADS.isConnected()); - ADS.setGain(0); // 6.144 volt - ADS.setDataRate(0); // 0 = slow 4 = medium 7 = fast (7 = fails ) - // every step is about a factor 2 slower. + ADS.setGain(0); // 6.144 volt + ADS.setDataRate(0); // 0 = slow 4 = medium 7 = fast (7 = fails ) + // every step is about a factor 2 slower. - // SET ALERT RDY PIN (QueConvert mode) - // set the MSB of the Hi_thresh register to 1 + // SET ALERT RDY PIN (QueConvert mode) + // set the MSB of the Hi_thresh register to 1 ADS.setComparatorThresholdHigh(0x8000); - // set the MSB of the Lo_thresh register to 0 + // set the MSB of the Lo_thresh register to 0 ADS.setComparatorThresholdLow(0x0000); ADS.setComparatorQueConvert(0); - // continuous mode + // continuous mode ADS.setMode(0); - // start with first pair + // start with first pair pair = 01; - // trigger first read + // trigger first read ADS.requestADC_Differential_0_1(); } @@ -86,19 +88,19 @@ void loop() Serial.println(); } - // do other stuff here - // delay(10); + // do other stuff here + // delay(10); } -// interrupt handler, sets the RDY flag +// interrupt handler, sets the RDY flag void adsReady() { RDY = true; } -// can be changed to hold other differentials or normal reads too. +// can be changed to hold other differentials or normal reads too. bool handleConversion() { if (RDY) @@ -109,21 +111,21 @@ bool handleConversion() val_01 = ADS.getValue(); pair = 23; ADS.requestADC_Differential_2_3(); - return false; // only one done + return false; // only one done } - // last of series to check + // last of series to check if (pair == 23) { val_23 = ADS.getValue(); pair = 01; ADS.requestADC_Differential_0_1(); - return true; // both are updated + return true; // both are updated } } - return false; // default not all read + return false; // default not all read } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/ADS_differential/ADS_differential.ino b/examples/ADS_differential/ADS_differential.ino index 243233e..546ce4a 100644 --- a/examples/ADS_differential/ADS_differential.ino +++ b/examples/ADS_differential/ADS_differential.ino @@ -38,6 +38,8 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); + Wire.begin(); + ADS.begin(); ADS.setGain(0); } @@ -64,5 +66,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/ADS_high_speed_differential/ADS_high_speed_differential.ino b/examples/ADS_high_speed_differential/ADS_high_speed_differential.ino index 4a618fb..f83b6e3 100644 --- a/examples/ADS_high_speed_differential/ADS_high_speed_differential.ino +++ b/examples/ADS_high_speed_differential/ADS_high_speed_differential.ino @@ -17,7 +17,7 @@ #include "ADS1X15.h" -// adjust addresses if needed +// adjust addresses if needed ADS1115 ADS_1(0x49); ADS1115 ADS_2(0x48); @@ -34,43 +34,45 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); - // SETUP FIRST ADS1115 + Wire.begin(); + + // SETUP FIRST ADS1115 ADS_1.begin(); - ADS_1.setGain(0); // 6.144 volt - ADS_1.setDataRate(7); // 0 = slow 4 = medium 7 = fast + ADS_1.setGain(0); // 6.144 volt + ADS_1.setDataRate(7); // 0 = slow 4 = medium 7 = fast - // SET ALERT RDY PIN (QueConvert mode) - // set the MSB of the Hi_thresh register to 1 + // SET ALERT RDY PIN (QueConvert mode) + // set the MSB of the Hi_thresh register to 1 ADS_1.setComparatorThresholdHigh(0x8000); // set the MSB of the Lo_thresh register to 0 ADS_1.setComparatorThresholdLow(0x0000); ADS_1.setComparatorQueConvert(0); - // SET INTERRUPT HANDLER TO CATCH CONVERSION READY + // SET INTERRUPT HANDLER TO CATCH CONVERSION READY pinMode(2, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), adsReady_1, RISING); - ADS_1.setMode(0); // continuous mode - ADS_1.readADC(channel); // trigger first read + ADS_1.setMode(0); // continuous mode + ADS_1.readADC(channel); // trigger first read - // SETUP SECOND ADS1115 + // SETUP SECOND ADS1115 ADS_2.begin(); - ADS_2.setGain(0); // 6.144 volt + ADS_2.setGain(0); // 6.144 volt ADS_2.setDataRate(7); - // SET ALERT RDY PIN - // set the MSB of the Hi_thresh register to 1 + // SET ALERT RDY PIN + // set the MSB of the Hi_thresh register to 1 ADS_2.setComparatorThresholdHigh(0x8000); // set the MSB of the Lo_thresh register to 0 ADS_2.setComparatorThresholdLow(0x0000); ADS_2.setComparatorQueConvert(0); - // SET INTERRUPT HANDLER TO CATCH CONVERSION READY + // SET INTERRUPT HANDLER TO CATCH CONVERSION READY pinMode(3, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(3), adsReady_2, RISING); - ADS_2.setMode(0); // continuous mode + ADS_2.setMode(0); // continuous mode ADS_2.readADC(channel); // trigger first read } @@ -89,7 +91,7 @@ void loop() } -// catch interrupt and set flag +// catch interrupt and set flag void adsReady_1() { RDY_1 = true; @@ -101,7 +103,7 @@ void adsReady_2() } -// handle conversions if both are ready +// handle conversions if both are ready bool handleConversion() { if (RDY_1 == false) return false; @@ -123,5 +125,5 @@ bool handleConversion() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/ADS_minimum/ADS_minimum.ino b/examples/ADS_minimum/ADS_minimum.ino index 67e01ce..021df49 100644 --- a/examples/ADS_minimum/ADS_minimum.ino +++ b/examples/ADS_minimum/ADS_minimum.ino @@ -35,6 +35,8 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); + Wire.begin(); + ADS.begin(); ADS.setGain(0); // 6.144 volt Serial.println("Voltage"); @@ -48,5 +50,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/ADS_performance/ADS_performance.ino b/examples/ADS_performance/ADS_performance.ino index b2d1a4c..a781d81 100644 --- a/examples/ADS_performance/ADS_performance.ino +++ b/examples/ADS_performance/ADS_performance.ino @@ -38,10 +38,10 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); - ADS.begin(); - + Wire.begin(); Wire.setClock(100000); + ADS.begin(); ADS.setGain(0); // 6.144 volt for (int dr = 0; dr < 8; dr++) diff --git a/examples/ADS_pointerToFunction/ADS_pointerToFunction.ino b/examples/ADS_pointerToFunction/ADS_pointerToFunction.ino index b8ea6fa..744ec19 100644 --- a/examples/ADS_pointerToFunction/ADS_pointerToFunction.ino +++ b/examples/ADS_pointerToFunction/ADS_pointerToFunction.ino @@ -23,6 +23,9 @@ void setup() while(!Serial); Serial.println(__FILE__); + Wire.begin(); + Wire.setClock(100000); + ADS.begin(); // use defaults readADC = analogRead; // start with internal @@ -45,4 +48,4 @@ int wrapper(uint8_t x) -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/ADS_read/ADS_read.ino b/examples/ADS_read/ADS_read.ino index 249c68b..32c1f00 100644 --- a/examples/ADS_read/ADS_read.ino +++ b/examples/ADS_read/ADS_read.ino @@ -26,6 +26,7 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); + Wire.begin(); ADS.begin(); } @@ -51,5 +52,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/ADS_read_RDY/ADS_read_RDY.ino b/examples/ADS_read_RDY/ADS_read_RDY.ino index 7e17aab..2b9d1f4 100644 --- a/examples/ADS_read_RDY/ADS_read_RDY.ino +++ b/examples/ADS_read_RDY/ADS_read_RDY.ino @@ -4,18 +4,18 @@ // PURPOSE: read analog inputs - straightforward. // URL: https://github.com/RobTillaart/ADS1X15 -// test -// connect 1 potmeter per port. +// test +// connect 1 potentiometer per port. // -// GND ---[ x ]------ 5V -// | +// GND ---[ x ]------ 5V +// | // -// measure at x (connect to AIN0). +// measure at x (connect to AIN0). // -// EXPERIMENTAL +// EXPERIMENTAL // -// The RDY pin (or ALERT Pin) is triggered when conversion is ready +// The RDY pin (or ALERT Pin) is triggered when conversion is ready // @@ -31,16 +31,18 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); + Wire.begin(); + ADS.begin(); - ADS.setGain(0); // 6.144 volt - ADS.setDataRate(7); // 0 = slow 4 = medium 7 = fast - ADS.setMode(1); // continuous mode - ADS.readADC(0); // first read to trigger + ADS.setGain(0); // 6.144 volt + ADS.setDataRate(7); // 0 = slow 4 = medium 7 = fast + ADS.setMode(1); // continuous mode + ADS.readADC(0); // first read to trigger - // set the thresholds to Trigger RDY pin + // set the thresholds to Trigger RDY pin ADS.setComparatorThresholdLow(0x0000); ADS.setComparatorThresholdHigh(0x0200); - ADS.setComparatorQueConvert(0); // enable RDY pin !! + ADS.setComparatorQueConvert(0); // enable RDY pin !! ADS.setComparatorLatch(0); } @@ -51,7 +53,7 @@ void loop() int16_t val_0 = ADS.readADC(0); - float f = ADS.toVoltage(1); // voltage factor + float f = ADS.toVoltage(1); // voltage factor Serial.print("\tAnalog0: "); Serial.print(val_0); @@ -62,5 +64,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/ADS_read_async/ADS_read_async.ino b/examples/ADS_read_async/ADS_read_async.ino index 23a4f36..e32ec7d 100644 --- a/examples/ADS_read_async/ADS_read_async.ino +++ b/examples/ADS_read_async/ADS_read_async.ino @@ -27,9 +27,11 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); + Wire.begin(); + ADS.begin(); ADS.setGain(0); - f = ADS.toVoltage(); // voltage factor + f = ADS.toVoltage(); // voltage factor ADS.requestADC(0); } @@ -39,16 +41,17 @@ void loop() if (ADS.isBusy() == false) { int16_t val_0 = ADS.getValue(); - ADS.requestADC(0); // request a new one + // request a new one + ADS.requestADC(0); Serial.print("\tAnalog0: "); Serial.print(val_0); Serial.print('\t'); Serial.println(val_0 * f, 3); } - // simulate other tasks... + // simulate other tasks... delay(2000); } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/ADS_read_async_rdy/ADS_read_async_rdy.ino b/examples/ADS_read_async_rdy/ADS_read_async_rdy.ino index 48ca367..5b1a47a 100644 --- a/examples/ADS_read_async_rdy/ADS_read_async_rdy.ino +++ b/examples/ADS_read_async_rdy/ADS_read_async_rdy.ino @@ -32,17 +32,20 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); + Wire.begin(); + ADS.begin(); - ADS.setGain(0); // 6.144 volt - // select slow so the led blinks visible for the eye. - ADS.setDataRate(0); // 0 = slow 4 = medium 7 = fast - f = ADS.toVoltage(); // voltage factor + ADS.setGain(0); // 6.144 volt + + // select slow so the led blinks visible for the eye. + ADS.setDataRate(0); // 0 = slow 4 = medium 7 = fast + f = ADS.toVoltage(); // voltage factor ADS.requestADC(0); - // set the thresholds to Trigger RDY pin + // set the thresholds to Trigger RDY pin ADS.setComparatorThresholdLow(0x0000); ADS.setComparatorThresholdHigh(0x0200); - ADS.setComparatorQueConvert(0); // enable RDY pin !! + ADS.setComparatorQueConvert(0); // enable RDY pin !! ADS.setComparatorLatch(0); } @@ -52,16 +55,17 @@ void loop() if (ADS.isReady()) { int16_t val_0 = ADS.getValue(); - ADS.requestADC(0); // request a new one + // request a new one + ADS.requestADC(0); Serial.print("\tAnalog0: "); Serial.print(val_0); Serial.print('\t'); Serial.println(val_0 * f, 3); } - // simulate other tasks... + // simulate other tasks... delay(2000); } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/ADS_read_comparator_1/ADS_read_comparator_1.ino b/examples/ADS_read_comparator_1/ADS_read_comparator_1.ino index bdd9365..d1e31b2 100644 --- a/examples/ADS_read_comparator_1/ADS_read_comparator_1.ino +++ b/examples/ADS_read_comparator_1/ADS_read_comparator_1.ino @@ -32,26 +32,28 @@ void setup() Serial.print("ADS1X15_LIB_VERSION: "); Serial.println(ADS1X15_LIB_VERSION); + Wire.begin(); + ADS.begin(); - // change if needed. - ADS.setComparatorMode(1); // 0 = TRADITIONAL 1 = WINDOW + // change if needed. + ADS.setComparatorMode(1); // 0 = TRADITIONAL 1 = WINDOW - ADS.setComparatorPolarity(0); // 0 = LOW (default) 1 = HIGH + ADS.setComparatorPolarity(0); // 0 = LOW (default) 1 = HIGH - // note NON-LATCH gives only a short pulse - ADS.setComparatorLatch(1); // 0 = NON LATCH 1 = LATCH + // note NON-LATCH gives only a short pulse + ADS.setComparatorLatch(1); // 0 = NON LATCH 1 = LATCH - ADS.setComparatorQueConvert(0); // 0 = trigger alert after 1 conversion + ADS.setComparatorQueConvert(0); // 0 = trigger alert after 1 conversion - // set the thresholds as a number... - // ADS.setComparatorThresholdLow(5000); // change if needed - // ADS.setComparatorThresholdHigh(20000); // change if needed + // set the thresholds as a number... + // ADS.setComparatorThresholdLow(5000); // change if needed + // ADS.setComparatorThresholdHigh(20000); // change if needed - // set the threshold as a voltage by using the voltage factor. - float f = ADS.toVoltage(1); // voltage factor - ADS.setComparatorThresholdLow(1.234 / f); // convert volts to number needed - ADS.setComparatorThresholdHigh(3.142 / f); // convert volts to number needed + // set the threshold as a voltage by using the voltage factor. + float f = ADS.toVoltage(1); // voltage factor + ADS.setComparatorThresholdLow(1.234 / f); // convert volts to number needed + ADS.setComparatorThresholdHigh(3.142 / f); // convert volts to number needed Serial.println(ADS.getComparatorThresholdLow()); Serial.println(ADS.getComparatorThresholdHigh()); @@ -64,7 +66,7 @@ void loop() int16_t val_0 = ADS.readADC(0); - float f = ADS.toVoltage(1); // voltage factor + float f = ADS.toVoltage(1); // voltage factor Serial.print("\tAnalog0: "); Serial.print(val_0); @@ -80,5 +82,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/ADS_setWireClock/ADS_setWireClock.ino b/examples/ADS_setWireClock/ADS_setWireClock.ino index cfc2da0..1608db2 100644 --- a/examples/ADS_setWireClock/ADS_setWireClock.ino +++ b/examples/ADS_setWireClock/ADS_setWireClock.ino @@ -4,13 +4,13 @@ // PURPOSE: read analog inputs - straightforward. // URL: https://github.com/RobTillaart/ADS1X15 -// test -// connect 1 potmeter per port. +// test +// connect 1 potentiometer per port. // -// GND ---[ x ]------ 5V -// | +// GND ---[ x ]------ 5V +// | // -// measure at x (connect to AIN0). +// measure at x (connect to AIN0). // @@ -50,7 +50,7 @@ void loop() int16_t val_2 = ADS.readADC(2); int16_t val_3 = ADS.readADC(3); - float f = ADS.toVoltage(1); // voltage factor + float f = ADS.toVoltage(1); // voltage factor Serial.print("\tAnalog0: "); Serial.print(val_0); Serial.print('\t'); Serial.println(val_0 * f, 3); Serial.print("\tAnalog1: "); Serial.print(val_1); Serial.print('\t'); Serial.println(val_1 * f, 3); @@ -62,5 +62,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/library.json b/library.json index 496f30b..1e19778 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/ADS1X15" }, - "version": "0.3.13", + "version": "0.4.0", "license": "MIT", "frameworks": "*", "platforms": "*", diff --git a/library.properties b/library.properties index ec84b7d..821fdf2 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ADS1X15 -version=0.3.13 +version=0.4.0 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 5f0a395..6df7781 100644 --- a/test/unit_test_001.cpp +++ b/test/unit_test_001.cpp @@ -27,6 +27,7 @@ unittest_setup() { + fprintf(stderr, "ADS1X15_LIB_VERSION: %s\n", (char *) ADS1X15_LIB_VERSION); } @@ -37,8 +38,6 @@ unittest_teardown() unittest(test_constants) { - fprintf(stderr, "ADS1X15_LIB_VERSION: %s\n", (char *) ADS1X15_LIB_VERSION); - assertEqual(0x48, ADS1015_ADDRESS); assertEqual(0x48, ADS1115_ADDRESS); assertEqual( 0, ADS1X15_OK); @@ -51,6 +50,9 @@ unittest(test_constants) unittest(test_begin) { ADS1115 ADS(0x48); + + Wire.begin(); + assertTrue(ADS.begin()); assertTrue(ADS.isConnected()); assertTrue(ADS.isBusy()); @@ -60,6 +62,9 @@ unittest(test_begin) unittest(test_gain) { ADS1115 ADS(0x48); + + Wire.begin(); + assertTrue(ADS.begin()); assertEqual(0, ADS.getGain()); @@ -78,6 +83,9 @@ unittest(test_gain) unittest(test_Voltage) { ADS1115 ADS(0x48); + + Wire.begin(); + assertTrue(ADS.begin()); // should test all values? @@ -96,4 +104,5 @@ unittest(test_Voltage) unittest_main() -// -------- +// -- END OF FILE -- +