• Stars
    star
    110
  • Rank 316,770 (Top 7 %)
  • Language
    C++
  • License
    MIT License
  • Created over 4 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Arduino library for ADS1015 = I2C 12 bit ADC and ADS1115 = I2C 16 bit ADC

Arduino CI Arduino-lint JSON check GitHub issues

License: MIT GitHub release PlatformIO Registry

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 ADS1015 and the ADS1115 are both 4 channels these are the most interesting from functionality point of view as these can also do differential measurements.

I2C Address

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 a constructor as described below.

  • ADS1x15() base constructor, should not be used.
  • ADS1013(uint8_t address, TwoWire *wire = &Wire) Constructor with device address, and optional the Wire interface as parameter.
  • ADS1014(uint8_t address, TwoWire *wire = &Wire) Constructor with device address, and optional the Wire interface as parameter.
  • ADS1015(uint8_t address, TwoWire *wire = &Wire) Constructor with device address, and optional the Wire interface as parameter.
  • ADS1113(uint8_t address, TwoWire *wire = &Wire) Constructor with device address, and optional the Wire interface as parameter.
  • ADS1114(uint8_t address, TwoWire *wire = &Wire) Constructor with device address, and optional the Wire interface as parameter.
  • ADS1115(uint8_t address, TwoWire *wire = &Wire) Constructor with device address, and optional the Wire interface as parameter.

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

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 - arduino/Arduino#11457

Question: Should this functionality be in this library?

Programmable Gain

  • void setGain(uint8_t gain) set the gain value, indicating the maxVoltage that can be measured Adjusting the gain allowing to make more precise measurements. Note: the gain is not set in the device until an explicit read/request of the ADC (any read call will do). See table below.
  • uint8_t getGain() returns the gain value (index).
PGA value Max Voltage Notes
0 Β±6.144V default
1 Β±4.096V
2 Β±2.048V
4 Β±1.024V
8 Β±0.512V
16 Β±0.256V
  • float getMaxVoltage() returns the max voltage with the current gain.
  • float toVoltage(int16_t raw = 1) converts a raw measurement to a voltage. Can be used for normal and differential measurements. The default value of 1 returns the conversion factor for any raw number.

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.

  • void setMode(uint8_t mode) 0 = CONTINUOUS, 1 = SINGLE (default) Note: the mode is not set in the device until an explicit read/request of the ADC (any read call will do).
  • uint8_t getMode() returns current mode 0 or 1, or ADS1X15_INVALID_MODE = 0xFE.

Data rate

  • void setDataRate(uint8_t dataRate) Data rate depends on type of device. For all devices the index 0..7 can be used, see table below. Values above 7 ==> will be set to the default 4. Note: the data rate is not set in the device until an explicit read/request of the ADC (any read call will do).
  • uint8_t getDataRate() returns the current data rate (index).

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.

  • 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.
// 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:

  • 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.

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.

  • 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
  • int16_t readADC_Differential_2_3() ADS1x15 only
  • int16_t readADC_Differential_0_2() ADS1x15 only - in software (no async equivalent)
  • int16_t readADC_Differential_1_2() ADS1x15 only - in software (no async equivalent)
// 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.

  • void requestADC_Differential_0_1() starts conversion for differential reading
  • void requestADC_Differential_0_3() ADS1x15 only
  • void requestADC_Differential_1_3() ADS1x15 only
  • void requestADC_Differential_2_3() ADS1x15 only

After one of these calls you need to call

  • int16_t getValue() Read the result of the last conversion.

See examples.

lastRequestMode

Since 0.3.12 the library tracks the last request mode, single pin or differential. This variable is set at the moment of request, and keeps its value until a new request is made. This implies that the value / request can be quite old.

Values >= 0x10 are differential, values < 0x10 are single pin.

  • uint8_t lastRequest() returns one of the values below.
Value Description Notes
0xFF no (invalid) request made after call constructor.
0x00 single pin 0
0x01 single pin 1
0x02 single pin 2
0x03 single pin 3
0x10 differential pin 1 0
0x30 differential pin 3 0
0x31 differential pin 3 1
0x32 differential pin 3 2

Please note that (for now) the function does not support a descriptive return value for the following two requests:

  • readADC_Differential_0_2() ADS1x15 only - in software (no async equivalent)
  • readADC_Differential_1_2() ADS1x15 only - in software (no async equivalent)

As these are emulated in software by two single pin calls, the state would be one of the two single pin values.

ReadADC continuous mode

To use the continuous mode you need call three functions:

  • void setMode(0) 0 = CONTINUOUS, 1 = SINGLE (default). Note: the mode is not set in the device until an explicit read/request of the ADC (any read call will do).
  • int16_t readADC(uint8_t pin) or void requestADC(uint8_t pin) to get the continuous mode started.
  • int16_t getValue() to return the last value read by the device. Note this can be a different pin, so be warned. Calling this over and over again can give the same value multiple times.
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 examples . By 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.

Switching mode or channel during continuous mode

When switching the operating mode or the ADC channel in continuous mode, be aware that the device will always finish the running conversion. This implies that after switching the mode or channel the first sample you get is probably the last sample with the previous settings, e.g. channel. This might be a problem for your project as this value can be in an "unexpected" range (outlier).

The robust way to change mode or channel therefore seems to be:

  1. stop continuous mode,
  2. wait for running conversion to be ready,
  3. reject the last conversion or process it "under old settings",
  4. change the settings,
  5. restart (continuous mode) with the new settings.

This explicit stop takes extra time, however it should prevent "incorrect" readings.

(need to be verified with different models)

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.

  • 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.
  • int16_t getComparatorThresholdHigh() reads value from device.

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.

  • void setComparatorMode(uint8_t mode) value 0 = TRADITIONAL 1 = WINDOW,
  • uint8_t getComparatorMode() returns value set.

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.

  • void setComparatorPolarity(uint8_t pol) Flag is only explicitly set after a readADC() or a requestADC()
  • uint8_t getComparatorPolarity() returns value set.

Latch

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

  • void setComparatorLatch(uint8_t latch) 0 = NO LATCH, not 0 = LATCH
  • uint8_t getComparatorLatch() returns value set.

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.

  • void setComparatorQueConvert(uint8_t mode) See table below.
  • uint8_t getComparatorQueConvert() returns value set.
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.

  • 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.
  • 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

  • More examples
  • SMB alert command (00011001) on I2C bus?
  • sync order .h / .cpp

Wont (unless requested)

  • type flag?
  • constructor for ADS1X15? No as all types are supported.

Support

If you appreciate my libraries, you can support the development and maintenance. Improve the quality of the libraries by providing issues and Pull Requests, or donate through PayPal or GitHub sponsors.

Thank you,

More Repositories

1

Arduino

Arduino libraries, code and applications
C++
1,361
star
2

AS5600

Arduino library for AS5600 magnetic rotation meter
C++
108
star
3

DHTNew

Arduino library for DHT11 and DHT22 with automatic sensor recognition
C++
84
star
4

PCF8574

Arduino library for PCF8574 - I2C IO expander
C++
77
star
5

ACS712

Arduino library for ACS Current Sensor - 5A, 20A, 30A
C++
74
star
6

CRC

CRC library for Arduino
C++
70
star
7

I2C_EEPROM

Library for I2C EEPROM - 24LC256
C++
59
star
8

PCF8575

Arduino library for PCF8575 - 16 channel I2C IO expander
C++
51
star
9

INA226

Arduino library for INA226 power sensor
C++
48
star
10

HX711

Arduino library for HX711 24 bit ADC used for load cells and scales.
C++
46
star
11

RS485

Arduino library for RS485, half duplex communication
C++
39
star
12

SHT31

Arduino library for the SHT31 temperature and humidity sensor
C++
34
star
13

RunningAverage

Arduino library to calculate the running average by means of a circular buffer.
C++
32
star
14

RunningMedian

Arduino library to determine the running median by means of a circular buffer.
C++
31
star
15

I2CKeyPad

Arduino libray for 4x4 (or smaller) KeyPad connected to an I2C PCF8574
C++
30
star
16

MCP_DAC

Arduino library for MCP_DAC MCP48xx and MCP49xx series SPI-DAC
C++
30
star
17

MCP23S17

Arduino library for SPI based MCP23S17 16 channel port expander
C++
27
star
18

FRAM_I2C

Arduino library for I2C FRAM
C++
22
star
19

INA219

Arduino library for INA219 voltage, current and power sensor
C++
22
star
20

DHT20

Arduino library for DHT20 I2C temperature and humidity sensor.
C++
22
star
21

Statistic

Statistic library for Arduino includes sum, average, variance and std deviation
C++
22
star
22

DS18B20_RT

Arduino library for the DS18B20 sensor - restricted to one sensor per pin.
C++
21
star
23

TCA9548

Arduino library for TCA9548 8 channel I2C multiplexer and compatibles.
C++
21
star
24

GY521

Arduino library for GY521 accelerometer- gyroscope a.k.a. MCU-6050
C++
20
star
25

MAX6675

Arduino library for MAX6675 chip for K type thermocouple
C++
19
star
26

SHT2x

Arduino library for the SHT2x series temperature and humidity sensors including SHT20, 21, 25.
C++
19
star
27

UUID

Arduino library for generating UUID strings.
C++
18
star
28

DHTlib

Arduino library for DHT temperature and humidity sensor. AVR optimized
C++
17
star
29

FastTrig

Arduino library with interpolated lookup for sin() and cos()
C++
17
star
30

float16

Arduino library to implement float16 data type
C++
16
star
31

MultiMap

Arduino library for fast non-linear mapping or interpolation of values
C++
16
star
32

AD9833

Arduino library for AD9833 function generator.
C++
15
star
33

DHTstable

Arduino Library for the DHT temperature and humidity sensor.
C++
14
star
34

TM1637_RT

TM1637 library for Arduino
C++
14
star
35

AGS02MA

Arduino library for AGS02MA TVOC sensor
C++
14
star
36

HT16K33

Arduino Library for HT16K33 4x7segment display
C++
14
star
37

HX711_MP

Arduino library for HX711 24 bit ADC used for load cells with multipoint calibration (MP).
C++
14
star
38

MCP4725

Arduino library for 12 bit I2C DAC - MCP4725
C++
13
star
39

MCP_ADC

Arduino library for MCP3001 MCP3002 MCP3004 MCP3008 MCP3201 MCP3202 MCP3204 MCP3208
C++
13
star
40

Max44009

Arduino library for I2C LUX sensor 22 bit, aka GY-49
C++
13
star
41

FastShiftOut

Arduino library for (AVR) optimized shiftOut - e.g. 74HC595
C++
12
star
42

Stopwatch_RT

Arduino Library implementing a stopwatch including minutes, seconds, milliseconds and microseconds
C++
12
star
43

I2C_SCANNER

Arduino library to implement an I2C scanner.
C++
12
star
44

ANSI

Arduino library with basic ANSI display codes for simple terminal apps
C++
12
star
45

INA3221_RT

Arduino library for the I2C INA3221 3 channel voltage and current sensor.
C++
12
star
46

fast_math

Arduino library for fast math algorithms
C++
11
star
47

I2CKeyPad8x8

Arduino library for 8x8 or smaller KeyPad connected to an I2C PCF8575.
C++
11
star
48

SHT85

Arduino library for the SHT85 temperature and humidity sensor
C++
11
star
49

rotaryDecoder

Arduino library for a PCF8574 based rotary decoder - supports 4 rotary encoders.
C++
11
star
50

MS5611

Arduino library for MS5611 temperature and pressure sensor
C++
11
star
51

MultiSpeedI2CScanner

Arduino I2C scanner that scans all addresses at multiple speeds.
C++
11
star
52

AD985X

Arduino library for AD9850 and AD9851 function generators.
C++
10
star
53

MT8870

Arduino library for MT8870 DTMF decoder (breakout)
C++
10
star
54

FastShiftIn

Arduino library for (AVR) optimized shiftIn - e.g. 74HC165
C++
10
star
55

BitArray

Arduino library for compact array of objects with a size expressed in bits. typically 1..10
C++
10
star
56

PCA9635

Arduino library for PCA9635 I2C 8 bit PWM LED driver - 16 channel.
C++
10
star
57

SGP30

Arduino library for SGP30 environment sensor
C++
10
star
58

MCP23008

Arduino library for I2C MCP23008 8 channel port expander
C++
10
star
59

DRV8825

Arduino library for DRV8825 stepper motor driver
C++
10
star
60

X9C10X

Arduino Library for X9C10X series digital potentiometer.
C++
10
star
61

TCA9555

Arduino library for I2C TCA9555 16 channel port expander
C++
10
star
62

ParallelPrinter

Arduino library that implements a parallel printer - print interface
C++
10
star
63

PID_RT

PID library for Arduino
C++
9
star
64

ADT7470

Arduino library for I2C ADT7470 Fan Monitoring
C++
9
star
65

FunctionGenerator

Arduino library to generate wave forms (nummeric) for a DAC
C++
9
star
66

map2colour

Arduino library for mapping a float to RGB colour spectrum
C++
9
star
67

AM232X

Arduino library for AM2320 AM2321 and AM2323 I2C temperature and humidity sensor
C++
9
star
68

I2C_24LC1025

Arduino library for the 24LC1025 I2C EEPROM and equivalent
C++
9
star
69

MAX31855_RT

Arduino library for MAX31855 chip for K type thermocouple
C++
9
star
70

I2C_LCD

Arduino library for I2C LCD displays e.g. 20x4
C++
9
star
71

MINMAX

Arduino library to find peaks in a signal
C++
8
star
72

DHTINT

Arduino library for DHT sensors - integer only
C++
8
star
73

MCP23017_RT

Arduino library for I2C MCP23017 16 channel port expander
C++
8
star
74

infiniteAverage

Arduino Library to calculate an average of many many samples
C++
8
star
75

DS18B20_INT

Arduino library for DS18B20 with minimal footprint. Whole degrees Celsius only.
C++
8
star
76

MiniMP3

Arduino library for DFRobotics MP3 player and compatibles.
C++
7
star
77

bitHelpers

Arduino library with static functions on bit level (a.k.a. bit hacks)
C++
7
star
78

AD520X

Arduino library for SPI AD5204 and AD5206 digital potentiometers
C++
7
star
79

PCA9634

Arduino library for PCA9634 I2C 8 bit PWM LED driver, 8 channel.
C++
7
star
80

AD5144A

Arduino library for I2C digital potentiometer AD5144A
C++
7
star
81

GAMMA

Arduino Library for the GAMMA function to adjust brightness of LED's etc.
C++
7
star
82

ML8511

Arduino library for ML8511 UV sensor
C++
7
star
83

Temperature

Arduino library with dewPoint humidex and heatIndex functions.
C++
7
star
84

printHelpers

Arduino library to help formatting data for printing
C++
7
star
85

SRF05

Arduino library for SRF05 distance sensor
C++
7
star
86

Kelvin2RGB

Arduino library for converting temperature to RGB values
C++
7
star
87

Complex

Arduino library for Complex math
C++
6
star
88

M62429

Arduino library for M62429 volume control IC
C++
6
star
89

CountDown

Arduino Library to implement a CountDown clock (in SW polling, no HW timer).
C++
6
star
90

Multiplex

Arduino library to multiplex streams
C++
6
star
91

AD524X

Arduino library for I2C digital potentiometer AD5241 AD5242
C++
6
star
92

PCA9685_RT

Arduino library for I2C PCA9685 16 channel PWM extender
C++
6
star
93

Correlation

Arduino Library to determine correlation between X and Y dataset
C++
6
star
94

DHT12

Arduino library for I2C DHT12 sensor
C++
6
star
95

FastShiftInOut

Arduino library for (AVR) optimized shiftInOut (simultaneously)
C++
6
star
96

AnalogPin

Arduino library to add functionality on top of analogRead()
C++
5
star
97

HC4052

Arduino library for HC4052 2x4 channel multiplexer and compatibles.
C++
5
star
98

MS5611_SPI

Arduino library (SPI) for MS5611 temperature and pressure sensor.
C++
5
star
99

TSL235R

Arduino library for the TSL235R light to frequency convertor
C++
5
star
100

ShiftOutSlow

Arduino library for shiftOut with build-in delay
C++
5
star