Skip to main content

Crate lsm9ds0

Crate lsm9ds0 

Source
Expand description

Platform-agnostic async driver for the LSM9DS0 3D accelerometer, 3D gyroscope, and 3D magnetometer inertial module.

This driver supports both I2C and SPI communication interfaces and is built on embedded-hal-async traits.

Datasheet: LSM9DS0

§Quick Start

use lsm9ds0::{AccelDataRate, GyroDataRate, I2cInterface, Lsm9ds0, Lsm9ds0Config, MagMode};
// use embassy_time::Delay;  // or your platform's delay implementation

// Create interface and driver with default configuration
let interface = I2cInterface::init(i2c);

// Turn on the sensors (since they are powered down by default)
let config = Lsm9ds0Config::new()
    .with_gyro_enabled(true)
    .with_gyro_data_rate(GyroDataRate::Hz95)
    .with_accel_data_rate(AccelDataRate::Hz100)
    .with_mag_mode(MagMode::ContinuousConversion)
    .with_temperature_enabled(true);

let mut imu = Lsm9ds0::new_with_config(interface, config);

// Initialize the sensor (verifies device IDs and applies config)
imu.init(&mut Delay).await?;

// Read sensor data (all return typed wrappers, use .as_f32() or .into() for raw values)
let (gx, gy, gz) = imu.read_gyro().await?;  // DegreesPerSecond
let (ax, ay, az) = imu.read_accel().await?; // GForce
let (mx, my, mz) = imu.read_mag().await?;   // Gauss
let temp_c = imu.read_temp().await?;        // Celsius

§Custom Configuration

Use the builder pattern to configure sensors before initialization:

use lsm9ds0::{
    Lsm9ds0, Lsm9ds0Config, I2cInterface,
    GyroScale, GyroDataRate, AccelScale, AccelDataRate, MagScale, MagDataRate, MagMode,
};
// use embassy_time::Delay;  // or your platform's delay implementation

let config = Lsm9ds0Config::new()
    // Gyroscope: ±500 dps at 190 Hz
    .with_gyro_enabled(true)
    .with_gyro_scale(GyroScale::Dps500)
    .with_gyro_data_rate(GyroDataRate::Hz190)
    // Accelerometer: ±4g at 100 Hz
    .with_accel_scale(AccelScale::G4)
    .with_accel_data_rate(AccelDataRate::Hz100)
    // Magnetometer: ±8 gauss at 50 Hz, continuous mode
    .with_mag_scale(MagScale::Gauss8)
    .with_mag_data_rate(MagDataRate::Hz50)
    .with_mag_mode(MagMode::ContinuousConversion)
    // Enable temperature sensor and block data update
    .with_temperature_enabled(true)
    .with_block_data_update(true)
    .with_auto_calibration(lsm9ds0::Orientation::ZUp);

let interface = I2cInterface::init(i2c);
let mut imu = Lsm9ds0::new_with_config(interface, config);
imu.init(&mut Delay).await?;

§SPI Interface

For SPI, provide separate SpiDevice instances for the gyroscope and accelerometer/magnetometer (each managing its own chip-select):

use lsm9ds0::{Lsm9ds0, SpiInterface};
// use embassy_time::Delay;  // or your platform's delay implementation

// g_spi and xm_spi are SpiDevice instances (e.g., from embassy or embedded-hal-bus)
let interface = SpiInterface::init(g_spi, xm_spi);
let mut imu = Lsm9ds0::new(interface);
imu.init(&mut Delay).await?;

§Runtime Configuration

Settings can be changed after initialization:

use lsm9ds0::{GyroScale, AccelDataRate};

// Change gyroscope scale dynamically
imu.set_gyro_scale(GyroScale::Dps2000).await?;

// Change accelerometer data rate
imu.set_accel_data_rate(AccelDataRate::Hz400).await?;

§Interrupt Configuration

Configure motion detection interrupts:

use lsm9ds0::{Lsm9ds0Config, GyroDataRate};

// Simple gyroscope motion detection
let config = Lsm9ds0Config::new()
    .with_gyro_enabled(true)
    .with_gyro_data_rate(GyroDataRate::Hz190)
    .with_gyro_motion_threshold(500);  // Threshold in raw units

// Or configure accelerometer motion detection
let config = Lsm9ds0Config::new()
    .with_accel_motion_threshold(32);  // 32 * 16mg = ~0.5g threshold

Re-exports§

pub use errors::Error;
pub use registers::AccelBandwidth;
pub use registers::AccelDataRate;
pub use registers::AccelHpfMode;
pub use registers::AccelMagRegisters;
pub use registers::AccelScale;
pub use registers::AccelSelfTest;
pub use registers::ActiveLevel;
pub use registers::ActiveLevelInverted;
pub use registers::BlockDataUpdate;
pub use registers::ClickSign;
pub use registers::ClickSrc;
pub use registers::Enable;
pub use registers::Endianness;
pub use registers::FifoMode;
pub use registers::FifoSrcReg;
pub use registers::FifoSrcRegG;
pub use registers::GyroBandwidth;
pub use registers::GyroDataRate;
pub use registers::GyroHpfCutoff;
pub use registers::GyroHpfMode;
pub use registers::GyroOutputSel;
pub use registers::GyroPowerMode;
pub use registers::GyroRegisters;
pub use registers::GyroScale;
pub use registers::GyroSelfTest;
pub use registers::Int1SrcG;
pub use registers::IntGenSrc;
pub use registers::IntSrcRegM;
pub use registers::InterruptCombination;
pub use registers::LatchInterrupt;
pub use registers::MagDataRate;
pub use registers::MagLowPower;
pub use registers::MagMode;
pub use registers::MagResolution;
pub use registers::MagScale;
pub use registers::OutputType;
pub use registers::PowerMode;
pub use registers::SpiMode;
pub use registers::StatusRegA;
pub use registers::StatusRegG;
pub use registers::StatusRegM;
pub use registers::device_constants;
pub use config::*;
pub use interface::*;
pub use types::*;

Modules§

config
Configuration for the LSM9DS0 Accelerometer/Magnetometer/Gyroscope sensors
errors
Error types for the LSM9DS0 driver
interface
Interface trait
registers
LSM9DS0 Register Definitions
types
Sensor measurement types with unit semantics.

Structs§

Lsm9ds0
LSM9DS0 9-axis IMU driver.