LSM6DSL Sensor

The ST LSM6DSL is a 3D digital accelerometer and gyroscope enabling always-on low-power features for an optimal motion experience.

Abstract Class of an LSM6DSL Inertial Measurement Unit (IMU) 6 axes sensor.

Assembly

LSM6DSLSensor.h

Summary

Types
DevI2C
PinName
LSM6DSL_Interrupt_Pin_t
LSM6DSL_Event_Status_t
Constructors
LSM6DSLSensor - LSM6DSLSensor(DevI2C &i2c, PinName int1_pin, PinName int2_pin)
LSM6DSLSensor - LSM6DSLSensor(DevI2C &i2c, PinName int1_pin, PinName int2_pin, uint8_t address)
Methods
init - int init(void *init)
enableAccelerator - int enableAccelerator(void)
enableGyroscope - int enableGyroscope(void)
disableAccelerator - int disableAccelerator(void)
disableGyroscope - int disableGyroscope(void)
readId - int readId(unsigned char *id)
getXAxes - int getXAxes(int *pData)
getGAxes - int getGAxes(int *pData)
getXSensitivity - int getXSensitivity(float *pfData)
getGSensitivity - int getGSensitivity(float *pfData)
getXAxesRaw - int getXAxesRaw(int16_t *pData)
getGAxesRaw - int getGAxesRaw(int16_t *pData)
getXOdr - int getXOdr(float* odr)
getGOdr - int getGOdr(float* odr)
setXOdr - int setXOdr(float odr)
setXOdrWhenEnabled - int setXOdrWhenEnabled(float odr)
setXOdrWhenDisabled - int setXOdrWhenDisabled(float odr)
setGOdr - int setGOdr(float odr)
setGOdrWhenEnabled - int setGOdrWhenEnabled(float odr)
setGOdrWhenDisabled - int setGOdrWhenDisabled(float odr)
getXFullScale - int getXFullScale(float* fullScale)
getGFullScale - int getGFullScale(float* fullScale)
setXFullScale - int setXFullScale(float fullScale)
setGFullScale - int setGFullScale(float fullScale)
enableFreeFallDetection - int enableFreeFallDetection(LSM6DSL_Interrupt_Pin_t pin)
disableFreeFallDetection - int disableFreeFallDetection(void)
setFreeFallThreshold - int setFreeFallThreshold(uint8_t thr)
enablePedometer - int enablePedometer(void)
disablePedometer - int disablePedometer(void)
getStepCounter - int getStepCounter(int *step_count)
resetStepCounter - int resetStepCounter(void)
setPedometerThreshold - int setPedometerThreshold(unsigned char thr)
enableTiltDetection - int enableTiltDetection(LSM6DSL_Interrupt_Pin_t pin)
disableTiltDetection - int disableTiltDetection(void)
enableWakeUpDetection - int enableWakeUpDetection(LSM6DSL_Interrupt_Pin_t pin)
disableWakeUpDetection - int disableWakeUpDetection(void)
setWakeUpThreshold - int setWakeUpThreshold(unsigned char thr)
enableSingleTapDetection - int enableSingleTapDetection(LSM6DSL_Interrupt_Pin_t pin)
disableSingleTapDetection - int disableSingleTapDetection(void)
enableDoubleTapDetection - int enableDoubleTapDetection(LSM6DSL_Interrupt_Pin_t pin)
disableDoubleTapDetection - int disableDoubleTapDetection(void)
setTapThreshold - int setTapThreshold(unsigned char thr)
setTapShockTime - int setTapShockTime(uint8_t time)
setTapQuietTime - int setTapQuietTime(uint8_t time)
setTapDurationTime - int setTapDurationTime(uint8_t time)
enable6dOrientation - int enable6dOrientation(LSM6DSL_Interrupt_Pin_t pin)
disable6dOrientation - int disable6dOrientation(void)
get6dOrientationXL - int get6dOrientationXL(uint8_t *xl)
get6dOrientationXH - int get6dOrientationXH(uint8_t *xh)
get6dOrientationYL - int get6dOrientationYL(uint8_t *yl)
get6dOrientationYH - int get6dOrientationYH(uint8_t *yh)
get6dOrientationZL - int get6dOrientationZL(uint8_t *zl)
get6dOrientationZH - int get6dOrientationZH(uint8_t *zh)
getEventStatus - int getEventStatus(LSM6DSL_Event_Status_t *status)
readReg - int readReg(uint8_t reg, uint8_t *data)
writeReg - int writeReg(uint8_t reg, uint8_t data)

Types

DevI2C

Provides functions for multi-register I2C communication.

PinName

Provides the mapping of mbed DIP and LPC Pin Names.

LSM6DSL_Interrupt_Pin_t

LSM6DSL interrupt pin type

LSM6DSL_Event_Status_t

LSM6DSL event status type

Constructors

LSM6DSLSensor

LSM6DSLSensor(DevI2C &i2c, PinName int1_pin, PinName int2_pin)

Parameters

Type Name Description
DevI2C & i2c The object of an helper class which handles the I2C peripheral.
PinName int1_pin LSM6DSL interrupt pin name.
PinName int2_pin LSM6DSL interrupt pin name.

LSM6DSLSensor

LSM6DSLSensor(DevI2C &i2c, PinName int1_pin, PinName int2_pin, uint8_t address)

Parameters

Type Name Description
DevI2C & i2c The object of an helper class which handles the I2C peripheral.
PinName int1_pin LSM6DSL interrupt pin name.
PinName int2_pin LSM6DSL interrupt pin name.
uint8_t address The address of the component’s instance.

Methods

init

int init(void *init)

Initializing the component.

Parameters

Type Name Description
void * init Pointer to device specific initalization structure.

Return value

Type Description
int 0 in case of success, an error code otherwise.

enableAccelerator

int enableAccelerator(void)

Enable LSM6DSL Accelerator.

Parameters

None.

Return value

Type Description
int 0 in case of success, an error code otherwise.

enableGyroscope

int enableGyroscope(void)

Enable LSM6DSL Gyroscope.

Parameters

None.

Return value

Type Description
int 0 in case of success, an error code otherwise.

disableAccelerator

int disableAccelerator(void)

Disable LSM6DSL Accelerator.

Parameters

None.

Return value

Type Description
int 0 in case of success, an error code otherwise.

disableGyroscope

int disableGyroscope(void)

Disable LSM6DSL Gyroscope.

Parameters

None.

Return value

Type Description
int 0 in case of success, an error code otherwise.

readId

int readId(unsigned char *id)

Read ID of LSM6DSL Accelerometer and Gyroscope.

Parameters

Type Name Description
unsigned char * id The pointer where the ID of the device is stored.

Return value

Type Description
int 0 in case of success, an error code otherwise.

getXAxes

int getXAxes(int *pData)

Read data from LSM6DSL Accelerometer.

Parameters

Type Name Description
int32_t * pData The pointer where the accelerometer data are stored.

Return value

Type Description
int 0 in case of success, an error code otherwise.

getGAxes

int getGAxes(int *pData)

Read data from LSM6DSL Gyroscope.

Parameters

Type Name Description
int * pData The pointer where the gyroscope data are stored

Return value

Type Description
int 0 in case of success, an error code otherwise.

getXSensitivity

int getXSensitivity(float *pfData)

Read Accelerometer Sensitivity.

Parameters

Type Name Description
float * pfData The pointer where the accelerometer sensitivity is stored

Return value

Type Description
int 0 in case of success, an error code otherwise.

getGSensitivity

int getGSensitivity(float *pfData)

Read Gyroscope Sensitivity.

Parameters

Type Name Description
float * pfData The pointer where the gyroscope sensitivity is stored.

Return value

Type Description
int 0 in case of success, an error code otherwise.

getXAxesRaw

int getXAxesRaw(int16_t *pData)

Read raw data from LSM6DSL Accelerometer.

Parameters

Type Name Description
int * pData The pointer where the accelerometer raw data are stored

Return value

Type Description
int 0 in case of success, an error code otherwise.

getGAxesRaw

int getGAxesRaw(int16_t *pData)

Read raw data from LSM6DSL Gyroscope.

Parameters

Type Name Description
int16_t * pData The pointer where the gyroscope raw data are stored.

Return value

Type Description
int 0 in case of success, an error code otherwise.

getXOdr

int getXOdr(float *odr)

Read LSM6DSL Accelerometer output data rate.

Parameters

Type Name Description
float * odr The pointer to the output data rate.

Return value

Type Description
int 0 in case of success, an error code otherwise.

getGOdr

int getGOdr(float *odr)

Read LSM6DSL Gyroscope output data rate.

Parameters

Type Name Description
float * odr The pointer to the output data rate.

Return value

Type Description
int 0 in case of success, an error code otherwise.

setXOdr

int setXOdr(float odr)

Set LSM6DSL Accelerometer output data rate.

Parameters

Type Name Description
float odr The output data rate to be set.

Return value

Type Description
int 0 in case of success, an error code otherwise.

setXOdrWhenEnabled

int setXOdrWhenEnabled(float odr)

Set LSM6DSL Accelerometer output data rate when enabled.

Parameters

Type Name Description
float odr The output data rate to be set.

Return value

Type Description
int 0 in case of success, an error code otherwise.

setXOdrWhenDisabled

int setXOdrWhenDisabled(float odr)

Set LSM6DSL Accelerometer output data rate when disabled.

Parameters

Type Name Description
float odr The output data rate to be set.

Return value

Type Description
int 0 in case of success, an error code otherwise.

setGOdr

int setGOdr(float odr)

Set LSM6DSL Gyroscope output data rate.

Parameters

Type Name Description
float odr The output data rate to be set.

Return value

Type Description
int 0 in case of success, an error code otherwise.

setGOdrWhenEnabled

int setGOdrWhenEnabled(float odr)

Set LSM6DSL Gyroscope output data rate when enabled.

Parameters

Type Name Description
float odr The output data rate to be set.

Return value

Type Description
int 0 in case of success, an error code otherwise.

setGOdrWhenDisabled

int setGOdrWhenDisabled(float odr)

Set LSM6DSL Gyroscope output data rate when disabled.

Parameters

Type Name Description
float odr The output data rate to be set.

Return value

Type Description
int 0 in case of success, an error code otherwise.

getXFullScale

int getXFullScale(float *fullScale)

Read LSM6DSL Accelerometer full scale.

Parameters

Type Name Description
float * fullScale The pointer to the full scale.

Return value

Type Description
int 0 in case of success, an error code otherwise.

getGFullScale

int getGFullScale(float *fullScale)

Read LSM6DSL Gyroscope full scale.

Parameters

Type Name Description
float * fullScale The pointer to the full scale.

Return value

Type Description
int 0 in case of success, an error code otherwise.

setXFullScale

int setXFullScale(float fullScale)

Set LSM6DSL Accelerometer full scale

Parameters

Type Name Description
float fullScale The full scale to be set.

Return value

Type Description
int 0 in case of success, an error code otherwise.

setGFullScale

int setGFullScale(float fullScale)

Set LSM6DSL Gyroscope full scale.

Parameters

Type Name Description
float fullScale The full scale to be set.

Return value

Type Description
int 0 in case of success, an error code otherwise.

enableFreeFallDetection

int enableFreeFallDetection(LSM6DSL_Interrupt_Pin_t pin)

Enable free fall detection. This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g.

Parameters

Type Name Description
LSM6DSL_Interrupt_Pin_t pin The interrupt pin to be used.

Return value

Type Description
int 0 in case of success, an error code otherwise.

disableFreeFallDetection

int disableFreeFallDetection(void)

Disable free fall detection.

Parameters

None.

Return value

Type Description
int 0 in case of success, an error code otherwise.

setFreeFallThreshold

int setFreeFallThreshold(int thr)

Set the free fall detection threshold for LSM6DSL accelerometer sensor.

Parameters

Type Name Description
int thr The threshold to be set.

Return value

Type Description
int 0 in case of success, an error code otherwise.

enablePedometer

int enablePedometer(void)

Enable the pedometer feature for LSM6DSL accelerometer sensor. This function sets the LSM6DSL accelerometer ODR to 26Hz and the LSM6DSL accelerometer full scale to 2g.

Parameters

None.

Return value

Type Description
int 0 in case of success, an error code otherwise.

disablePedometer

int disablePedometer(void)

Disable the pedometer feature for LSM6DSL accelerometer sensor.

Parameters

None.

Return value

Type Description
int 0 in case of success, an error code otherwise.

getStepCounter

int getStepCounter(int *step_count)

Get the step counter for LSM6DSL accelerometer sensor.

Parameters

Type Name Description
int * step_count The pointer to the step counter.

Return value

Type Description
int 0 in case of success, an error code otherwise.

resetStepCounter

int resetStepCounter(void)

Reset of the step counter for LSM6DSL accelerometer sensor.

Parameters

None.

Return value

Type Description
int 0 in case of success, an error code otherwise.

setPedometerThreshold

int setPedometerThreshold(unsigned char thr)

Set the pedometer threshold for LSM6DSL accelerometer sensor.

Parameters

Type Name Description
unsigned char thr The threshold to be set.

Return value

Type Description
int 0 in case of success, an error code otherwise.

enableTiltDetection

int enableTiltDetection(LSM6DSL_Interrupt_Pin_t pin)

Enable the tilt detection for LSM6DSL accelerometer sensor. This function sets the LSM6DSL accelerometer ODR to 26Hz and the LSM6DSL accelerometer full scale to 2g.

Parameters

Type Name Description
LSM6DSL_Interrupt_Pin_t pin The interrupt pin to be used.

Return value

Type Description
int 0 in case of success, an error code otherwise.

disableTiltDetection

int disableTiltDetection(void)

Disable the tilt detection for LSM6DSL accelerometer sensor.

Parameters

None.

Return value

Type Description
int 0 in case of success, an error code otherwise.

enableWakeUpDetection

int enableWakeUpDetection(LSM6DSL_Interrupt_Pin_t pin)

Enable the wake up detection for LSM6DSL accelerometer sensor. This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g.

Parameters

Type Name Description
LSM6DSL_Interrupt_Pin_t pin The interrupt pin to be used.

Return value

Type Description
int 0 in case of success, an error code otherwise.

disableWakeUpDetection

int disableWakeUpDetection(void)

Disable the wake up detection for LSM6DSL accelerometer sensor.

Parameters

None.

Return value

Type Description
int 0 in case of success, an error code otherwise.

setWakeUpThreshold

int setWakeUpThreshold(unsigned char thr)

Set the wake up threshold for LSM6DSL accelerometer sensor.

Parameters

Type Name Description
unsigned char thr The threshold to be set.

Return value

Type Description
int 0 in case of success, an error code otherwise.

enableSingleTapDetection

int enableSingleTapDetection(LSM6DSL_Interrupt_Pin_t pin)

Enable the single tap detection for LSM6DSL accelerometer sensor. This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g.

Parameters

Type Name Description
LSM6DSL_Interrupt_Pin_t pin The interrupt pin to be used.

Return value

Type Description
int 0 in case of success, an error code otherwise.

disableSingleTapDetection

int disableSingleTapDetection(void)

Disable the single tap detection for LSM6DSL accelerometer sensor.

Parameters

None.

Return value

Type Description
int 0 in case of success, an error code otherwise.

enableDoubleTapDetection

int enableDoubleTapDetection(LSM6DSL_Interrupt_Pin_t pin)

Enable the double tap detection for LSM6DSL accelerometer sensor. This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g.

Parameters

Type Name Description
LSM6DSL_Interrupt_Pin_t pin The interrupt pin to be used.

Return value

Type Description
int 0 in case of success, an error code otherwise.

disableDoubleTapDetection

int disableDoubleTapDetection(void)

Disable the double tap detection for LSM6DSL accelerometer sensor.

Parameters

None.

Return value

Type Description
int 0 in case of success, an error code otherwise.

setTapThreshold

int setTapThreshold(unsigned char thr)

Set the tap threshold for LSM6DSL accelerometer sensor.

Parameters

Type Name Description
unsigned char thr The threshold to be set.

Return value

Type Description
int 0 in case of success, an error code otherwise.

setTapShockTime

int setTapShockTime(uint8_t time)

Set the tap shock time window for LSM6DSL accelerometer sensor.

Parameters

Type Name Description
uint8_t time The shock time window to be set.

Return value

Type Description
int 0 in case of success, an error code otherwise.

setTapQuietTime

int setTapQuietTime(uint8_t time)

Set the tap quiet time window for LSM6DSL accelerometer sensor.

Parameters

Type Name Description
uint8_t time The quiet time window to be set.

Return value

Type Description
int 0 in case of success, an error code otherwise.

setTapDurationTime

int setTapDurationTime(uint8_t time)

Set the tap duration of the time window for LSM6DSL accelerometer sensor.

Parameters

Type Name Description
uint8_t time The duration of the time window to be set.

Return value

Type Description
int 0 in case of success, an error code otherwise.

enable6dOrientation

int enable6dOrientation(LSM6DSL_Interrupt_Pin_t pin)

Enable the 6D orientation detection for LSM6DSL accelerometer sensor. This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g.

Parameters

Type Name Description
LSM6DSL_Interrupt_Pin_t pin The interrupt pin to be used.

Return value

Type Description
int 0 in case of success, an error code otherwise.

disable6dOrientation

int disable6dOrientation(void)

Disable the 6D orientation detection for LSM6DSL accelerometer sensor.

Parameters

None.

Return value

Type Description
int 0 in case of success, an error code otherwise.

get6dOrientationXL

int get6dOrientationXL(uint8_t *xl)

Get the 6D orientation XL axis for LSM6DSL accelerometer sensor.

Parameters

Type Name Description
uint8_t * xl The pointer to the 6D orientation XL axis.

Return value

Type Description
int 0 in case of success, an error code otherwise.

get6dOrientationXH

int get6dOrientationXH(uint8_t *xh)

Get the 6D orientation XH axis for LSM6DSL accelerometer sensor.

Parameters

Type Name Description
uint8_t * xh The pointer to the 6D orientation XH axis.

Return value

Type Description
int 0 in case of success, an error code otherwise.

get6dOrientationYL

int get6dOrientationYL(uint8_t *yl)

Get the 6D orientation YL axis for LSM6DSL accelerometer sensor.

Parameters

Type Name Description
uint8_t * yl The pointer to the 6D orientation YL axis

Return value

Type Description
int 0 in case of success, an error code otherwise.

get6dOrientationYH

int get6dOrientationYH(uint8_t *yh)

Get the 6D orientation YH axis for LSM6DSL accelerometer sensor.

Parameters

Type Name Description
uint8_t * yh The pointer to the 6D orientation YH axis.

Return value

Type Description
int 0 in case of success, an error code otherwise.

get6dOrientationZL

int get6dOrientationZL(uint8_t *zl)

Get the 6D orientation ZL axis for LSM6DSL accelerometer sensor.

Parameters

Type Name Description
uint8_t * zl The pointer to the 6D orientation ZL axis.

Return value

Type Description
int 0 in case of success, an error code otherwise.

get6dOrientationZH

int get6dOrientationZH(uint8_t *zh)

Get the 6D orientation ZH axis for LSM6DSL accelerometer sensor.

Parameters

Type Name Description
uint8_t * zh The pointer to the 6D orientation ZH axis.

Return value

Type Description
int 0 in case of success, an error code otherwise.

getEventStatus

int getEventStatus(LSM6DSL_Event_Status_t *status)

Get the status of all hardware events for LSM6DSL accelerometer sensor.

Parameters

Type Name Description
LSM6DSL_Event_Status_t * status The pointer to the status of all hardware events.

Return value

Type Description
int 0 in case of success, an error code otherwise.

readReg

int readReg(uint8_t reg, uint8_t *data)

Parameters

Type Name Description
uint8_t reg Register address.
uint8_t * data Register data.

Return value

Type Description
int 0 in case of success, an error code otherwise.

writeReg

int writeReg(uint8_t reg, uint8_t data)

Write the data to register.

Parameters

Type Name Description
uint8_t reg Register address.
uint8_t * data Register data.

Return value

Type Description
int 0 in case of success, an error code otherwise.

Sample code

#include "LSM6DSLSensor.h"
DevI2C *i2c;
LSM6DSLSensor *sensor;
int32_t axes[3];
int16_t raws[3];
float data;
void setup(){
    i2c = new DevI2C(D14, D15);
    sensor = new LSM6DSLSensor(*i2c, D4, D5);
    // init
    sensor->init(NULL);
}
void loop(){
    // Accelerometer test
    accelerometer_test();
    // Gyroscope test
    gyroscope_test();
    delay(1000);
}
void accelerometer_test(){
    Serial.println("***Accelerator***");
    // enableAccelerator
    sensor->enableAccelerator();
    // getXAxes
    sensor->getXAxes(axes);
    Serial.printf("Axes: x: %d, y: %d, z: %d\n", axes[0], axes[1], axes[2]);
    // getXSensitivity
    sensor->getXSensitivity(&data);
    Serial.print("Sensitivity: ");
    Serial.println(data);
    // getXAxesRaw
    sensor->getXAxesRaw(raws);
    Serial.printf("Raw: x: %d, y: %d, z: %d\n", raws[0], raws[1], raws[2]);
}
void gyroscope_test(){
    Serial.println("***Gyroscope***");
    // enableGyroscope
    sensor->enableGyroscope();
    // getGAxes
    sensor->getGAxes(axes);
    Serial.printf("Axes: x: %d, y: %d, z: %d\n", axes[0], axes[1], axes[2]);
    // getGSensitivity
    sensor->getGSensitivity(&data);
    Serial.print("Sensitivity: ");
    Serial.println(data);
    // getGAxesRaw
    sensor->getGAxesRaw(raws);
    Serial.printf("Raw: x: %d, y: %d, z: %d\n", raws[0], raws[1], raws[2]);
}