Crate ads1x1x

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 I²C.

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:

§Examples

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.

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

§Creating a Driver Instance for an ADS1013

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

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

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

§Creating a driver instance for an ADS1013 with the ADDR pin connected to SDA.

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

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let adc = Ads1x1x::new_ads1013(dev, TargetAddr::Sda);

§Taking a One-Shot Measurement

use ads1x1x::{channel, Ads1x1x, TargetAddr};
use linux_embedded_hal::I2cdev;
use nb::block;

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

§Changing to Continuous Conversion Mode and Reading 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, TargetAddr};

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let adc = Ads1x1x::new_ads1013(dev, TargetAddr::default());
match adc.into_continuous() {
    Err(ModeChangeError::I2C(e, adc)) => {
        panic!("Mode change failed: {e}")
    },
    Ok(mut adc) => {
        let measurement = adc.read().unwrap();
        // ...
    }
}

§Setting 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, TargetAddr};

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

§Configuring 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 controller.

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

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let address = TargetAddr::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();

Re-exports§

pub use channel::ChannelId;

Modules§

channel
ADC input channels.
mode
Mode marker types

Structs§

Ads1x1x
ADS1x1x ADC driver

Enums§

ComparatorLatching
Comparator latching (only for ADS1x14, ADS1x15).
ComparatorMode
Comparator mode (only for ADS1x14, ADS1x15).
ComparatorPolarity
Comparator polarity (only for ADS1x14, ADS1x15).
ComparatorQueue
Comparator alert queue (only for ADS1x14, ADS1x15).
DataRate12Bit
Data rate for ADS101x.
DataRate16Bit
Data rate for ADS111x.
Error
Errors in this crate
FullScaleRange
Full-scale range configuration for the programmable gain amplifier (PGA) (only for ADS1x14, ADS1x15).
ModeChangeError
Error type for mode changes.
TargetAddr
A target address.