Our offices will be closed Saturday December 23rd through January 1st for the Holidays. Dismiss

How to Interface Arduino to the LSM9DS0 Accelerometer

This simple guide shows you how to interface your favorite Arduino to the LSM9DS0 accelerometer sensor.  The LSM9DS0 is a combination of digital 3D accelerometer, gyroscope and magnetometer. This device has inbuilt interrupt signals on dedicated pins and is capable of motion and magnetic field detection. The working voltage of this device is 5V with 16-bit data output resolution. On its full-scale operation, acceleration goes from ±2g/±4g/±6g/±8g/±16g , magnetic field of scale ±2/±4/±8/±12 Gauss and angular rate of ±245/±500/±2000 dps.

Applications for the LSM9DS0

– Measuring orientation changes in aircraft and changing orientation in satellites and space vehicles.
– Auto rotation of the display view when a Phone is rotated
– Angular motion in a Mobile Game by 3D motion of the device
– RC Airplane Flight Dynamics and quad copter using Arduino
– Detect buried and submerged objects
– Gaming and virtual input devices

LSM9DS0 Sensor Overview

The LSM9DS0 is a system-in-package featuring a 3D digital linear acceleration sensor, a 3D digital angular rate sensor, and a 3D digital magnetic sensor. Magnetic, accelerometer and gyroscope sensing can be enabled or set in power-down mode separately for smart power management.  The LSM9DS0 it is guaranteed to operate over an extended temperature range from -40 °C to +85 °C.

Hardware Required

  1.  LSM9DS0 3D Accelerometer Gyroscope Magnetometer I2C Mini Module
  2. Arduino Nano
  3. I2C Shield for Arduino Nano

Note: We also have hardware for interfacing this sensor to just about any Arduino platform

Advantage of Choosing LSM9DS0 Sensor

The device is considered very helpful in designing prototypes of self-balanced robots because this device is a combination of 3 sensors in a single chip. We can calculate acceleration and angular velocity of any moving object with high accuracy. To detect magnetic fields, the in-built magnetometer helps mobile phones and other devices in detecting magnetic North.

Addressing the LSM9DS0

Integrated I2C address jumpers make it easy to connect up to 4 LSM9DS0 Mini Modules to a single I2C address buss.  The LSM9DS0 has a I2C Start Address of 0x1E, which changes depending on the I2C address jumper settings.

LSM9DS0 Address Jumpers

0x1E = Jumper 0 Removed, Jumper 1 Removed
0x1F = Jumper 0 Installed, Jumper 1 Removed
0x20 = Jumper 0 Removed, Jumper 1 Installed
ox21 = Jumper 0 Installed, Jumper 1 Installed

Register Description

The device has registers for Gyroscope (REG_GYRO_), Accelerometer(REG_ACCELMAG_), Temperature(REG_TEMP_) and Magnetometer(REG_MAG_) with address of every X,Y,Z  axis and other device configuration addresses. These addresses are selected by the programmer according to requirement like data rate selection, gyro scale etc.

LSM9DS0 Arduino Library

In  Arduino library we define address of every register, enabling commands, XYZ axis, high-low commands.

/**************************************************************************
    I2C ADDRESS/BITS
**************************************************************************/
    #define LSM9DS0_DEFAULT_ADDRESS_GYRO                    (0x6A)      // D4 >> 1 = 7-bit default
    #define LSM9DS0_ADDRESS_GYRO_UPDATED                    (0x6B)      // D6 >> 1 = 7-bit default
    #define LSM9DS0_DEFAULT_ADDRESS_ACCELMAG                (0x1E)      // 3C >> 1 = 7-bit default
    #define LSM9DS0_ADDRESS_ACCELMAG_UPDATED                (0x1D)      // 3B >> 1 = 7-bit default
    #define LSM9DS0_ACCELMAG_DEV_ID                         (0x49)      // 0100 1001
    #define LSM9DS0_GYRO_DEV_ID                             (0xD4)      // 1101 0100

/**************************************************************************
    CONVERSION DELAY (in mS)
**************************************************************************/
    #define LSM9DS0_CONVERSIONDELAY                         (100)

/**************************************************************************
    GYROSCOPE REGISTERS
**************************************************************************/
    #define LSM9DS0_REG_GYRO_WHO_AM_I                       (0x0F)      // Device identification Register
    #define LSM9DS0_REG_GYRO_CTRL_REG1                      (0x20)      // Gyroscope Control Register 1
    #define LSM9DS0_REG_GYRO_CTRL_REG2                      (0x21)      // Gyroscope Control Register 2
    #define LSM9DS0_REG_GYRO_CTRL_REG3                      (0x22)      // Gyroscope Control Register 3
    #define LSM9DS0_REG_GYRO_CTRL_REG4                      (0x23)      // Gyroscope Control Register 4
    #define LSM9DS0_REG_GYRO_CTRL_REG5                      (0x24)      // Gyroscope Control Register 5
    #define LSM9DS0_REG_GYRO_REFERENCE                      (0x25)      // Reference/Datacapture Register
    #define LSM9DS0_REG_GYRO_STATUS                         (0x27)      // Status Register
    #define LSM9DS0_REG_GYRO_OUT_X_L                        (0x28)      // X-Axis Angular Rate Data Low Register
    #define LSM9DS0_REG_GYRO_OUT_X_H                        (0x29)      // X-Axis Angular Rate Data High Register
    #define LSM9DS0_REG_GYRO_OUT_Y_L                        (0x2A)      // Y-Axis Angular Rate Data Low Register
    #define LSM9DS0_REG_GYRO_OUT_Y_H                        (0x2B)      // Y-Axis Angular Rate Data High Register
    #define LSM9DS0_REG_GYRO_OUT_Z_L                        (0x2C)      // Z-Axis Angular Rate Data Low Register
    #define LSM9DS0_REG_GYRO_OUT_Z_H                        (0x2D)      // Z-Axis Angular Rate Data High Register
    #define LSM9DS0_REG_GYRO_FIFO_CTRL                      (0x2E)      // FIFO Control Register
    #define LSM9DS0_REG_GYRO_FIFO_SRC                       (0x2F)      // FIFO Source Register
    #define LSM9DS0_REG_GYRO_INT1_CFG                       (0x30)      // Interrupt Configuration Register
    #define LSM9DS0_REG_GYRO_INT1_SRC                       (0x31)      // Interrupt Source Register
    #define LSM9DS0_REG_GYRO_INT1_THS_XH                    (0x32)      // Interrupt X-Axis High Threshold Register
    #define LSM9DS0_REG_GYRO_INT1_THS_XL                    (0x33)      // Interrupt X-Axis Low Threshold Register
    #define LSM9DS0_REG_GYRO_INT1_THS_YH                    (0x34)      // Interrupt Y-Axis High Threshold Register
    #define LSM9DS0_REG_GYRO_INT1_THS_YL                    (0x35)      // Interrupt Y-Axis Low Threshold Register
    #define LSM9DS0_REG_GYRO_INT1_THS_ZH                    (0x36)      // Interrupt Z-Axis High Threshold Register
    #define LSM9DS0_REG_GYRO_INT1_THS_ZL                    (0x37)      // Interrupt Z-Axis Low Threshold Register
    #define LSM9DS0_REG_GYRO_INT1_DURATION                  (0x38)      // Interrupt Duration Register

/**************************************************************************
    TEMPERATURE REGISTERS
**************************************************************************/
    #define LSM9DS0_REG_TEMP_OUT_L                          (0x05)      // Temperature Sensor Low Register
    #define LSM9DS0_REG_TEMP_OUT_H                          (0x06)      // Temperature Sensor High Register

/**************************************************************************
    MAGNETOMETER/ACCELEROMETER REGISTERS
**************************************************************************/
    #define LSM9DS0_REG_MAG_STATUS_M                        (0x07)      // Magnetic Data Status Register
    #define LSM9DS0_REG_MAG_OUT_X_L                         (0x08)      // X-Axis Magnetic Data Low Register
    #define LSM9DS0_REG_MAG_OUT_X_H                         (0x09)      // X-Axis Magnetic Data High Register
    #define LSM9DS0_REG_MAG_OUT_Y_L                         (0x0A)      // Y-Axis Magnetic Data Low Register
    #define LSM9DS0_REG_MAG_OUT_Y_H                         (0x0B)      // Y-Axis Magnetic Data High Register
    #define LSM9DS0_REG_MAG_OUT_Z_L                         (0x0C)      // Z-Axis Magnetic Data Low Register
    #define LSM9DS0_REG_MAG_OUT_Z_H                         (0x0D)      // Z-Axis Magnetic Data High Register
    #define LSM9DS0_REG_MAG_WHO_AM_I                        (0x0F)      // Device identification Register
    #define LSM9DS0_REG_MAG_INT_CTRL                        (0x12)      // Interrupt Recognition Register
    #define LSM9DS0_REG_MAG_INT_SRC                         (0x13)      // Interrupt Source Register
    #define LSM9DS0_REG_MAG_INT_THS_L                       (0x14)      // Magnetic Interrupt Threshold Low Register
    #define LSM9DS0_REG_MAG_INT_THS_H                       (0x15)      // Magnetic Interrupt Threshold High Register
    #define LSM9DS0_REG_MAG_OFFSET_X_L                      (0x16)      // Magnetic Offset for X-Axis Low Register
    #define LSM9DS0_REG_MAG_OFFSET_X_H                      (0x17)      // Magnetic Offset for X-Axis High Register
    #define LSM9DS0_REG_MAG_OFFSET_Y_L                      (0x18)      // Magnetic Offset for Y-Axis Low Register
    #define LSM9DS0_REG_MAG_OFFSET_Y_H                      (0x19)      // Magnetic Offset for Y-Axis High Register
    #define LSM9DS0_REG_MAG_OFFSET_Z_L                      (0x1A)      // Magnetic Offset for Z-Axis Low Register
    #define LSM9DS0_REG_MAG_OFFSET_Z_H                      (0x1B)      // Magnetic Offset for Z-Axis High Register
    #define LSM9DS0_REG_ACCEL_REF_X                         (0x1C)      // Reference Value for High-Pass Filter for X-Axis Acceleration Data
    #define LSM9DS0_REG_ACCEL_REF_Y                         (0x1D)      // Reference Value for High-Pass Filter for Y-Axis Acceleration Data
    #define LSM9DS0_REG_ACCEL_REF_Z                         (0x1E)      // Reference Value for High-Pass Filter for Z-Axis Acceleration Data
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG0                  (0x1F)      // Accelerometer/Magnetometer Control Register 0
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1                  (0x20)      // Accelerometer/Magnetometer Control Register 1
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2                  (0x21)      // Accelerometer/Magnetometer Control Register 2
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG3                  (0x22)      // Accelerometer/Magnetometer Control Register 3
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG4                  (0x23)      // Accelerometer/Magnetometer Control Register 4
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5                  (0x24)      // Accelerometer/Magnetometer Control Register 5
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG6                  (0x25)      // Accelerometer/Magnetometer Control Register 6
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG7                  (0x26)      // Accelerometer/Magnetometer Control Register 7
    #define LSM9DS0_REG_ACCEL_STATUS                        (0x27)      // Acceleration Status Register
    #define LSM9DS0_REG_ACCEL_OUT_X_L                       (0x28)      // X-Axis Acceleration Data Low Register
    #define LSM9DS0_REG_ACCEL_OUT_X_H                       (0x29)      // X-Axis Acceleration Data High Register
    #define LSM9DS0_REG_ACCEL_OUT_Y_L                       (0x2A)      // Y-Axis Acceleration Data Low Register
    #define LSM9DS0_REG_ACCEL_OUT_Y_H                       (0x2B)      // Y-Axis Acceleration Data High Register
    #define LSM9DS0_REG_ACCEL_OUT_Z_L                       (0x2C)      // Z-Axis Acceleration Data Low Register
    #define LSM9DS0_REG_ACCEL_OUT_Z_H                       (0x2D)      // Z-Axis Acceleration Data High Register
    #define LSM9DS0_REG_ACCELMAG_FIFO_CTRL                  (0x2E)      // FIFO Control Register
    #define LSM9DS0_REG_ACCELMAG_FIFO_SRC                   (0x2F)      // FIFO Source Register
    #define LSM9DS0_REG_ACCELMAG_INT1_GEN                   (0x30)      // Interrupt 1 Recognition Register
    #define LSM9DS0_REG_ACCELMAG_INT1_SRC                   (0x31)      // Interrupt 1 Source Register
    #define LSM9DS0_REG_ACCELMAG_INT1_THS                   (0x32)      // Interrupt 1 Threshold Register
    #define LSM9DS0_REG_ACCELMAG_INT1_DUR                   (0x33)      // Interrupt 1 Duration Register
    #define LSM9DS0_REG_ACCELMAG_INT2_GEN                   (0x34)      // Interrupt 2 Recognition Register
    #define LSM9DS0_REG_ACCELMAG_INT2_SRC                   (0x35)      // Interrupt 2 Source Register
    #define LSM9DS0_REG_ACCELMAG_INT2_THS                   (0x36)      // Interrupt 2 Threshold Register
    #define LSM9DS0_REG_ACCELMAG_INT2_DUR                   (0x37)      // Interrupt 2 Duration Register
    #define LSM9DS0_REG_ACCELMAG_CLICK_CFG                  (0x38)      // Interrupt Click Recognition Register
    #define LSM9DS0_REG_ACCELMAG_CLICK_SRC                  (0x39)      // Interrupt Click Source Register
    #define LSM9DS0_REG_ACCELMAG_CLICK_THS                  (0x3A)      // Interrupt Click Threshold Register
    #define LSM9DS0_REG_ACCELMAG_TIME_LIMIT                 (0x3B)      // Click Time Limit Register
    #define LSM9DS0_REG_ACCELMAG_TIME_LAT                   (0x3C)      // Click Time Latency Register
    #define LSM9DS0_REG_ACCELMAG_TIME_WIN                   (0x3D)      // Click Time Window Register
    #define LSM9DS0_REG_ACCELMAG_ACT_THS                    (0x3E)      // Sleep-to-Wake, Return-to-Sleep Activation Threshold Register
    #define LSM9DS0_REG_ACCELMAG_ACT_DUR                    (0x3F)      // Sleep-to-Wake, Return-to-Sleep Duration Register

/**************************************************************************
    GYROSCOPE CONTROL REGISTER 1 DESCRIPTION
**************************************************************************/
    #define LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_MASK            (0xF0)  // Output Data Rate and Bandwidth Selection
    #define LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_95_12_5         (0x00)  // ODR (Hz): 95, Cutoff: 12.5
    #define LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_95_25           (0x30)  // ODR (Hz): 95, Cutoff: 25
    #define LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_190_12_5        (0x40)  // ODR (Hz): 190, Cutoff: 12.5
    #define LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_190_25          (0x50)  // ODR (Hz): 190, Cutoff: 25
    #define LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_190_50          (0x60)  // ODR (Hz): 190, Cutoff: 50
    #define LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_190_70          (0x70)  // ODR (Hz): 190, Cutoff: 70
    #define LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_380_20          (0x80)  // ODR (Hz): 380, Cutoff: 20
    #define LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_380_25          (0x90)  // ODR (Hz): 380, Cutoff: 25
    #define LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_380_50          (0xA0)  // ODR (Hz): 380, Cutoff: 50
    #define LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_380_100         (0xB0)  // ODR (Hz): 380, Cutoff: 100
    #define LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_760_30          (0xC0)  // ODR (Hz): 760, Cutoff: 30
    #define LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_760_35          (0xD0)  // ODR (Hz): 760, Cutoff: 35
    #define LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_760_50          (0xE0)  // ODR (Hz): 760, Cutoff: 50
    #define LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_760_100         (0xF0)  // ODR (Hz): 760, Cutoff: 100

    #define LSM9DS0_REG_GYRO_CTRL_REG1_PD_MASK              (0x08)  // Power-Down Mode Enable
    #define LSM9DS0_REG_GYRO_CTRL_REG1_PD_DOWN              (0x00)  // Power-Down Mode
    #define LSM9DS0_REG_GYRO_CTRL_REG1_PD_NORMAL            (0x08)  // Normal Mode or Sleep Mode

    #define LSM9DS0_REG_GYRO_CTRL_REG1_ZEN_MASK             (0x04)  // Gyroscope Z-axis Enable
    #define LSM9DS0_REG_GYRO_CTRL_REG1_ZEN_DISABLE          (0x00)  // Gyroscope Z-axis Disabled
    #define LSM9DS0_REG_GYRO_CTRL_REG1_ZEN_ENABLE           (0x04)  // Gyroscope Z-axis Enabled

    #define LSM9DS0_REG_GYRO_CTRL_REG1_YEN_MASK             (0x02)  // Gyroscope Y-axis Enable
    #define LSM9DS0_REG_GYRO_CTRL_REG1_YEN_DISABLE          (0x00)  // Gyroscope Y-axis Disabled
    #define LSM9DS0_REG_GYRO_CTRL_REG1_YEN_ENABLE           (0x02)  // Gyroscope Y-axis Enabled

    #define LSM9DS0_REG_GYRO_CTRL_REG1_XEN_MASK             (0x01)  // Gyroscope X-axis Enable
    #define LSM9DS0_REG_GYRO_CTRL_REG1_XEN_DISABLE          (0x00)  // Gyroscope X-axis Disabled
    #define LSM9DS0_REG_GYRO_CTRL_REG1_XEN_ENABLE           (0x01)  // Gyroscope X-axis Enabled

/**************************************************************************
    GYROSCOPE CONTROL REGISTER 4 DESCRIPTION
**************************************************************************/
    #define LSM9DS0_REG_GYRO_CTRL_REG4_BDU_MASK             (0x80)  // Block Data Update
    #define LSM9DS0_REG_GYRO_CTRL_REG4_BDU_CONTINUOUS       (0x00)  // Continuous Update
    #define LSM9DS0_REG_GYRO_CTRL_REG4_BDU_NOTUPDATE        (0x80)  // Output Registers Not Updated until MSB and LSB Read

    #define LSM9DS0_REG_GYRO_CTRL_REG4_BLE_MASK             (0x40)  // Big/Little Endian Data Selection
    #define LSM9DS0_REG_GYRO_CTRL_REG4_BLE_LSB              (0x00)  // Data LSB @ lower address
    #define LSM9DS0_REG_GYRO_CTRL_REG4_BLE_MSB              (0x40)  // Data MSB @ lower address

    #define LSM9DS0_REG_GYRO_CTRL_REG4_FS_MASK              (0x30)  // Full-Scale Selection
    #define LSM9DS0_REG_GYRO_CTRL_REG4_FS_245               (0x00)  // 245 dps
    #define LSM9DS0_REG_GYRO_CTRL_REG4_FS_500               (0x10)  // 500 dps
    #define LSM9DS0_REG_GYRO_CTRL_REG4_FS_2000              (0x30)  // 2000 dps

    #define LSM9DS0_REG_GYRO_CTRL_REG4_ST_MASK              (0x06)  // Self-Test Enable
    #define LSM9DS0_REG_GYRO_CTRL_REG4_ST_NORMAL            (0x00)  // Normal Mode
    #define LSM9DS0_REG_GYRO_CTRL_REG4_ST_0                 (0x02)  // Self-Test 0 (X Positive Sign, Y and Z Negative Sign)
    #define LSM9DS0_REG_GYRO_CTRL_REG4_ST_1                 (0x06)  // Self-Test 1 (X Negative Sign, Y and Z Positive Sign)

    #define LSM9DS0_REG_GYRO_CTRL_REG4_SIM_MASK             (0x01)  // SPI Serial Interface Mode Selection
    #define LSM9DS0_REG_GYRO_CTRL_REG4_SIM_4WIRE            (0x00)  // 4-Wire Interface
    #define LSM9DS0_REG_GYRO_CTRL_REG4_SIM_3WIRE            (0x01)  // 3-Wire Interface

/**************************************************************************
    ACCELEROMETER/MAGNETOMETER CONTROL REGISTER 1 DESCRIPTION
**************************************************************************/
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_MASK        (0xF0)  // Acceleration Data Rate Selection
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_PD          (0x00)  // Power-Down Mode
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_3_125       (0x10)  // 3.125 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_6_25        (0x20)  // 6.25 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_12_5        (0x30)  // 12.5 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_25          (0x40)  // 25 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_50          (0x50)  // 50 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_100         (0x60)  // 100 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_200         (0x70)  // 200 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_400         (0x80)  // 400 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_800         (0x90)  // 800 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_1600        (0xA0)  // 1600 Hz

    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_BDU_MASK         (0x08)  // Block Data Update for Acceleration and Magnetic Data
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_BDU_CONTINUOUS   (0x00)  // Continuous Update
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_BDU_NOTUPDATE    (0x08)  // Output Registers Not Updated until MSB and LSB Read

    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AZEN_MASK        (0x04)  // Acceleration Z-axis Enable
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AZEN_DISABLE     (0x00)  // Acceleration Z-axis Disabled
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AZEN_ENABLE      (0x04)  // Acceleration Z-axis Enabled

    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AYEN_MASK        (0x02)  // Acceleration Y-axis Enable
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AYEN_DISABLE     (0x00)  // Acceleration Y-axis Disabled
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AYEN_ENABLE      (0x02)  // Acceleration Y-axis Enabled

    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AXEN_MASK        (0x01)  // Acceleration X-axis Enable
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AXEN_DISABLE     (0x00)  // Acceleration X-axis Disabled
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG1_AXEN_ENABLE      (0x01)  // Acceleration X-axis Enabled

/**************************************************************************
    ACCELEROMETER/MAGNETOMETER CONTROL REGISTER 2 DESCRIPTION
**************************************************************************/
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_ABW_MASK         (0xC0)  // Accelerometer Anti-Alias Filter Bandwidth
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_ABW_773          (0x00)  // 773 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_ABW_194          (0x40)  // 194 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_ABW_362          (0x80)  // 362 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_ABW_50           (0xC0)  // 50 Hz

    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_AFS_MASK         (0x38)  // Acceleration Full-Scale Selection
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_AFS_2G           (0x00)  // ±2 g
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_AFS_4G           (0x08)  // ±4 g
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_AFS_6G           (0x10)  // ±6 g
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_AFS_8G           (0x18)  // ±8 g
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_AFS_16G          (0x20)  // ±16 g

    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_AST_MASK         (0x06)  // Acceleration Self-Test Enable
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_AST_NORMAL       (0x00)  // Normal Mode
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_AST_POSITIVE     (0x02)  // Positive Sign Self-Test
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_AST_NEGATIVE     (0x04)  // Negative Sign Self-Test
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_AST_NOT          (0x06)  // Not Allowed

    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_SIM_MASK         (0x01)  // SPI Serial Interface Mode Selection
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_SIM_4WIRE        (0x00)  // 4-Wire Interface
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG2_SIM_3WIRE        (0x01)  // 3-Wire Interface

/**************************************************************************
    ACCELEROMETER/MAGNETOMETER CONTROL REGISTER 5 DESCRIPTION
**************************************************************************/
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_TEMP_MASK        (0x80)  // Temperature Sensor Enable
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_TEMP_DISABLED    (0x00)  // Temperature Sensor Disabled
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_TEMP_ENABLED     (0x80)  // Temperature Sensor Enabled

    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_M_RES_MASK       (0x60)  // Magnetic Resolution Selection
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_M_RES_LOW        (0x00)  // Magnetic Resolution Low
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_M_RES_HIGH       (0x60)  // Magnetic Resolution High

    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_MODR_MASK        (0x1C)  // Magnetic Data Rate Selection
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_MODR_3_125       (0x00)  // 3.125 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_MODR_6_25        (0x04)  // 6.25 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_MODR_12_5        (0x08)  // 12.5 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_MODR_25          (0x0C)  // 25 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_MODR_50          (0x10)  // 50 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_MODR_100         (0x14)  // 100 Hz
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_MODR_RES         (0x1C)  // Reserved

    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_LIR2_MASK        (0x02)  // Latch Interrupt Request on INT2_SRC Register, with INT2_SRC Register Cleared by Reading INT2_SRC Itself
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_LIR2_NOLATCH     (0x00)  // Interrupt Request Not Latched
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_LIR2_LATCH       (0x02)  // Interrupt Request Latched

    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_LIR1_MASK        (0x01)  // Latch Interrupt Request on INT1_SRC Register, with INT1_SRC Register Cleared by Reading INT1_SRC Itself
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_LIR1_NOLATCH     (0x00)  // Interrupt Request Not Latched
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG5_LIR1_LATCH       (0x01)  // Interrupt Request Latched

/**************************************************************************
    ACCELEROMETER/MAGNETOMETER CONTROL REGISTER 6 DESCRIPTION
**************************************************************************/
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG6_MFS_MASK         (0x60)  // Magnetic Full-Scale Selection
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG6_MFS_2G           (0x00)  // ±2 gauss
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG6_MFS_4G           (0x20)  // ±4 gauss
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG6_MFS_8G           (0x40)  // ±8 gauss
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG6_MFS_12G          (0x60)  // ±12 gauss

/**************************************************************************
    ACCELEROMETER/MAGNETOMETER CONTROL REGISTER 7 DESCRIPTION
**************************************************************************/
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG7_AHPM_MASK        (0xC0)  // High-Pass Filter Mode Selection for Acceleration Data
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG7_AHPM_NORMAL      (0x00)  // Normal mode (resets x, y and z-axis reading REFERENCE_X (1Ch), REFERENCE_Y (1Dh) and REFERENCE_Y (1Dh) registers respectively)
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG7_AHPM_REF         (0x40)  // Reference signal for filtering
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG7_AHPM_NORMAL1     (0x80)  // Normal mode
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG7_AHPM_AUTO        (0xC0)  // Autoreset on interrupt event

    #define LSM9DS0_REG_ACCELMAG_CTRL_REG7_AFDS_MASK        (0x20)  // Filtered Acceleration Data Selection
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG7_AFDS_BYPASS      (0x00)  // Internal Filter Bypassed
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG7_AFDS_INTFILTER   (0x20)  // Data From Internal Filter Sent to Output Register and FIFO

    #define LSM9DS0_REG_ACCELMAG_CTRL_REG7_MLP_MASK         (0x04)  // Magnetic Sensor Mode Selection
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG7_MLP_MODR         (0x00)  // Magnetic Data Rate is Configured by MODR Bits
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG7_MLP_3_125        (0x04)  // MODR is Set to 3.125 Hz Independently From the MODR Settings

    #define LSM9DS0_REG_ACCELMAG_CTRL_REG7_MD_MASK          (0x03)  // Magnetic Sensor Mode Selection
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG7_MD_CONTINUOUS    (0x00)  // Continuous-Conversion Mode
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG7_MD_SINGLE        (0x01)  // Single-conversion mode
    #define LSM9DS0_REG_ACCELMAG_CTRL_REG7_MD_PDOWN         (0x03)  // Power-Down mode


typedef enum
{
    GYRO_DATARATE_95_12_5           = LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_95_12_5,
    GYRO_DATARATE_95_25             = LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_95_25,
    GYRO_DATARATE_190_12_5          = LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_190_12_5,
    GYRO_DATARATE_190_25            = LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_190_25,
    GYRO_DATARATE_190_50            = LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_190_50,
    GYRO_DATARATE_190_70            = LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_190_70,
    GYRO_DATARATE_380_20            = LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_380_20,
    GYRO_DATARATE_380_25            = LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_380_25,
    GYRO_DATARATE_380_50            = LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_380_50,
    GYRO_DATARATE_380_100           = LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_380_100,
    GYRO_DATARATE_760_30            = LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_760_30,
    GYRO_DATARATE_760_35            = LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_760_35,
    GYRO_DATARATE_760_50            = LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_760_50,
    GYRO_DATARATE_760_100           = LSM9DS0_REG_GYRO_CTRL_REG1_DRBW_760_100
    
} lsmGyroDataRate_t;

typedef enum
{
    GYRO_POWER_DOWN_MODE            = LSM9DS0_REG_GYRO_CTRL_REG1_PD_DOWN,
    GYRO_NORMAL_MODE                = LSM9DS0_REG_GYRO_CTRL_REG1_PD_NORMAL
    
} lsmGyroMode_t;

typedef enum
{
    GYRO_Z_AXIS_DISABLED            = LSM9DS0_REG_GYRO_CTRL_REG1_ZEN_DISABLE,
    GYRO_Z_AXIS_ENABLED             = LSM9DS0_REG_GYRO_CTRL_REG1_ZEN_ENABLE
    
} lsmGyroZen_t;

typedef enum
{
    GYRO_Y_AXIS_DISABLED            = LSM9DS0_REG_GYRO_CTRL_REG1_YEN_DISABLE,
    GYRO_Y_AXIS_ENABLED             = LSM9DS0_REG_GYRO_CTRL_REG1_YEN_ENABLE
    
} lsmGyroYen_t;

typedef enum
{
    GYRO_X_AXIS_DISABLED            = LSM9DS0_REG_GYRO_CTRL_REG1_XEN_DISABLE,
    GYRO_X_AXIS_ENABLED             = LSM9DS0_REG_GYRO_CTRL_REG1_XEN_ENABLE
    
} lsmGyroXen_t;

typedef enum
{
    GYRO_CONTINUOUS                 = LSM9DS0_REG_GYRO_CTRL_REG4_BDU_CONTINUOUS,
    GYRO_NOT_UPDATED                = LSM9DS0_REG_GYRO_CTRL_REG4_BDU_NOTUPDATE
    
} lsmGyroBlockData_t;

typedef enum
{
    GYRO_DATA_LSB                   = LSM9DS0_REG_GYRO_CTRL_REG4_BLE_LSB,
    GYRO_DATA_MSB                   = LSM9DS0_REG_GYRO_CTRL_REG4_BLE_MSB
    
} lsmGyroEndianData_t;

typedef enum
{
    GYRO_SCALE_245DPS               = LSM9DS0_REG_GYRO_CTRL_REG4_FS_245,
    GYRO_SCALE_500DPS               = LSM9DS0_REG_GYRO_CTRL_REG4_FS_500,
    GYRO_SCALE_2000DPS              = LSM9DS0_REG_GYRO_CTRL_REG4_FS_2000
    
} lsmGyroScale_t;

typedef enum
{
    GYRO_SELF_TEST_NORMAL           = LSM9DS0_REG_GYRO_CTRL_REG4_ST_NORMAL,
    GYRO_SELF_TEST_0                = LSM9DS0_REG_GYRO_CTRL_REG4_ST_0,
    GYRO_SELF_TEST_1                = LSM9DS0_REG_GYRO_CTRL_REG4_ST_1
    
} lsmGyroSelfTest_t;

typedef enum
{
    GYRO_SPI_WIRE_4                 = LSM9DS0_REG_GYRO_CTRL_REG4_SIM_4WIRE,
    GYRO_SPI_WIRE_3                 = LSM9DS0_REG_GYRO_CTRL_REG4_SIM_3WIRE
    
} lsmGyroSPIWire_t;

typedef enum
{
    ACCEL_DATARATE_POWERDOWN        = LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_PD,
    ACCEL_DATARATE_3_125HZ          = LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_3_125,
    ACCEL_DATARATE_6_25HZ           = LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_6_25,
    ACCEL_DATARATE_12_5HZ           = LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_12_5,
    ACCEL_DATARATE_25HZ             = LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_25,
    ACCEL_DATARATE_50HZ             = LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_50,
    ACCEL_DATARATE_100HZ            = LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_100,
    ACCEL_DATARATE_200HZ            = LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_200,
    ACCEL_DATARATE_400HZ            = LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_400,
    ACCEL_DATARATE_800HZ            = LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_800,
    ACCEL_DATARATE_1600HZ           = LSM9DS0_REG_ACCELMAG_CTRL_REG1_AODR_1600
    
} lsmAccelDataRate_t;

typedef enum
{
    ACCELMAG_CONTINUOUS                = LSM9DS0_REG_ACCELMAG_CTRL_REG1_BDU_CONTINUOUS,
    ACCELMAG_NOT_UPDATED               = LSM9DS0_REG_ACCELMAG_CTRL_REG1_BDU_NOTUPDATE
    
} lsmAccelMagBlockData_t;

typedef enum
{
    ACCEL_Z_AXIS_DISABLED           =   LSM9DS0_REG_ACCELMAG_CTRL_REG1_AZEN_DISABLE,
    ACCEL_Z_AXIS_ENABLED            = LSM9DS0_REG_ACCELMAG_CTRL_REG1_AZEN_ENABLE
    
} lsmAccelZen_t;

typedef enum
{
    ACCEL_Y_AXIS_DISABLED           = LSM9DS0_REG_ACCELMAG_CTRL_REG1_AYEN_DISABLE,
    ACCEL_Y_AXIS_ENABLED            = LSM9DS0_REG_ACCELMAG_CTRL_REG1_AYEN_ENABLE
    
} lsmAccelYen_t;

typedef enum
{
    ACCEL_X_AXIS_DISABLED           = LSM9DS0_REG_ACCELMAG_CTRL_REG1_AYEN_DISABLE,
    ACCEL_X_AXIS_ENABLED            = LSM9DS0_REG_ACCELMAG_CTRL_REG1_AYEN_ENABLE
    
} lsmAccelXen_t;

typedef enum
{
    ACCEL_BANDWIDTH_773HZ           = LSM9DS0_REG_ACCELMAG_CTRL_REG2_ABW_773,
    ACCEL_BANDWIDTH_194HZ           = LSM9DS0_REG_ACCELMAG_CTRL_REG2_ABW_194,
    ACCEL_BANDWIDTH_362HZ           = LSM9DS0_REG_ACCELMAG_CTRL_REG2_ABW_362,
    ACCEL_BANDWIDTH_50HZ            = LSM9DS0_REG_ACCELMAG_CTRL_REG2_ABW_50

} lsmAccelBandwidth_t;


typedef enum
{
    ACCEL_RANGE_2G                  = LSM9DS0_REG_ACCELMAG_CTRL_REG2_AFS_2G,
    ACCEL_RANGE_4G                  = LSM9DS0_REG_ACCELMAG_CTRL_REG2_AFS_4G,
    ACCEL_RANGE_6G                  = LSM9DS0_REG_ACCELMAG_CTRL_REG2_AFS_6G,
    ACCEL_RANGE_8G                  = LSM9DS0_REG_ACCELMAG_CTRL_REG2_AFS_8G,
    ACCEL_RANGE_16G                 = LSM9DS0_REG_ACCELMAG_CTRL_REG2_AFS_16G
    
} lsmAccelRange_t;

typedef enum
{
    ACCEL_SELF_TEST_NORMAL          = LSM9DS0_REG_ACCELMAG_CTRL_REG2_AST_NORMAL,
    ACCEL_SELF_TEST_POSITIVE        = LSM9DS0_REG_ACCELMAG_CTRL_REG2_AST_POSITIVE,
    ACCEl_SELF_TEST_NEGATIVE        = LSM9DS0_REG_ACCELMAG_CTRL_REG2_AST_NEGATIVE
    
} lsmAccelSelfTest_t;

typedef enum
{
    ACCEL_MAG_SPI_WIRE_4                 = LSM9DS0_REG_ACCELMAG_CTRL_REG2_SIM_4WIRE,
    ACCEL_MAG_SPI_WIRE_3                 = LSM9DS0_REG_ACCELMAG_CTRL_REG2_SIM_3WIRE
    
} lsmAccelMagSPIWire_t;

typedef enum
{
    TEMP_SENSOR_DISABLED            = LSM9DS0_REG_ACCELMAG_CTRL_REG5_TEMP_DISABLED,
    TEMP_SENSOR_ENABLED             = LSM9DS0_REG_ACCELMAG_CTRL_REG5_TEMP_ENABLED
    
} lsmTempSensor_t;

typedef enum
{
    MAG_RESOLUTION_LOW              = LSM9DS0_REG_ACCELMAG_CTRL_REG5_M_RES_LOW,
    MAG_RESOLUTION_HIGH             = LSM9DS0_REG_ACCELMAG_CTRL_REG5_M_RES_HIGH
    
} lsmMagResolution_t;

typedef enum
{
    MAG_DATARATE_3_125HZ            = LSM9DS0_REG_ACCELMAG_CTRL_REG5_MODR_3_125,
    MAG_DATARATE_6_25HZ             = LSM9DS0_REG_ACCELMAG_CTRL_REG5_MODR_6_25,
    MAG_DATARATE_12_5HZ             = LSM9DS0_REG_ACCELMAG_CTRL_REG5_MODR_12_5,
    MAG_DATARATE_25HZ               = LSM9DS0_REG_ACCELMAG_CTRL_REG5_MODR_25,
    MAG_DATARATE_50HZ               = LSM9DS0_REG_ACCELMAG_CTRL_REG5_MODR_50,
    MAG_DATARATE_100HZ              = LSM9DS0_REG_ACCELMAG_CTRL_REG5_MODR_100
    
} lsmMagDataRate_t;

typedef enum
{
    MAG_INTR2_NOT_LATCHED           = LSM9DS0_REG_ACCELMAG_CTRL_REG5_LIR2_NOLATCH,
    MAG_INTR2_LATCHED               = LSM9DS0_REG_ACCELMAG_CTRL_REG5_LIR2_LATCH
    
} lsmMagIntr2Latch_t;

typedef enum
{
    MAG_INTR1_NOT_LATCHED           = LSM9DS0_REG_ACCELMAG_CTRL_REG5_LIR1_NOLATCH,
    MAG_INTR1_LATCHED               = LSM9DS0_REG_ACCELMAG_CTRL_REG5_LIR1_LATCH
    
} lsmMagIntr1Latch_t;

typedef enum
{
    MAG_GAIN_2GAUSS                 = LSM9DS0_REG_ACCELMAG_CTRL_REG6_MFS_2G,
    MAG_GAIN_4GAUSS                 = LSM9DS0_REG_ACCELMAG_CTRL_REG6_MFS_4G,
    MAG_GAIN_8GAUSS                 = LSM9DS0_REG_ACCELMAG_CTRL_REG6_MFS_8G,
    MAG_GAIN_12GAUSS                = LSM9DS0_REG_ACCELMAG_CTRL_REG6_MFS_12G
    
} lsmMagGain_t;

typedef enum
{
    MAG_CONTINUOUS                  = LSM9DS0_REG_ACCELMAG_CTRL_REG7_MD_CONTINUOUS,
    MAG_SINGLE                      = LSM9DS0_REG_ACCELMAG_CTRL_REG7_MD_SINGLE,
    MAG_POWER_DOWN                  = LSM9DS0_REG_ACCELMAG_CTRL_REG7_MD_PDOWN
    
} lsmMagMode_t;

typedef struct
{
    int16_t X;
    int16_t Y;
    int16_t Z;
    float   T;
    
} lsmSensorData_t;


class LSM9DS0
{
    protected:
        // Instance-specific properties
        uint8_t lsm_conversionDelay;
        lsmGyroDataRate_t lsm_gyrodatarate;
        lsmGyroMode_t lsm_gyromode;
        lsmGyroZen_t lsm_gyrozen;
        lsmGyroYen_t lsm_gyroyen;
        lsmGyroXen_t lsm_gyroxen;
        lsmGyroBlockData_t lsm_gyroblockdata;
        lsmGyroEndianData_t lsm_gyroendiandata;
        lsmGyroScale_t lsm_gyroscale;
        lsmGyroSelfTest_t lsm_gyroselftest;
        lsmGyroSPIWire_t lsm_gyrospiwire;
        lsmAccelDataRate_t lsm_acceldatarate;
        lsmAccelMagBlockData_t lsm_accelmagblockdata;
        lsmAccelZen_t lsm_accelzen;
        lsmAccelYen_t lsm_accelyen;
        lsmAccelXen_t lsm_accelxen;
        lsmAccelBandwidth_t lsm_accelbandwidth;
        lsmAccelRange_t lsm_accelrange;
        lsmAccelSelfTest_t lsm_accelselftest;
        lsmAccelMagSPIWire_t lsm_accelmagspiwire;
        lsmTempSensor_t lsm_tempsensor;
        lsmMagResolution_t lsm_magresolution;
        lsmMagDataRate_t lsm_magdatarate;
        lsmMagIntr2Latch_t lsm_magintr2latch;
        lsmMagIntr1Latch_t lsm_magintr1latch;
        lsmMagGain_t lsm_maggain;
        lsmMagMode_t lsm_magmode;

    public:
        uint8_t lsm_i2cAddressG;
        uint8_t lsm_i2cAddressXM;
        lsmSensorData_t lsm_gyroData;
        lsmSensorData_t lsm_accelData;
        lsmSensorData_t lsm_magData;
        lsmSensorData_t lsm_tempData;
        int16_t tempData;
        void getAddrG_LSM9DS0(uint8_t i2cAddress);
        void getAddrXM_LSM9DS0(uint8_t i2cAddress);
        bool begin(void);
        void Measure_Sensor(void);
        void setUpGyroscope(void);
        void setUpAccelerometer(void);
        void setUpMagnetometer(void);
        void Measure_Gyroscope(void);
        void Measure_Accelerometer(void);
        void Measure_Magnetometer(void);
        void Measure_Temperature(void);
        void setGyroDataRate(lsmGyroDataRate_t gyrodatarate);
        lsmGyroDataRate_t getGyroDataRate(void);
        void setGyroMode(lsmGyroMode_t gyromode);
        lsmGyroMode_t getGyroMode(void);
        void setGyroZen(lsmGyroZen_t gyrozen);
        lsmGyroZen_t getGyroZen(void);
        void setGyroYen(lsmGyroYen_t gyroyen);
        lsmGyroYen_t getGyroYen(void);
        void setGyroXen(lsmGyroXen_t gyroxen);
        lsmGyroXen_t getGyroXen(void);
        void setGyroBlockData(lsmGyroBlockData_t gyroblockdata);
        lsmGyroBlockData_t getGyroBlockData(void);
        void setGyroEndianData(lsmGyroEndianData_t gyroendiandata);
        lsmGyroEndianData_t getGyroEndianData(void);
        void setGyroScale(lsmGyroScale_t gyroscale);
        lsmGyroScale_t getGyroScale(void);
        void setGyroSelfTest(lsmGyroSelfTest_t gyroselftest);
        lsmGyroSelfTest_t getGyroSelfTest(void);
        void setGyroSPIWire(lsmGyroSPIWire_t gyrospiwire);
        lsmGyroSPIWire_t getGyroSPIWire(void);
        void setAccelDataRate(lsmAccelDataRate_t acceldatarate);
        lsmAccelDataRate_t getAccelDataRate(void);
        void setAccelMagBlockData(lsmAccelMagBlockData_t accelmagblockdata);
        lsmAccelMagBlockData_t getAccelMagBlockData(void);
        void setAccelZen(lsmAccelZen_t accelzen);
        lsmAccelZen_t getAccelZen(void);
        void setAccelYen(lsmAccelYen_t accelyen);
        lsmAccelYen_t getAccelYen(void);
        void setAccelXen(lsmAccelXen_t accelxen);
        lsmAccelXen_t getAccelXen(void);
        void setAccelBandwidth(lsmAccelBandwidth_t accelbandwidth);
        lsmAccelBandwidth_t getAccelBandwidth(void);
        void setAccelRange(lsmAccelRange_t accelrange);
        lsmAccelRange_t getAccelRange(void);
        void setAccelSelfTest(lsmAccelSelfTest_t accelselftest);
        lsmAccelSelfTest_t getAccelSelfTest(void);
        void setAccelMagSPIWire(lsmAccelMagSPIWire_t accelmagspiwire);
        lsmAccelMagSPIWire_t getAccelMagSPIWire(void);
        void setTempSensor(lsmTempSensor_t tempsensor);
        lsmTempSensor_t getTempSensor(void);
        void setMagResolution(lsmMagResolution_t magresolution);
        lsmMagResolution_t getMagResolution(void);
        void setMagDataRate(lsmMagDataRate_t magdatarate);
        lsmMagDataRate_t getMagDataRate(void);
        void setMagIntr2Latch(lsmMagIntr2Latch_t magintr2latch);
        lsmMagIntr2Latch_t getMagIntr2Latch(void);
        void setMagIntr1Latch(lsmMagIntr1Latch_t magintr1latch);
        lsmMagIntr1Latch_t getMagIntr1Latch(void);
        void setMagGain(lsmMagGain_t maggain);
        lsmMagGain_t getMagGain(void);
        void setMagMode(lsmMagMode_t magmode);
        lsmMagMode_t getMagMode(void);
    
    private:
};

Sensor read values
LSM9DS0 Sensor reads value of gyroscope, Magnetometer and accelerometer using these functions as you can see that a variable config1 is used to add all the x,y,z axis and enable configurations by using a bit-wise OR.

 

LSM9DS0 Arduino Code

In Arduino code our first step is to call addresses of every sensor Accelerometer, Gyroscope, and Magnetometer. Setting up their data rates and gain, then we read sensor data by calling the functions  like  setUpGyroscope, setUpAccelerometer, setUpMagnetometer. These functions read binary data and convert those values into decimal form.

#include 
#include 

LSM9DS0 lsm;

void setup(void)
{
    Serial.begin(9600);
    // The address can be changed making the option of connecting multiple devices
    lsm.getAddrG_LSM9DS0(LSM9DS0_DEFAULT_ADDRESS_GYRO);             // 0x6A
    // lsm.getAddrG_LSM9DS0(LSM9DS0_ADDRESS_GYRO_UPDATED);          // 0x6B

    lsm.getAddrXM_LSM9DS0(LSM9DS0_DEFAULT_ADDRESS_ACCELMAG);        // 0x1E
    // lsm.getAddrXM_LSM9DS0(LSM9DS0_ADDRESS_ACCELMAG_UPDATED);     // 0x1D

    // The Gyroscope's Output Data Rate and Bandwidth Selection and Full-Scale Selection,
    // Acceleration Data Rate Selection and Acceleration Full-Scale Selection,
    // Magnetic Data Rate Selection and Magnetic Full-Scale Selection
    // can be changed via the following functions

    /*
    // Also some more parameters can be changed using the following functions
    // Gyroscope:
    // Power-Down Mode Enable, Gyroscope X, Y, Z Axes Enable,
    // Block Data Update, Big/Little Endian Data Selection, Full-Scale Selection,
    // Self-Test Enable and SPI Serial Interface Mode Selection
    // Accelerometer and Magmetometer:
    // Block Data Update for Acceleration and Magnetic Data
    // Acceleration X, Y, Z Axes Enable
    // Acceleration Anti-Alias Filter Bandwidth
    // Self-Test Mode Configuration, SPI Serial Interface Mode Selection
    // Temperature Sensor Enable, Magnetic Resolution Selection
    // Latch Interrupt Request on INT2_SRC Register, Latch Interrupt Request on INT1_SRC Register
    // Magnetic Sensor Mode Selection
    // These functions can be modified and added here so that the parameters can be altered as per requirement
    */

    lsm.setGyroDataRate(GYRO_DATARATE_95_12_5);      // ODR (Hz): 95, Cutoff: 12.5
    // lsm.setGyroDataRate(GYRO_DATARATE_95_25);     // ODR (Hz): 95, Cutoff: 25
    // lsm.setGyroDataRate(GYRO_DATARATE_190_12_5);  // ODR (Hz): 190, Cutoff: 12.5
    // lsm.setGyroDataRate(GYRO_DATARATE_190_25);    // ODR (Hz): 190, Cutoff: 25
    // lsm.setGyroDataRate(GYRO_DATARATE_190_50);    // ODR (Hz): 190, Cutoff: 50
    // lsm.setGyroDataRate(GYRO_DATARATE_190_70);    // ODR (Hz): 190, Cutoff: 70
    // lsm.setGyroDataRate(GYRO_DATARATE_380_20);    // ODR (Hz): 380, Cutoff: 20
    // lsm.setGyroDataRate(GYRO_DATARATE_380_25);    // ODR (Hz): 380, Cutoff: 25
    // lsm.setGyroDataRate(GYRO_DATARATE_380_50);    // ODR (Hz): 380, Cutoff: 50
    // lsm.setGyroDataRate(GYRO_DATARATE_380_100);   // ODR (Hz): 380, Cutoff: 100
    // lsm.setGyroDataRate(GYRO_DATARATE_760_30);    // ODR (Hz): 760, Cutoff: 30
    // lsm.setGyroDataRate(GYRO_DATARATE_760_35);    // ODR (Hz): 760, Cutoff: 35
    // lsm.setGyroDataRate(GYRO_DATARATE_760_50);    // ODR (Hz): 760, Cutoff: 50
    // lsm.setGyroDataRate(GYRO_DATARATE_760_100);   // ODR (Hz): 760, Cutoff: 100

    lsm.setGyroScale(GYRO_SCALE_2000DPS);            // 2000 dps
    // lsm.setGyroScale(GYRO_SCALE_245DPS);          // 245 dps
    // lsm.setGyroScale(GYRO_SCALE_500DPS);          // 500 dps

    lsm.setAccelDataRate(ACCEL_DATARATE_3_125HZ);        // AODR (Hz): 3.125
    // lsm.setAccelDataRate(ACCEL_DATARATE_POWERDOWN);   // Power Down Mode
    // lsm.setAccelDataRate(ACCEL_DATARATE_6_25HZ);      // AODR (Hz): 6.25
    // lsm.setAccelDataRate(ACCEL_DATARATE_12_5HZ);      // AODR (Hz): 12.5
    // lsm.setAccelDataRate(ACCEL_DATARATE_25HZ);        // AODR (Hz): 25
    // lsm.setAccelDataRate(ACCEL_DATARATE_50HZ);        // AODR (Hz): 50
    // lsm.setAccelDataRate(ACCEL_DATARATE_100HZ);       // AODR (Hz): 100
    // lsm.setAccelDataRate(ACCEL_DATARATE_200HZ);       // AODR (Hz): 200
    // lsm.setAccelDataRate(ACCEL_DATARATE_400HZ);       // AODR (Hz): 400
    // lsm.setAccelDataRate(ACCEL_DATARATE_800HZ);       // AODR (Hz): 800
    // lsm.setAccelDataRate(ACCEL_DATARATE_1600HZ);      // AODR (Hz): 1600

    lsm.setAccelRange(ACCEL_RANGE_16G);          // ±16 g
    // lsm.setAccelRange(ACCEL_RANGE_2G);        // ±2 g
    // lsm.setAccelRange(ACCEL_RANGE_4G);        // ±4 g
    // lsm.setAccelRange(ACCEL_RANGE_6G);        // ±6 g
    // lsm.setAccelRange(ACCEL_RANGE_8G);        // ±8 g

    lsm.setMagDataRate(MAG_DATARATE_3_125HZ);    // MODR (Hz): 3.125
    // lsm.setMagDataRate(MAG_DATARATE_6_25HZ);  // MODR (Hz): 6.25
    // lsm.setMagDataRate(MAG_DATARATE_12_5HZ);  // MODR (Hz): 12.5
    // lsm.setMagDataRate(MAG_DATARATE_25HZ);    // MODR (Hz): 25
    // lsm.setMagDataRate(MAG_DATARATE_50HZ);    // MODR (Hz): 50
    // lsm.setMagDataRate(MAG_DATARATE_100HZ);   // MODR (Hz): 100

    lsm.setMagGain(MAG_GAIN_12GAUSS);            // ±12 guass
    // lsm.setMagGain(MAG_GAIN_2GAUSS);          // ±2 guass
    // lsm.setMagGain(MAG_GAIN_4GAUSS);          // ±4 guass
    // lsm.setMagGain(MAG_GAIN_8GAUSS);          // ±8 guass

    lsm.begin();
    delay(1000);
}

void loop(void)
{
    byte errorG, errorXM;
    int8_t addressG, addressXM;

    // LSM9DS0 Gyroscope Address
    addressG = lsm.lsm_i2cAddressG;
    // LSM9DS0 Accelerometer/Magnetometer Address
    addressXM = lsm.lsm_i2cAddressXM;

    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(addressG);
    errorG = Wire.endTransmission();
    Wire.beginTransmission(addressXM);
    errorXM = Wire.endTransmission();
    if ((errorG == 0) && (errorXM == 0))
    {
        Serial.println("Getting Angular Rate, Linear Acceleration, and Magnetic Field Data Readings from LSM9DS0");
        Serial.println(" ");
        // Set up the sensor for Gyroscope, Accelerometer, and Magnetometer
        lsm.setUpGyroscope();
        lsm.setUpAccelerometer();
        lsm.setUpMagnetometer();
        delay(500);
        // Display the results for Angular Rate, Acceleration, and Magnetic Field Values
        lsm.Measure_Sensor();
        lsm.Measure_Gyroscope();
        lsm.Measure_Accelerometer();
        lsm.Measure_Magnetometer();

        // Output Data to Screen
        Serial.print("Rotation in X-Axis: ");
        Serial.println(lsm.lsm_gyroData.X);
        Serial.print("Rotation in Y-Axis: ");
        Serial.println(lsm.lsm_gyroData.Y);
        Serial.print("Rotation in Z-Axis: ");
        Serial.println(lsm.lsm_gyroData.Z);
        Serial.println(" ");
        Serial.println("    ***********     ");
        Serial.println(" ");
        delay(500);
        Serial.print("Linear Acceleration in X-Axis: ");
        Serial.println(lsm.lsm_accelData.X);
        Serial.print("Linear Acceleration in Y-Axis: ");
        Serial.println(lsm.lsm_accelData.Y);
        Serial.print("Linear Acceleration in Z-Axis: ");
        Serial.println(lsm.lsm_accelData.Z);
        Serial.println(" ");
        Serial.println("    ***********     ");
        Serial.println(" ");
        delay(500);
        Serial.print("Magnetic Field in X-Axis: ");
        Serial.println(lsm.lsm_magData.X);
        Serial.print("Magnetic Field in Y-Axis: ");
        Serial.println(lsm.lsm_magData.Y);
        Serial.print("Magnetic Field in Z-Axis: ");
        Serial.println(lsm.lsm_magData.Z);
        Serial.println(" ");
        Serial.println("    ***********     ");
        Serial.println(" ");
        Serial.println("LSM9DS0 Temperature Readings");
        Serial.println(" ");
        Serial.print("Temperature Reading in Celsius: ");
        Serial.print(lsm.lsm_tempData.T);
        Serial.println(" °C");
        Serial.print("Temperature Reading in Fahrenheit: ");
        Serial.print(((lsm.lsm_tempData.T * 1.8) + 32));
        Serial.println(" °F");
        Serial.println(" ");
        Serial.println("        ***************************        ");
        Serial.println(" ");
        delay(1000);
    }
    else
    {
        Serial.println("LSM9DS0 Disconnected!");
        Serial.println(" ");
        Serial.println("        ************        ");
        Serial.println(" ");
    }


    delay(1000);
}

Testing of the sensor
Getting values for linear acceleration and rotational axis
first value

Getting remaining values of magnetic field axis and temperature in second image

second value

 

Reference:

Datasheet

Library

 

void LSM9DS0::setUpGyroscope(void)
{
    // Set Up the Configuration for the Gyroscope Control Register 1
    /*
    // Set the Output Data Rate
    uint8_t config1 = lsm_gyrodatarate;
    
    // Set the Power Down Mode Enable
    config1 |= lsm_gyromode;
    
    // Set the Gyroscpe Z-Axis Enable
    config1 |= lsm_gyrozen;
    
    // Set the Gyroscpe Y-Axis Enable
    config1 |= lsm_gyroyen;
    
    // Set the Gyroscpe X-Axis Enable
    config1 |= lsm_gyroxen;
    */
    
    uint8_t config1 =   LSM9DS0_REG_GYRO_CTRL_REG1_PD_NORMAL    |   // Normal Mode
                        LSM9DS0_REG_GYRO_CTRL_REG1_ZEN_ENABLE   |   // Gyroscope Z-Axis Enabled
                        LSM9DS0_REG_GYRO_CTRL_REG1_YEN_ENABLE   |   // Gyroscope Y-Axis Enabled
                        LSM9DS0_REG_GYRO_CTRL_REG1_XEN_ENABLE;      // Gyroscope X-Axis Enabled
    
    config1 |= lsm_gyrodatarate;       // Output Data Rate and Bandwidth Selection
    
    // Write the configuration to the Gyroscope Control Register 1
    writeRegister(lsm_i2cAddressG, LSM9DS0_REG_GYRO_CTRL_REG1, config1);
    
    // Wait for the configuration to complete
    delay(lsm_conversionDelay);
    
    // Set Up the Configuration for the Gyroscope Control Register 4
    /*
    // Set the Block Data Update
    uint8_t config4 = lsm_gyroblockdata;
    
    // Set the Big/Little Endian Data Selection
    config4 |= lsm_gyroendiandata;
    
    // Set the Full Scale Selection
    config4 |= lsm_gyroscale;
    
    // Set the Self-Test Enable
    config4 |= lsm_gyroselftest;
    
    // Set the SPI Serial Interface Mode Selection
    config4 |= lsm_gyrospiwire;
    */
    
    uint8_t config4 =   LSM9DS0_REG_GYRO_CTRL_REG4_BDU_CONTINUOUS   |   // Continuous Update
                        LSM9DS0_REG_GYRO_CTRL_REG4_BLE_LSB          |   // Data LSB @ Lower Address
                        LSM9DS0_REG_GYRO_CTRL_REG4_ST_NORMAL        |   // Normal Mode
                        LSM9DS0_REG_GYRO_CTRL_REG4_SIM_4WIRE;           // 4-Wire Interface
    
    config4 |= lsm_gyroscale;      // Full-Scale Selection
    
    // Write the configuration to the Gyroscope Control Register 4
    writeRegister(lsm_i2cAddressG, LSM9DS0_REG_GYRO_CTRL_REG4, config4);
    
    // Wait for the configuration to complete
    delay(lsm_conversionDelay);
}

/**************************************************************************/
/*
        Sets up the Accelerometer
*/
/**************************************************************************/
void LSM9DS0::setUpAccelerometer(void)
{
    // Set Up the Configuration for the Accelerometer/Magnetometer Control Register 1
    /*
    // Set the Acceleration Output Data Rate
    uint8_t config1 = lsm_acceldatarate;
    
    // Set the Block Data Update for Acceleration and Magnetic Data
    config1 |= lsm_accelmagblockdata;
    
    // Set the Acceleration Z-Axis Enable
    config1 |= lsm_accelzen;
    
    // Set the Acceleration Y-Axis Enable
    config1 |= lsm_accelyen;
    
    // Set the Acceleration X-Axis Enable
    config1 |= lsm_accelxen;
    */
    
    uint8_t config1 =   LSM9DS0_REG_ACCELMAG_CTRL_REG1_BDU_CONTINUOUS   |   // Continuous Update
                        LSM9DS0_REG_ACCELMAG_CTRL_REG1_AZEN_ENABLE      |   // Acceleration Z-Axis Enabled
                        LSM9DS0_REG_ACCELMAG_CTRL_REG1_AYEN_ENABLE      |   // Acceleration Y-Axis Enabled
                        LSM9DS0_REG_ACCELMAG_CTRL_REG1_AXEN_ENABLE;         // Acceleration X-Axis Enabled
    
    config1 |= lsm_acceldatarate;      // Acceleration Data Rate Configuration
    
    // Write the configuration to the Accelerometer/Magnetometer Control Register 1
    writeRegister(lsm_i2cAddressXM, LSM9DS0_REG_ACCELMAG_CTRL_REG1, config1);
    
    // Wait for the configuration to complete
    delay(lsm_conversionDelay);
    
    // Set Up the Configuration for the Accelerometer/Magnetometer Control Register 2
    /*
    // Set the Accelerometer Anti-Alias Filter Bandwidth
    uint8_t config2 = lsm_accelbandwidth;
    
    // Set the Acceleration Full-Scale Selection
    config2 |= lsm_accelrange;
    
    // Set the Acceleration Self-Test Enable
    config2 |= lsm_sccelselftest;
    
    // Set the SPI Serial Interface Mode Selection
    config2 |= lsm_accelmagspiwire;
    */
    
    uint8_t config2 =   LSM9DS0_REG_ACCELMAG_CTRL_REG2_ABW_773      |   // 773 Hz, Accelerometer Anti-Alias Filter Bandwidth
                        LSM9DS0_REG_ACCELMAG_CTRL_REG2_AST_NORMAL   |   // Normal Mode
                        LSM9DS0_REG_ACCELMAG_CTRL_REG2_SIM_4WIRE;       // 4-Wire Interface
    
    config2 |= lsm_accelrange;     // Acceleration Full-Scale Selection
    
    // Write the configuration to the Accelerometer/Magnetometer Control Register 2
    writeRegister(lsm_i2cAddressXM, LSM9DS0_REG_ACCELMAG_CTRL_REG2, config2);
        
    // Wait for the configuration to complete
    delay(lsm_conversionDelay);
}

/**************************************************************************/
/*
        Sets up the Magnetometer
*/
/**************************************************************************/
void LSM9DS0::setUpMagnetometer(void)
{
    // Set Up the Configuration for the Accelerometer/Magnetometer Control Register 5
    /*
    // Set the Temperature Sensor Enable
    uint8_t config5 = lsm_tempsensor;
    
    // Set the Magnetic Resolution Selection
    config5 |= lsm_magresolution;
    
    // Set the Magnetic Data Rate Selection
    config5 |= lsm_magdatarate;
    
    // Set the Latch Interrupt Request on INT2_SRC Register
    config5 |= lsm_magintr2latch;
    
    // Set the Latch Interrupt Request on INT1_SRC Register
    config5 |= lsm_magintr1latch;
    */
    
    uint8_t config5 =   LSM9DS0_REG_ACCELMAG_CTRL_REG5_TEMP_ENABLED     |   // Temperature Sensor Enabled
                        LSM9DS0_REG_ACCELMAG_CTRL_REG5_M_RES_HIGH;          // Magnetic Resolution High
    
    config5 |= lsm_magdatarate;     // Magnetic Data Rate Selection
    
    // Write the configuration to the Accelerometer/Magnetometer Control Register 5
    writeRegister(lsm_i2cAddressXM, LSM9DS0_REG_ACCELMAG_CTRL_REG5, config5);
    
    // Wait for the configuration to complete
    delay(lsm_conversionDelay);
    
    // Set Up the Configuration for the Accelerometer/Magnetometer Control Register 6
    // Set the Magnetic Full-Scale Selection
    uint8_t config6 = lsm_maggain;
        
    // Write the configuration to the Accelerometer/Magnetometer Control Register 6
    writeRegister(lsm_i2cAddressXM, LSM9DS0_REG_ACCELMAG_CTRL_REG6, config6);
    
    // Wait for the configuration to complete
    delay(lsm_conversionDelay);
    
    // Set Up the Configuration for the Accelerometer/Magnetometer Control Register 7
    
    uint8_t config7 =   LSM9DS0_REG_ACCELMAG_CTRL_REG7_AHPM_NORMAL  |   // Normal Mode
                        LSM9DS0_REG_ACCELMAG_CTRL_REG7_AFDS_BYPASS  |   // Internal Filter Bypassed
                        LSM9DS0_REG_ACCELMAG_CTRL_REG7_MLP_MODR     |   // Magnetic Data Rate is Configured by MODR Bits
                        LSM9DS0_REG_ACCELMAG_CTRL_REG7_MD_CONTINUOUS;   // Continuous-Conversion Mode
    
    // Set the Magnetic Sensor Mode
    config7 |= lsm_magmode;
    
    // Write the configuration to the Accelerometer/Magnetometer Control Register 7
    writeRegister(lsm_i2cAddressXM, LSM9DS0_REG_ACCELMAG_CTRL_REG7, config7);
    
    // Wait for the configuration to complete
    delay(lsm_conversionDelay);
}
0 Comments

Leave a reply

ControlAnything.com and ControlEverything Site Migration Notes

©2017 National Control Devices, LLC

ncd.io

To learn something new, take the path that you took yesterday. ~John Burroughs

Log in with your credentials

or    

Forgot your details?

Create Account