Crate ads1x1x

source ·
Expand description

This is a platform-agnostic Rust driver for the ADS1013, ADS1014, ADS1015, ADS1113, ADS1114, and ADS1115 ultra-small, low-power analog-to-digital converters (ADC), based on the embedded-hal traits.

This driver allows you to:

The devices

The devices are precision, low power, 12/16-bit analog-to-digital converters (ADC) that provide all features necessary to measure the most common sensor signals in an ultra-small package. Depending on the device, these integrate a programmable gain amplifier (PGA), voltage reference, oscillator and high-accuracy temperature sensor.

The devices can perform conversions at data rates up to 3300 samples per second (SPS). The PGA offers input ranges from ±256 mV to ±6.144 V, allowing both large and small signals to be measured with high resolution. An input multiplexer (MUX) allows to measure two differential or four single-ended inputs. The high-accuracy temperature sensor can be used for system-level temperature monitoring or cold-junction compensation for thermocouples.

The devices operate either in continuous-conversion mode, or in a single-shot mode that automatically powers down after a conversion. Single-shot mode significantly reduces current consumption during idle periods. Data is transferred through I2C.

Here is a comparison of the caracteristics of the devices:

DeviceResolutionSample RateChannelsMulti-channelFeatures
ADS101312-bitMax 3300 SPS1N/A
ADS101412-bitMax 3300 SPS1N/AComparator, PGA
ADS101512-bitMax 3300 SPS4MultiplexedComparator, PGA
ADS111316-bitMax 860 SPS1N/A
ADS111416-bitMax 860 SPS1N/AComparator, PGA
ADS111516-bitMax 860 SPS4MultiplexedComparator, PGA

Datasheets:

Usage examples (see also examples folder)

To use this driver, import this crate and an embedded_hal implementation, then instantiate the appropriate device. In the following examples an instance of the device ADS1013 will be created as an example. Other devices can be created with similar methods like: Ads1x1x::new_ads1114(...).

Create a driver instance for the ADS1013

extern crate linux_embedded_hal as hal;
extern crate ads1x1x;
use ads1x1x::{ Ads1x1x, SlaveAddr };

let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let adc = Ads1x1x::new_ads1013(dev, address);
// do something...

// get the I2C device back
let dev = adc.destroy_ads1013();

Create a driver instance for the ADS1013 with an alternative address

extern crate linux_embedded_hal as hal;
extern crate ads1x1x;
use ads1x1x::{ Ads1x1x, SlaveAddr };

let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let (a1, a0) = (true, false);
let address = SlaveAddr::Alternative(a1, a0);
let adc = Ads1x1x::new_ads1013(dev, address);

Make a one-shot measurement

extern crate embedded_hal;
use embedded_hal::adc::OneShot;
extern crate linux_embedded_hal;
#[macro_use(block)]
extern crate nb;
extern crate ads1x1x;

use linux_embedded_hal::I2cdev;
use ads1x1x::{ Ads1x1x, SlaveAddr, channel };

    let dev = I2cdev::new("/dev/i2c-1").unwrap();
    let mut adc = Ads1x1x::new_ads1013(dev, SlaveAddr::default());
    let measurement = block!(adc.read(&mut channel::DifferentialA0A1)).unwrap();
    println!("Measurement: {}", measurement);
    let _dev = adc.destroy_ads1013(); // get I2C device back

Change into continuous conversion mode and read the last measurement

extern crate linux_embedded_hal as hal;
extern crate ads1x1x;
use ads1x1x::{ Ads1x1x, SlaveAddr };

let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let adc = Ads1x1x::new_ads1013(dev, address);
let mut adc = adc.into_continuous().unwrap();
adc.start().unwrap();
while(adc.is_measurement_in_progress().unwrap()) {
    // some delay...
}
let measurement = adc.read().unwrap();

Set the data rate

For 12-bit devices, the available data rates are given by DataRate12Bit. For 16-bit devices, the available data rates are given by DataRate16Bit.

extern crate linux_embedded_hal as hal;
extern crate ads1x1x;
use ads1x1x::{ Ads1x1x, SlaveAddr, DataRate16Bit };

let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let mut adc = Ads1x1x::new_ads1115(dev, address);
adc.set_data_rate(DataRate16Bit::Sps860).unwrap();

Configure the comparator

Configure the comparator to assert when the voltage drops below -1.5V or goes above 1.5V in at least two consecutive conversions. Then the ALERT/RDY pin will be set high and it will be kept so until the measurement is read or an appropriate SMBus alert response is sent by the master.

extern crate linux_embedded_hal as hal;
extern crate ads1x1x;
use ads1x1x::{ Ads1x1x, SlaveAddr, ComparatorQueue, ComparatorPolarity,
               ComparatorMode, ComparatorLatching, FullScaleRange };

let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let mut adc = Ads1x1x::new_ads1015(dev, address);
adc.set_comparator_queue(ComparatorQueue::Two).unwrap();
adc.set_comparator_polarity(ComparatorPolarity::ActiveHigh).unwrap();
adc.set_comparator_mode(ComparatorMode::Window).unwrap();
adc.set_full_scale_range(FullScaleRange::Within2_048V).unwrap();
adc.set_low_threshold_raw(-1500).unwrap();
adc.set_high_threshold_raw(1500).unwrap();
adc.set_comparator_latching(ComparatorLatching::Latching).unwrap();

Modules

ADC input channel selection
Mode marker types

Structs

ADS1x1x ADC driver

Enums

Comparator polarity (only for ADS1x14, ADS1x15)
Comparator mode (only for ADS1x14, ADS1x15)
Comparator polarity (only for ADS1x14, ADS1x15)
Comparator alert queue (only for ADS1x14, ADS1x15)
Data rate for ADS1013, ADS1014, ADS1015
Data rate for ADS1113, ADS1114, ADS1115
All possible errors in this crate
Full-scale range configuration for the programmable gain amplifier (PGA) (only for ADS1x14, ADS1x15)
Possible slave addresses