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:
- Set the operating mode to one-shot or continuous. See:
into_continuous(). - Make a measurement in one-shot mode. See:
read(). - Start continuous conversion mode. See:
start(). - Read the last measurement made in continuous conversion mode. See:
read(). - Set the data rate. See:
set_data_rate(). - Set the full-scale range (gain amplifier). See
set_full_scale_range(). - Read whether a measurement is in progress. See:
is_measurement_in_progress(). - Set the ALERT/RDY pin to be used as conversion-ready pin. See:
use_alert_rdy_pin_as_ready(). - Comparator:
- Set the low and high thresholds. See:
set_high_threshold_raw(). - Set the comparator mode. See:
set_comparator_mode(). - Set the comparator polarity. See:
set_comparator_polarity(). - Set the comparator latching. See:
set_comparator_latching(). - Set the comparator queue. See:
set_comparator_queue(). - Disable the comparator. See:
disable_comparator().
- Set the low and high thresholds. See:
§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:
| Device | Resolution | Sample Rate | Channels | Multi-channel | Features |
|---|---|---|---|---|---|
| ADS1013 | 12-bit | Max 3300 SPS | 1 | N/A | |
| ADS1014 | 12-bit | Max 3300 SPS | 1 | N/A | Comparator, PGA |
| ADS1015 | 12-bit | Max 3300 SPS | 4 | Multiplexed | Comparator, PGA |
| ADS1113 | 16-bit | Max 860 SPS | 1 | N/A | |
| ADS1114 | 16-bit | Max 860 SPS | 1 | N/A | Comparator, PGA |
| ADS1115 | 16-bit | Max 860 SPS | 4 | Multiplexed | Comparator, 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§
Structs§
- Ads1x1x
- ADS1x1x ADC driver
Enums§
- Comparator
Latching - Comparator latching (only for ADS1x14, ADS1x15).
- Comparator
Mode - Comparator mode (only for ADS1x14, ADS1x15).
- Comparator
Polarity - Comparator polarity (only for ADS1x14, ADS1x15).
- Comparator
Queue - Comparator alert queue (only for ADS1x14, ADS1x15).
- Data
Rate12 Bit - Data rate for ADS101x.
- Data
Rate16 Bit - Data rate for ADS111x.
- Error
- Errors in this crate
- Full
Scale Range - Full-scale range configuration for the programmable gain amplifier (PGA) (only for ADS1x14, ADS1x15).
- Mode
Change Error - Error type for mode changes.
- Target
Addr - A target address.