0.3.4 ADS1x15

This commit is contained in:
rob tillaart 2021-12-11 16:46:06 +01:00
parent 389d0834a5
commit 21615207a0
25 changed files with 291 additions and 30 deletions

View File

@ -1,7 +1,7 @@
//
// FILE: ADS1X15.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.3
// VERSION: 0.3.4
// DATE: 2013-03-24
// PUPROSE: Arduino library for ADS1015 and ADS1115
// URL: https://github.com/RobTillaart/ADS1X15
@ -22,6 +22,8 @@
// 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.
#include "ADS1X15.h"
@ -102,7 +104,7 @@ differs for different devices, check datasheet or readme.md
#define ADS1X15_COMP_QUE_1_CONV 0x0000 // trigger alert after 1 convert
#define ADS1X15_COMP_QUE_2_CONV 0x0001 // trigger alert after 2 converts
#define ADS1X15_COMP_QUE_4_CONV 0x0002 // trigger alert after 4 converts
#define ADS1X15_COMP_QUE_NONE 0x0003 // dosable comparator
#define ADS1X15_COMP_QUE_NONE 0x0003 // disable comparator
// _CONFIG masks
@ -230,21 +232,21 @@ uint8_t ADS1X15::getGain()
}
float ADS1X15::toVoltage(int16_t val)
float ADS1X15::toVoltage(int16_t value)
{
if (val == 0) return 0;
if (value == 0) return 0;
float volts = getMaxVoltage();
if (volts < 0) return volts;
volts *= val;
volts *= value;
if (_config & ADS_CONF_RES_16)
{
volts /= 32767; // val = 16 bits - sign bit = 15 bits mantissa
volts /= 32767; // value = 16 bits - sign bit = 15 bits mantissa
}
else
{
volts /= 2047; // val = 12 bits - sign bit = 11 bit mantissa
volts /= 2047; // value = 12 bits - sign bit = 11 bit mantissa
}
return volts;
}
@ -418,6 +420,7 @@ int16_t ADS1X15::_readADC(uint16_t readmode)
return getValue();
}
void ADS1X15::_requestADC(uint16_t readmode)
{
// write to register is needed in continuous mode as other flags can be changed
@ -436,6 +439,7 @@ void ADS1X15::_requestADC(uint16_t readmode)
_writeRegister(_address, ADS1X15_REG_CONFIG, config);
}
bool ADS1X15::_writeRegister(uint8_t address, uint8_t reg, uint16_t value)
{
_wire->beginTransmission(address);
@ -445,6 +449,7 @@ bool ADS1X15::_writeRegister(uint8_t address, uint8_t reg, uint16_t value)
return (_wire->endTransmission() == 0);
}
uint16_t ADS1X15::_readRegister(uint8_t address, uint8_t reg)
{
_wire->beginTransmission(address);
@ -506,43 +511,49 @@ ADS1015::ADS1015(uint8_t address, TwoWire *wire)
_maxPorts = 4;
}
int16_t ADS1015::readADC_Differential_0_3()
{
return _readADC(ADS1X15_MUX_DIFF_0_3);
}
int16_t ADS1015::readADC_Differential_1_3()
{
return _readADC(ADS1X15_MUX_DIFF_1_3);
}
int16_t ADS1015::readADC_Differential_2_3()
{
return _readADC(ADS1X15_MUX_DIFF_2_3);
}
int16_t ADS1015::readADC_Differential_0_2()
{
return readADC(2) - readADC(0);
}
int16_t ADS1015::readADC_Differential_1_2()
{
return readADC(2) - readADC(1);;
}
void ADS1015::requestADC_Differential_0_3()
{
_requestADC(ADS1X15_MUX_DIFF_0_3);
}
void ADS1015::requestADC_Differential_1_3()
{
_requestADC(ADS1X15_MUX_DIFF_1_3);
}
void ADS1015::requestADC_Differential_2_3()
{
_requestADC(ADS1X15_MUX_DIFF_2_3);
@ -593,44 +604,54 @@ ADS1115::ADS1115(uint8_t address, TwoWire *wire)
_maxPorts = 4;
}
int16_t ADS1115::readADC_Differential_0_3()
{
return _readADC(ADS1X15_MUX_DIFF_0_3);
}
int16_t ADS1115::readADC_Differential_1_3()
{
return _readADC(ADS1X15_MUX_DIFF_1_3);
}
int16_t ADS1115::readADC_Differential_2_3()
{
return _readADC(ADS1X15_MUX_DIFF_2_3);
}
int16_t ADS1115::readADC_Differential_0_2()
{
return readADC(2) - readADC(0);
}
int16_t ADS1115::readADC_Differential_1_2()
{
return readADC(2) - readADC(1);;
}
void ADS1115::requestADC_Differential_0_3()
{
_requestADC(ADS1X15_MUX_DIFF_0_3);
}
void ADS1115::requestADC_Differential_1_3()
{
_requestADC(ADS1X15_MUX_DIFF_1_3);
}
void ADS1115::requestADC_Differential_2_3()
{
_requestADC(ADS1X15_MUX_DIFF_2_3);
}
// --- END OF FILE

View File

@ -2,7 +2,7 @@
//
// FILE: ADS1X15.H
// AUTHOR: Rob Tillaart
// VERSION: 0.3.3
// VERSION: 0.3.4
// 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.3"))
#define ADS1X15_LIB_VERSION (F("0.3.4"))
// allow compile time default address
// address in { 0x48, 0x49, 0x4A, 0x4B }, no test...
@ -53,7 +53,7 @@ public:
uint8_t getGain(); // 0xFF == invalid gain error.
// both may return ADS1X15_INVALID_VOLTAGE if the gain is invalid.
float toVoltage(int16_t val = 1); // converts raw to voltage
float toVoltage(int16_t value = 1); // converts raw to voltage
float getMaxVoltage(); // -100 == invalid voltage error
// 0 = CONTINUOUS

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2013-2021 Rob Tillaart
Copyright (c) 2013-2022 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -7,7 +7,7 @@
# ADS1X15
Arduino library for I2C ADC ADS1015, ADS1115,
Arduino library for I2C ADC ADS1015, ADS1115, and similar.
## Description
@ -31,7 +31,7 @@ differential measurement.
## Interface
The address of the ADS1113/4/5 is determined by to which pin the ADDR
The address of the ADS1113/4/5 is determined by to which pin the **ADDR**
is connected to:
| ADDR pin connected to | Address | Notes |
@ -42,7 +42,7 @@ is connected to:
| SCL | 0x4B | |
- **ADS1x15()** constructor, should not be used.
- **ADS1x15()** base constructor, should not be used.
- **ADS1013(address, TwoWire \*wire = &Wire)** Constructor with device address,
and optional the Wire interface as parameter.
- **ADS1014(address, TwoWire \*wire = &Wire)** Constructor with device address,
@ -170,7 +170,7 @@ in terms of code
{
if (ADS.isReady())
{
val = ADS.getValue();
value = ADS.getValue();
ADS.requestADC(pin); // request new conversion
}
// do other things here
@ -183,6 +183,7 @@ See examples
## ReadADC Differential
For reading the ADC in a differential way there are 4 calls possible.
- **int16_t readADC_Differential_0_1()** returns the difference between 2 ADC pins.
- **int16_t readADC_Differential_0_3()** ADS1x15 only
- **int16_t readADC_Differential_1_3()** ADS1x15 only
@ -191,6 +192,7 @@ For reading the ADC in a differential way there are 4 calls possible.
- **int16_t readADC_Differential_1_2()** ADS1x15 only - in software (no async equivalent)
The differential reading of the ADC can also be done with asynchronous calls.
- **void requestADC_Differential_0_1()** starts conversion for differential reading
- **void requestADC_Differential_0_3()** ADS1x15 only
- **void requestADC_Differential_1_3()** ADS1x15 only
@ -199,8 +201,8 @@ The differential reading of the ADC can also be done with asynchronous calls.
After one of these calls one need to call
- **int16_t getValue()** Read the result of the last conversion.
The readiness of a CONTINUOUS conversion can only be detected by the RDY line.
Use interrupt for this, see examples.
The readiness of a CONTINUOUS conversion can only be detected by the **RDY** line.
Best to use an interrupt for this, see examples.
#### ReadADC continuous mode
@ -226,6 +228,7 @@ See examples.
If the thresholdHigh is set to 0x0100 and the thresholdLow to 0x0000
the **ALERT/RDY** pin is triggered when a conversion is ready.
- **void setComparatorThresholdLow(int16_t lo)** writes value to device directly.
- **void setComparatorThresholdHigh(int16_t hi)** writes value to device directly.
- **int16_t getComparatorThresholdLow()** reads value from device.
@ -304,6 +307,7 @@ A value of 3 (or above) effectively disables the comparator. See table below.
Depending on the comparator mode **TRADITIONAL** or **WINDOW** the thresholds registers
mean something different see - Comparator Mode above or datasheet.
- **void setComparatorThresholdLow(int16_t lo)** set the low threshold; take care the hi >= lo.
- **void setComparatorThresholdHigh(int16_t hi)** set the high threshold; take care the hi >= lo.
- **int16_t getComparatorThresholdLow()** reads value from device.

View File

@ -22,6 +22,7 @@ int idx = 0;
uint32_t last = 0, now = 0;
void setup()
{
Serial.begin(115200);
@ -116,4 +117,6 @@ void ADS_print_all()
Serial.println();
}
// -- END OF FILE --

View File

@ -28,6 +28,7 @@ int idx = 0;
uint32_t lastTime = 0;
void setup()
{
Serial.begin(115200);
@ -133,4 +134,6 @@ void ADS_print_all()
// Serial.println();
}
// -- END OF FILE --

View File

@ -18,6 +18,7 @@
#include "ADS1X15.h"
// choose you sensor
// ADS1013 ADS(0x48);
// ADS1014 ADS(0x48);
@ -51,6 +52,7 @@ void setup()
ADS.requestADC_Differential_0_1();
}
void loop()
{
if (handleConversion() == true)
@ -92,4 +94,6 @@ bool handleConversion()
return false; // default not all read
}
// -- END OF FILE --

View File

@ -16,6 +16,7 @@
#include "ADS1X15.h"
// choose you sensor
// ADS1013 ADS(0x48);
// ADS1014 ADS(0x48);
@ -39,9 +40,13 @@ void setup()
ADS.readADC(0); // first read to trigger
}
void loop()
{
Serial.println(ADS.getValue());
// optional other code here
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -18,8 +18,10 @@
// so one can see these as references in the output.
//
#include "ADS1X15.h"
// choose you sensor
// ADS1013 ADS(0x48);
// ADS1014 ADS(0x48);
@ -32,6 +34,7 @@ volatile bool RDY = false;
uint8_t channel = 0;
int16_t val[4] = { 0, 0, 0, 0 };
void setup()
{
Serial.begin(115200);
@ -59,6 +62,7 @@ void setup()
ADS.readADC(channel); // trigger first read
}
void loop()
{
handleConversion();
@ -73,11 +77,15 @@ void loop()
delay(100);
}
// interrupt service routine
// kept as minimal as possible
void adsReady()
{
RDY = true;
}
void handleConversion()
{
if (RDY)
@ -91,4 +99,7 @@ void handleConversion()
RDY = false;
}
}
// -- END OF FILE --

View File

@ -18,18 +18,25 @@
// so one can see these as references in the output.
//
#include "ADS1X15.h"
// adjust addresses if needed
ADS1115 ADS_1(0x49);
ADS1115 ADS_2(0x48);
// two interrupt flags
volatile bool RDY_1 = false;
volatile bool RDY_2 = false;
uint8_t channel_1 = 0;
uint8_t channel_2 = 0;
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 };
void setup()
{
Serial.begin(115200);
@ -73,6 +80,7 @@ void setup()
ADS_2.readADC(channel_2); // trigger first read
}
void loop()
{
handleConversion();
@ -87,17 +95,20 @@ void loop()
delay(100);
}
// catch interrupt and set flag
// 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
void handleConversion()
{
@ -123,4 +134,6 @@ void handleConversion()
}
}
// -- END OF FILE --

View File

@ -18,12 +18,14 @@
#include "ADS1X15.h"
// choose you sensor
// ADS1013 ADS(0x48);
// ADS1014 ADS(0x48);
// ADS1015 ADS(0x48);
// ADS1113 ADS(0x48);
// ADS1114 ADS(0x48);
ADS1115 ADS(0x48);
@ -87,12 +89,14 @@ void loop()
// delay(10);
}
// interrupt handler, just 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.
bool handleConversion()
{
@ -121,3 +125,4 @@ bool handleConversion()
// -- END OF FILE --

View File

@ -26,10 +26,12 @@
// measure at x and y (connect to AIN0 and AIN1).
// range from -VDD .. +VDD are possible
#include <ADS1X15.h>
ADS1115 ADS(0x48);
void setup()
{
Serial.begin(115200);
@ -41,6 +43,7 @@ void setup()
ADS.setGain(0);
}
void loop()
{
int16_t val_01 = ADS.readADC_Differential_0_1();
@ -61,4 +64,6 @@ void loop()
delay(1000);
}
// -- END OF FILE --

View File

@ -0,0 +1,124 @@
//
// FILE: ADS_high_speed_differential.ino.ino
// AUTHOR: Rob.Tillaart
// VERSION: 0.1.0
// PURPOSE: read from 2 IC's for high speed differential
// interrupt driven to catch all conversions.
//
// test setup (not tested yet)
// - connect 2 ADS1x15 to I2C bus
// - connect potmeters to all channels
// - code reads both at the same frequency
// and calculates differential per pair.
// as 2 ADC's go in parallel, two ADS1015 should get
// 3000+ differential samples / second.
//
#include "ADS1X15.h"
// adjust addresses if needed
ADS1115 ADS_1(0x49);
ADS1115 ADS_2(0x48);
volatile bool RDY_1 = false;
volatile bool RDY_2 = false;
uint8_t channel = 0;
int32_t differential[4] = { 0, 0, 0, 0 };
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("ADS1X15_LIB_VERSION: ");
Serial.println(ADS1X15_LIB_VERSION);
// SETUP FIRST ADS1115
ADS_1.begin();
ADS_1.setGain(0); // 6.144 volt
ADS_1.setDataRate(7); // fastest conversion rate.
// 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); // continuous mode
ADS_1.readADC(channel); // trigger first read
// SETUP SECOND ADS1115
ADS_2.begin();
ADS_2.setGain(0); // 6.144 volt
ADS_2.setDataRate(7);
// 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); // continuous mode
ADS_2.readADC(channel); // trigger first read
}
void loop()
{
if (handleConversion() == true)
{
for (int i = 0; i < 4; i++)
{
Serial.print(differential[i]);
Serial.print("\t");
}
Serial.println();
}
}
// catch interrupt and set flag
void adsReady_1()
{
RDY_1 = true;
}
void adsReady_2()
{
RDY_2 = true;
}
// handle conversions if both are ready
bool handleConversion()
{
if (RDY_1 == false) return false;
if (RDY_2 == false) return false;
// read the value of both
int16_t a = ADS_1.getValue();
int16_t b = ADS_2.getValue();
differential[channel] = a - b;
// request next channel
channel++;
if (channel >= 4) channel = 0;
ADS_1.readADC(channel);
ADS_2.readADC(channel);
RDY_1 = false;
RDY_2 = false;
return true;
}
// -- END OF FILE --

View File

@ -15,16 +15,20 @@
// view with Serial Plotter
#include "ADS1X15.h"
// choose you sensor
// ADS1013 ADS(0x48);
// ADS1014 ADS(0x48);
// ADS1015 ADS(0x48);
// ADS1113 ADS(0x48);
// ADS1114 ADS(0x48);
ADS1115 ADS(0x48);
void setup()
{
Serial.begin(115200);
@ -37,10 +41,13 @@ void setup()
Serial.println("Voltage");
}
void loop()
{
int16_t raw = ADS.readADC(0);
Serial.println(ADS.toVoltage(raw), 3);
}
// -- END OF FILE --

View File

@ -16,17 +16,20 @@
#include "ADS1X15.h"
// choose you sensor
// ADS1013 ADS(0x48);
// ADS1014 ADS(0x48);
// ADS1015 ADS(0x48);
// ADS1113 ADS(0x48);
// ADS1114 ADS(0x48);
ADS1115 ADS(0x48);
uint32_t start, d1, d2;
int x;
void setup()
{
Serial.begin(115200);
@ -53,10 +56,12 @@ void setup()
Serial.println("\nDone...");
}
void loop()
{
}
void test_single_shot()
{
Serial.print(__FUNCTION__);
@ -73,6 +78,7 @@ void test_single_shot()
Serial.println(d1);
}
void test_continuous()
{
Serial.print(__FUNCTION__);
@ -89,4 +95,6 @@ void test_continuous()
Serial.println(d2);
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -14,10 +14,12 @@
// measure at x (connect to AIN0).
//
#include "ADS1X15.h"
ADS1115 ADS(0x48);
void setup()
{
Serial.begin(115200);
@ -28,6 +30,7 @@ void setup()
ADS.begin();
}
void loop()
{
ADS.setGain(0);
@ -48,4 +51,6 @@ void loop()
delay(1000);
}
// -- END OF FILE --

View File

@ -19,10 +19,12 @@
// The RDY pin (or ALERT Pin) is triggered when conversion is ready
//
#include "ADS1X15.h"
ADS1115 ADS(0x48);
void setup()
{
Serial.begin(115200);
@ -43,6 +45,7 @@ void setup()
ADS.setComparatorLatch(0);
}
void loop()
{
ADS.setGain(0);
@ -59,4 +62,6 @@ void loop()
delay(1000);
}
// -- END OF FILE --

View File

@ -14,11 +14,13 @@
// measure at x (connect to AIN0).
//
#include "ADS1X15.h"
ADS1115 ADS(0x48);
float f = 0;
void setup()
{
Serial.begin(115200);
@ -32,6 +34,7 @@ void setup()
ADS.requestADC(0);
}
void loop()
{
if (ADS.isBusy() == false)
@ -47,4 +50,6 @@ void loop()
delay(2000);
}
// -- END OF FILE --

View File

@ -19,11 +19,13 @@
// The RDY pin (or ALERT Pin) is triggered when conversion is ready
//
#include "ADS1X15.h"
ADS1115 ADS(0x48);
float f = 0;
void setup()
{
Serial.begin(115200);
@ -44,6 +46,7 @@ void setup()
ADS.setComparatorLatch(0);
}
void loop()
{
if (ADS.isReady())
@ -59,4 +62,6 @@ void loop()
delay(2000);
}
// -- END OF FILE --

View File

@ -25,6 +25,7 @@
ADS1115 ADS(0x48);
void setup()
{
Serial.begin(115200);
@ -55,9 +56,9 @@ void setup()
Serial.println(ADS.getComparatorThresholdLow());
Serial.println(ADS.getComparatorThresholdHigh());
}
void loop()
{
ADS.setGain(0);
@ -79,4 +80,6 @@ void loop()
delay(100);
}
// -- END OF FILE --

View File

@ -14,10 +14,12 @@
// measure at x (connect to AIN0).
//
#include "ADS1X15.h"
ADS1115 ADS(0x48);
void setup()
{
Serial.begin(115200);
@ -39,6 +41,7 @@ void setup()
Serial.println();
}
void loop()
{
ADS.setGain(0);
@ -59,4 +62,6 @@ void loop()
delay(1000);
}
// -- END OF FILE --

View File

@ -1,6 +1,6 @@
# Syntax Coloring Map For ADS1X15
# Syntax Colouring Map For ADS1X15
# Datatypes (KEYWORD1)
# Data types (KEYWORD1)
ADS1X13 KEYWORD1
ADS1014 KEYWORD1
ADS1015 KEYWORD1
@ -61,6 +61,7 @@ requestADC_Differential_2_3 KEYWORD2
# Constants (LITERAL1)
ADS1X15_LIB_VERSION LITERAL1
ADS1X15_INVALID_VOLTAGE LITERAL1
ADS1X15_INVALID_GAIN LITERAL1
ADS1X15_INVALID_MODE LITERAL1

View File

@ -15,8 +15,9 @@
"type": "git",
"url": "https://github.com/RobTillaart/ADS1X15"
},
"version": "0.3.3",
"version": "0.3.4",
"license": "MIT",
"frameworks": "*",
"platforms": "*"
"platforms": "*",
"headers": "ADS1X15.h"
}

View File

@ -1,5 +1,5 @@
name=ADS1X15
version=0.3.3
version=0.3.4
author=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

View File

@ -29,10 +29,25 @@ unittest_setup()
{
}
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);
assertEqual(-100, ADS1X15_INVALID_VOLTAGE);
assertEqual(0xFF, ADS1X15_INVALID_GAIN);
assertEqual(0xFE, ADS1X15_INVALID_MODE);
}
unittest(test_begin)
{
ADS1115 ADS(0x48);
@ -41,6 +56,7 @@ unittest(test_begin)
assertTrue(ADS.isBusy());
}
unittest(test_gain)
{
ADS1115 ADS(0x48);
@ -58,6 +74,7 @@ unittest(test_gain)
assertEqual(0, ADS.getGain());
}
unittest(test_Voltage)
{
ADS1115 ADS(0x48);
@ -78,4 +95,5 @@ unittest(test_Voltage)
unittest_main()
// --------