Electrochemical Gas Sensor Library 1.0.0
A library for the Electrochemical Gas Sensor breakout board
Loading...
Searching...
No Matches
ADS1X15 Directory Reference

Files

 ADS1X15.cpp
 
 ADS1X15.h
 

Detailed Description

Arduino CI Arduino-lint JSON check License: MIT GitHub release

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.

Description

This library should work for the devices mentioned below, although not all sensors support all functionality.

Device Channels Resolution Max sps Comparator ProgGainAMP Notes
ADS1013 1 12 3300 N N
ADS1014 1 12 3300 Y Y
ADS1015 4 12 3300 Y Y
ADS1113 1 16 860 N N
ADS1114 1 16 860 Y Y
ADS1115 4 16 860 Y Y Tested

As the 1015 and the 1115 are both 4 channels these are the most interesting from functionality point of view as these can also do differential measurement.

The address of the ADS1113/4/5 is determined by to which pin the ADDR is connected to:

ADDR pin connected to Address Notes
GND 0x48 default
VDD 0x49
SDA 0x4A
SCL 0x4B

Interface

#include "ADS1X15.h"

Initializing

To initialize the library you must call constructor as described below.

After construction the ADS.begin() need to be called. This will return false if an invalid address is used. The function bool isConnected() can be used to verify the reading of the ADS. The function void reset() is sets the parameters to their initial value as in the constructor.

For example.

#include "ADS1X15.h"
// initialize ADS1115 on I2C bus 1 with default address 0x48
ADS1115 ADS(0x48);
void begin() {
if (!ADS.isConnected()) {
// error ADS1115 not connected
}
}
Definition ADS1X15.h:210

I2C clock speed

The function void setWireClock(uint32_t speed = 100000) is used to set the clock speed in Hz of the used I2C interface. typical value is 100 KHz.

The function uint32_t getWireClock() is a prototype. It returns the value set by setWireClock(). This is not necessary the actual value. When no value is set getWireClock() returns 0. Need to implement a read / calculate from low level I2C code (e.g. TWBR on AVR), better the Arduino Wire lib should support this call (ESP32 does).

See - https://github.com/arduino/Arduino/issues/11457

Question: should this functionality be in this library?

Programmable Gain

PGA value Max Voltage Notes
0 ±6.144V default
1 ±4.096V
2 ±2.048V
4 ±1.024V
8 ±0.512V
16 ±0.256V

The voltage factor can also be used to set HIGH and LOW threshold registers with a voltage in the comparator mode. Check the examples.

float f = ADS.toVoltage();
ADS.setComparatorThresholdLow( 3.0 / f );
ADS.setComparatorThresholdLow( 4.3 / f );

Operational mode

The ADS sensor can operate in single shot or continuous mode. Depending on how often conversions needed you can tune the mode.

Data rate

The library has no means to convert this index to the actual numbers as that would take 32 bytes.

Data rate in samples per second, based on datasheet is described on table below.

data rate ADS101x ADS111x Notes
0 128 8 slowest
1 250 16
2 490 32
3 920 64
4 1600 128 default
5 2400 250
6 3300 475
7 3300 860 fastest

ReadADC Single mode

Reading the ADC is very straightforward, the readADC() function handles all in one call. Under the hood it uses the asynchronous calls.

// read ADC in pin 2
ADS.readADC(2);
// read ADC in pin 0 - two ways
ADS.readADC();
ADS.readADC(0);

See examples.

To read the ADC in an asynchronous way (e.g. to minimize blocking) you need call three functions:

in terms of code

void setup()
{
// other setup things here
ADS.setMode(1); // SINGLE SHOT MODE
ADS.requestADC(pin);
}
void loop()
{
if (ADS.isReady())
{
value = ADS.getValue();
ADS.requestADC(pin); // request new conversion
}
// do other things here
}

See examples.

ReadADC Differential

For reading the ADC in a differential way there are 4 calls possible.

// read differential ADC between pin 0 and 1
ADS.readADC_Differential_0_1(0);

The differential reading of the ADC can also be done with asynchronous calls.

After one of these calls you need to call

See examples.

ReadADC continuous mode

To use the continuous mode you need call three functions:

void setup() {
// configuration things here
ADS.setMode(ADS.MODE_CONTINUOUS);
ADS.requestADC(0); // request on pin 0
}
void loop() {
value = ADS.getValue()
sleep(1)
}

See examplesBy using bool isBusy() or bool isReady() one can wait until new data is available. Note this only works in the SINGLE_SHOT modus.

In continuous mode, you can't use isBusy() or isReady() functions to wait until new data available. Instead you can configure the threshold registers to allow the ALERT/RDY pin to trigger an interrupt signal when conversion data ready.

Threshold registers

If the thresholdHigh is set to 0x0100 and the thresholdLow to 0x0000 the ALERT/RDY pin is triggered when a conversion is ready.

See examples.

Comparator

Please read Page 15 of the datasheet as the behaviour of the comparator is not trivial.

NOTE: all comparator settings are copied to the device only after calling readADC() or requestADC() functions.

Comparator Mode

When configured as a TRADITIONAL comparator, the ALERT/RDY pin asserts (active low by default) when conversion data exceed the limit set in the high threshold register. The comparator then de-asserts when the input signal falls below the low threshold register value.

If the comparator LATCH is set, the ALERT/RDY pin asserts and it will be reset after reading the sensor (conversion register) again. An SMB alert command (00011001) on the I2C bus will also reset the alert state. Not implemented in the library (yet)

In WINDOW comparator mode, the ALERT/RDY pin asserts if conversion data exceeds the high threshold register or falls below the low threshold register. In this mode the alert is held if the LATCH is set. This is similar as above.

Polarity

Default state of the ALERT/RDY pin is LOW, can be to set HIGH.

Latch

Holds the ALERT/RDY to HIGH (or LOW depending on polarity) after triggered even if actual value has been 'restored to normal' value.

QueConvert

Set the number of conversions before trigger activates. The void setComparatorQueConvert(uint8_t mode) is used to set the number of conversions that exceed the threshold before the ALERT/RDY pin is set HIGH. A value of 3 (or above) effectively disables the comparator. See table below.

value meaning Notes
0 trigger alert after 1 conversion
1 trigger alert after 2 conversions
2 trigger alert after 4 conversions
3 Disable comparator default

Threshold registers comparator mode

Depending on the comparator mode TRADITIONAL or WINDOW the thresholds registers mean something different see - Comparator Mode above or datasheet.

RP2040 specific

Future ideas & improvements

Must

Should

Could

Wont (unless requested)