Arduino library 路 Sensors

Adafruit ADS1X15: 16-bit analog inputs over I2C

The ADS1115 is a 16-bit analog to digital converter with four inputs, a programmable gain amplifier and an I2C interface, used when the board鈥檚 own 10 or 12-bit ADC is not fine enough or when a board, like the ESP8266, has only one analog pin. The Adafruit ADS1X15 library drives it and its 12-bit cousin, the ADS1015.

Built in on Mokxi: #include <Adafruit_ADS1X15.h> and it builds, with nothing to install. Free, and no account needed.

What Adafruit ADS1X15 does

The gain sets the input range, and that range is the most important choice in the sketch. GAIN_TWOTHIRDS, the default, covers plus or minus 6.144 V at 187.5 microvolts a count; GAIN_ONE covers 4.096 V, GAIN_TWO 2.048 V, down to GAIN_SIXTEEN at 0.256 V. The range is a measuring scale, not a permission: an input must never go below ground or above the supply.

Inputs can be read single-ended, each against ground, or as differential pairs (0 and 1, 2 and 3), which subtract out noise common to both wires and suit a bridge sensor or a shunt.

Library Manager nameAdafruit ADS1X15
AuthorAdafruit
Include#include <Adafruit_ADS1X15.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesSixteen-bit ADC, ADS1115

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <Adafruit_ADS1X15.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Adafruit ADS1X15" by Adafruit from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit ADS1X15 to lib_deps in PlatformIO. Mokxi follows the calls of release 2.5.0, so a sketch written here builds there too.

ESP32-C3 路 Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 ESP32-C6 路 Raspberry Pi Pico 路 Raspberry Pi Pico W 路 Seeed XIAO SAMD21 路 STM32 路 STM32 Nucleo-F411RE 路 STM32 Blue Pill 路 micro:bit V2 路 Arduino Uno R4 路 ESP32 DevKit 路 ESP32-S3-DevKitC-1 路 ESP8266 NodeMCU 路 Arduino Pro Mini 3.3 V 路 Seeed XIAO ESP32-C3 路 ESP32-C3 SuperMini 路 Seeed XIAO ESP32-C6 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 Arduino Nano RP2040 Connect 路 Seeed XIAO RP2040 路 Waveshare RP2040-Zero 路 Arduino Uno R4 WiFi

The calls you will use

In the order a sketch meets them.

Adafruit_ADS1115 ads

Creates the 16-bit converter. Adafruit_ADS1015 is the 12-bit one.

ads.setGain(GAIN_ONE)

Sets the range to plus or minus 4.096 V, 125 microvolts a count.

ads.begin()

Starts I2C at 0x48. begin(0x49) and so on for the other ADDR settings. Returns false when nothing answers.

int16_t adc0 = ads.readADC_SingleEnded(0)

Reads input A0 against ground, from 0 to 32767.

ads.computeVolts(adc0)

Converts a reading to volts using the current gain.

int16_t adc0 = ads.readADC_SingleEnded(0);
float volts0 = ads.computeVolts(adc0);
Serial.println(volts0, 4);

ads.readADC_Differential_0_1()

The difference between A0 and A1, which can be negative.

ads.startADCReading(ADS1X15_REG_CONFIG_MUX_SINGLE_0, true)

Starts continuous conversions on A0. Read each result with getLastConversionResults(), or wait for conversionComplete().

ads.setDataRate(RATE_ADS1115_860SPS)

Samples faster than the default 128 a second, with a little more noise.

Example: Current and voltage

On the ESP32-C3-DevKitM-1. It runs in the browser exactly as printed.

sketch.ino 路 ESP32-C3-DevKitM-1
// A load's current and its voltage, two ways: an INA219 at 0x40 and an
// ADS1115 at 0x48 on the ESP32-C3's I2C pins, GPIO 8 and 9.
//
// The INA219 is in series with the load: 3.3 V into VIN+, VIN- on to a 220
// ohm resistor to ground. Its 0.1 ohm shunt is a real resistor in the
// circuit, and the chip reads the few millivolts across it to 10 uV, and the
// voltage on the load side to 4 mV. The library writes the chip's calibration
// in begin(); without it the current register reads zero whatever flows.
//
// The ADS1115's A0 is on the same load-side node, so the two chips measure
// the same voltage: the INA219 in 4 mV steps, the ADS1115 at GAIN_ONE in
// 125 uV steps, a single-shot conversion taking 7.8 ms at the default rate.
//
// Change the resistor in the properties panel and watch both follow.

#include <Wire.h>
#include <Adafruit_INA219.h>
#include <Adafruit_ADS1X15.h>

Adafruit_INA219 ina219;
Adafruit_ADS1115 ads;

void setup() {
  Serial.begin(115200);
  if (!ina219.begin()) Serial.println("no INA219 at 0x40");
  ads.setGain(GAIN_ONE);
  if (!ads.begin()) Serial.println("no ADS1115 at 0x48");
}

void loop() {
  int16_t counts = ads.readADC_SingleEnded(0);
  Serial.print("load ");
  Serial.print(ina219.getCurrent_mA());
  Serial.print(" mA, ");
  Serial.print(ina219.getPower_mW());
  Serial.print(" mW   INA219 says ");
  Serial.print(ina219.getBusVoltage_V());
  Serial.print(" V, ADS1115 says ");
  Serial.print(ads.computeVolts(counts));
  Serial.print(" V (");
  Serial.print(counts);
  Serial.println(" counts)");
  delay(500);
}

Open it in the editor, wired and ready to run 路 Current and voltage: the project page

Common errors and how to fix them

Failed to initialize ADS.

begin() got no answer. Check SDA and SCL and the address: ADDR to GND is 0x48, to VDD 0x49, to SDA 0x4A, to SCL 0x4B.

The reading is stuck at 32767

The input is above the gain鈥檚 range, so the converter is at full scale. Choose a lower gain (GAIN_TWOTHIRDS is the widest) or divide the input voltage down.

computeVolts() gives the wrong voltage

setGain() was called after the reading, or a hand-written conversion assumes another gain. Set the gain once in setup() and let computeVolts() do the arithmetic.

Try Adafruit ADS1X15 without installing anything

Open the example, change the sketch, and keep your version in a free account.