Crate ads1x1x[][src]

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(...).

Please find additional examples using hardware in this repository: driver-examples

Create a driver instance for the ADS1013

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

let dev = 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 (method 1)

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

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let (bit1, bit0) = (true, false); // last two bits of address
let address = SlaveAddr::Alternative(bit1, bit0);
let adc = Ads1x1x::new_ads1013(dev, address);

Create a driver instance for the ADS1013 with an alternative address (method 2)

Using helper SlaveAddr creation method depending on the connection of the ADDR pin.

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

let dev = I2cdev::new("/dev/i2c-1").unwrap();
// `ADDR` pin connected to SDA results in the 0x4A effective address
let address = SlaveAddr::new_sda();
let adc = Ads1x1x::new_ads1013(dev, address);

Make a one-shot measurement

use ads1x1x::{channel, Ads1x1x, SlaveAddr};
use embedded_hal::adc::OneShot;
use linux_embedded_hal::I2cdev;
use nb::block;

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

Changing the mode may fail in case there was a communication error. In this case, you can retrieve the unchanged device from the error type.

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

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let adc = Ads1x1x::new_ads1013(dev, address);
match adc.into_continuous() {
    Err(ModeChangeError::I2C(e, adc)) => /* mode change failed handling */ panic!(),
    Ok(mut adc) => {
        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.

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

let dev = 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.

use linux_embedded_hal::I2cdev;
use ads1x1x::{
    Ads1x1x, SlaveAddr, ComparatorQueue, ComparatorPolarity,
    ComparatorMode, ComparatorLatching, FullScaleRange
};

let dev = 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

ADC input channel selection

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

Errors in this crate

Full-scale range configuration for the programmable gain amplifier (PGA) (only for ADS1x14, ADS1x15)

Error type for mode changes.

Possible slave addresses

Traits

Multi channel One-shot ADC