0.3.6 ADS1x15

This commit is contained in:
rob tillaart 2022-03-15 15:40:30 +01:00
parent 5c94b97699
commit cbb91dc824
9 changed files with 384 additions and 32 deletions

View File

@ -1,31 +1,10 @@
// //
// FILE: ADS1X15.cpp // FILE: ADS1X15.cpp
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.3.5 // VERSION: 0.3.6
// DATE: 2013-03-24 // DATE: 2013-03-24
// PUPROSE: Arduino library for ADS1015 and ADS1115 // PUPROSE: Arduino library for ADS1015 and ADS1115
// URL: https://github.com/RobTillaart/ADS1X15 // 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" #include "ADS1X15.h"

View File

@ -2,7 +2,7 @@
// //
// FILE: ADS1X15.H // FILE: ADS1X15.H
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.3.5 // VERSION: 0.3.6
// DATE: 2013-03-24 // DATE: 2013-03-24
// PUPROSE: Arduino library for ADS1015 and ADS1115 // PUPROSE: Arduino library for ADS1015 and ADS1115
// URL: https://github.com/RobTillaart/ADS1X15 // URL: https://github.com/RobTillaart/ADS1X15
@ -12,7 +12,7 @@
#include "Arduino.h" #include "Arduino.h"
#include "Wire.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 // allow compile time default address
// address in { 0x48, 0x49, 0x4A, 0x4B }, no test... // address in { 0x48, 0x49, 0x4A, 0x4B }, no test...
@ -67,7 +67,7 @@ public:
void setDataRate(uint8_t dataRate = 4); // invalid values are mapped on 4 (default) void setDataRate(uint8_t dataRate = 4); // invalid values are mapped on 4 (default)
uint8_t getDataRate(); // actual speed depends on device 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(); int16_t readADC_Differential_0_1();
// used by continuous mode and async mode. // used by continuous mode and async mode.
@ -78,7 +78,7 @@ public:
// ASYNC INTERFACE // ASYNC INTERFACE
// requestADC(pin) -> isBusy() or isReady() -> getValue(); // requestADC(pin) -> isBusy() or isReady() -> getValue();
// see examples // see examples
void requestADC(uint8_t pin); void requestADC(uint8_t pin = 0);
void requestADC_Differential_0_1(); void requestADC_Differential_0_1();
bool isBusy(); bool isBusy();
bool isReady(); bool isReady();
@ -163,6 +163,7 @@ protected:
uint32_t _clockSpeed = 0; uint32_t _clockSpeed = 0;
}; };
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// //
// Derived classes from ADS1X15 // Derived classes from ADS1X15
@ -173,12 +174,14 @@ public:
ADS1013(uint8_t Address = ADS1015_ADDRESS, TwoWire *wire = &Wire); ADS1013(uint8_t Address = ADS1015_ADDRESS, TwoWire *wire = &Wire);
}; };
class ADS1014 : public ADS1X15 class ADS1014 : public ADS1X15
{ {
public: public:
ADS1014(uint8_t Address = ADS1015_ADDRESS, TwoWire *wire = &Wire); ADS1014(uint8_t Address = ADS1015_ADDRESS, TwoWire *wire = &Wire);
}; };
class ADS1015 : public ADS1X15 class ADS1015 : public ADS1X15
{ {
public: public:
@ -193,6 +196,7 @@ public:
void requestADC_Differential_2_3(); void requestADC_Differential_2_3();
}; };
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// //
// Derived classes from ADS1X15 // Derived classes from ADS1X15
@ -203,12 +207,14 @@ public:
ADS1113(uint8_t address = ADS1115_ADDRESS, TwoWire *wire = &Wire); ADS1113(uint8_t address = ADS1115_ADDRESS, TwoWire *wire = &Wire);
}; };
class ADS1114 : public ADS1X15 class ADS1114 : public ADS1X15
{ {
public: public:
ADS1114(uint8_t address = ADS1115_ADDRESS, TwoWire *wire = &Wire); ADS1114(uint8_t address = ADS1115_ADDRESS, TwoWire *wire = &Wire);
}; };
class ADS1115 : public ADS1X15 class ADS1115 : public ADS1X15
{ {
public: public:
@ -223,4 +229,6 @@ public:
void requestADC_Differential_2_3(); void requestADC_Differential_2_3();
}; };
// --- END OF FILE --- // --- END OF FILE ---

View File

@ -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

View File

@ -5,12 +5,14 @@
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/ADS1X15/blob/master/LICENSE) [![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) [![GitHub release](https://img.shields.io/github/release/RobTillaart/ADS1X15.svg?maxAge=3600)](https://github.com/RobTillaart/ADS1X15/releases)
# ADS1X15 # ADS1X15
Arduino library for I2C ADC ADS1015, ADS1115, and similar. 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). 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 ## Description
This library should work for the devices mentioned below, 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. Reading the ADC is very straightforward, the **readADC()** function handles all in one call.
Under the hood it uses the asynchronous calls. 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. 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 ```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); ADS.readADC(0);
``` ```
See [examples](https://github.com/RobTillaart/ADS1X15/blob/master/examples/ADS_minimum/ADS_minimum.ino). 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: 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 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()** ) - **bool isReady()** Is the conversion ready? Works only in SINGLE mode! (= wrapper around **isBusy()** )
- **int16_t getValue()** Read the result of the conversion. - **int16_t getValue()** Read the result of the conversion.

View File

@ -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 --

View File

@ -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 --

View File

@ -110,8 +110,9 @@ void adsReady_2()
// handle conversions that are ready // handle conversions that are ready
void handleConversion() bool handleConversion()
{ {
bool rv = false;
if (RDY_1) if (RDY_1)
{ {
// save the last value // save the last value
@ -121,6 +122,7 @@ void handleConversion()
if (channel_1 >= 4) channel_1 = 0; if (channel_1 >= 4) channel_1 = 0;
ADS_1.readADC(channel_1); ADS_1.readADC(channel_1);
RDY_1 = false; RDY_1 = false;
rv = true;
} }
if (RDY_2) if (RDY_2)
{ {
@ -131,7 +133,9 @@ void handleConversion()
if (channel_2 >= 4) channel_2 = 0; if (channel_2 >= 4) channel_2 = 0;
ADS_2.readADC(channel_2); ADS_2.readADC(channel_2);
RDY_2 = false; RDY_2 = false;
rv = true;
} }
return rv;
} }

View File

@ -15,7 +15,7 @@
"type": "git", "type": "git",
"url": "https://github.com/RobTillaart/ADS1X15" "url": "https://github.com/RobTillaart/ADS1X15"
}, },
"version": "0.3.5", "version": "0.3.6",
"license": "MIT", "license": "MIT",
"frameworks": "*", "frameworks": "*",
"platforms": "*", "platforms": "*",

View File

@ -1,5 +1,5 @@
name=ADS1X15 name=ADS1X15
version=0.3.5 version=0.3.6
author=Rob Tillaart <rob.tillaart@gmail.com> author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com> maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for ADS1015 - I2C 12 bit ADC and ADS1115 I2C 16 bit ADC sentence=Arduino library for ADS1015 - I2C 12 bit ADC and ADS1115 I2C 16 bit ADC