LSM6DS3 Breakout Hookup Guide

Pages
Contributors: MTaylor
Favorited Favorite 1

Using the Arduino Library

Test the library

  • MinimalistExample - The easiest configuration

Hook up the LSM6DS3 to the I2C bus, and click "File -> Examples -> Sparkfun LSM6DS3 Breakout -> MinimalistExample". This example demonstrates the highest level of usage. The default settings in the driver are I2C at the default address, 0x6B, so all you have to do is create a variable of the type "LSM6DS3" and .begin(); it.

Key parts:

language:c
LSM6DS3 myIMU; //Default constructor is I2C, addr 0x6B

This line creates a variable myIMU of type LSM6DS3. As the comment says, the default parameters are what we desire.

language:c
//Call .begin() to configure the IMU
myIMU.begin();

Calling .begin(); causes the driver to initialize the IMU. .begin can pass some diagnostics information back, but it's not necessary for this basic example.

language:c
  Serial.println(myIMU.readFloatAccelX(), 4);

Calling .readFloatAccelX() causes the driver to go get data from the IMU. In this line, the data is also passed to the print function.

Configure More Settings

  • FifoExample - Demonstrates using the built-in buffer to burst-collect data - Good demonstration of settings

To get the most out of the LSM6DS3 you'll probably want to configure it. This sketch was put together as a template that shows you ALL the settings that the driver supports, and you can remove lines you don't need.

The creation of myIMU has a dark secret. If you're not using I2C, address 0x6B (defualt), you can specify your port during the construction. From the example,

language:c
LSM6DS3 myIMU( SPI_MODE, 10 );

...we specify SPI_MODE, a custom keyword, and pin 10. This makes use of the SPI interface and uses pin 10 for the CS line. As the logic is not arduino specific, any pin can be CS.

Here's the low-down on the arguments that can be used.

language:c
LSM6DS3 <your variable name>( SPI_MODE, <CS PIN NUMBER> );

...or...

language:c
LSM6DS3 <your variable name>( I2C_MODE, <address> );

...where address can be 0x6A or 0x6B.

Note: This example pulls a lot of data from the IMU. SPI is used for better performance.

The other settings:

language:c
//Over-ride default settings if desired
myIMU.settings.gyroEnabled = 1;  //Can be 0 or 1
myIMU.settings.gyroRange = 2000;   //Max deg/s.  Can be: 125, 245, 500, 1000, 2000
myIMU.settings.gyroSampleRate = 833;   //Hz.  Can be: 13, 26, 52, 104, 208, 416, 833, 1666
myIMU.settings.gyroBandWidth = 200;  //Hz.  Can be: 50, 100, 200, 400;
myIMU.settings.gyroFifoEnabled = 1;  //Set to include gyro in FIFO
myIMU.settings.gyroFifoDecimation = 1;  //set 1 for on /1

myIMU.settings.accelEnabled = 1;
myIMU.settings.accelRange = 16;      //Max G force readable.  Can be: 2, 4, 8, 16
myIMU.settings.accelSampleRate = 833;  //Hz.  Can be: 13, 26, 52, 104, 208, 416, 833, 1666, 3332, 6664, 13330
myIMU.settings.accelBandWidth = 200;  //Hz.  Can be: 50, 100, 200, 400;
myIMU.settings.accelFifoEnabled = 1;  //Set to include accelerometer in the FIFO
myIMU.settings.accelFifoDecimation = 1;  //set 1 for on /1
myIMU.settings.tempEnabled = 1;

//Non-basic mode settings
myIMU.settings.commMode = 1;

//FIFO control settings
myIMU.settings.fifoThreshold = 100;  //Can be 0 to 4096 (16 bit bytes)
myIMU.settings.fifoSampleRate = 50;  //Hz.  Can be: 10, 25, 50, 100, 200, 400, 800, 1600, 3300, 6600
myIMU.settings.fifoModeWord = 6;  //FIFO mode.
//FIFO mode.  Can be:
//  0 (Bypass mode, FIFO off)
//  1 (Stop when full)
//  3 (Continuous during trigger)
//  4 (Bypass until trigger)
//  6 (Continous mode)

Read the datasheet, and select the parameters that suit your needs. They can only take on the values listed, or the default value will be used. If you don't care about a setting, omit that line.

A Low-Level Example

  • LowLevelExample - Demonstrates using only the core driver without math and settings overhead

This little example was put together to show you how to use the sensor without all the crazy floating point math. It saves memory in the processor (roughly half), but you won't have access to all the fancy math functions.

language:c
myIMU.writeRegister(LSM6DS3_ACC_GYRO_CTRL1_XL, dataToWrite)

and

language:c
myIMU.readRegister(&dataToWrite, LSM6DS3_ACC_GYRO_CTRL4_C);

show reading and writing to arbitrary registers

The Other Examples

  • InterruptFreeFall - Embedded function demonstrating free-fall detection
  • InterruptHWTapConfig - Embedded function demonstrating tap and double-tap detection
  • MemoryPagingExample - Demonstrates switching between memory pages
  • MultiI2C - Using two LSM6DS3s over I2C
  • MultiSPI - Using two LSM6DS3s over SPI
  • Pedometer - Embedded function demonstrating step-counting feature

User API Functions

Here's an explanation of the regular functions the user might call

Construction:

language:c
LSM6DS3( uint8_t busType, uint8_t inputArg );

busType can be SPI_MODE or I2C_MODE.

For SPI_MODE, inputArg specifies pin number for I2C_MODE, inputArg specifies either address 0x6A or 0x6B

language:c
status_t begin(void);

always call .begin(); before using the following functions!

language:c
int16_t readRawAccelX( void );
int16_t readRawAccelY( void );
int16_t readRawAccelZ( void );
int16_t readRawGyroX( void );
int16_t readRawGyroY( void );
int16_t readRawGyroZ( void );

These functions return the 16bit raw values from the LSM6DS3.

language:c
float readFloatAccelX( void );
float readFloatAccelY( void );
float readFloatAccelZ( void );
float readFloatGyroX( void );
float readFloatGyroY( void );
float readFloatGyroZ( void );

These functions return the floating point real-world values. Accel functions return in g's and Gyro return in degrees/second.

language:c
int16_t readRawTemp( void );

Gets the raw temperature value

language:c
float readTempC( void );
float readTempF( void );

Gets the temperature in your favorite scale (sorry, no kelvin)

These next functions operate the FIFO. Using the FIFO is more advanced and definitely requires consulting the datasheet for the LSM6DS3

language:c
void fifoBegin( void );

Configures the FIFO. This will make the FIFO start listening to Accelerometer data and/or gyroscope data, depending on the settings (the settings.accelFifoEnabled = 1; will include the accel in the fifo).

language:c
void fifoClear( void );

This clears the FIFO by reading out all the data and dumping it. If the FIFO is full and the bus is slow, this may take some time.

language:c
int16_t fifoRead( void );

Get the 16 bits of the next data coming out. This might be accelerometer data or gyroscope data, you can use the status bits to determine which (or by clearing out the FIFO completely and starting from a known reference).

language:c
uint16_t fifoGetStatus( void );

Get the 16 bits of the status word.

language:c
void fifoEnd( void );

Disables the FIFO.

Functions 'under the hood'

language:c
float calcGyro( int16_t );
float calcAccel( int16_t );

This converts raw values into real numbers. Internally, it uses the .settings to do the math. When using the high-level functions that return floating point numbers, these are internally called.

The only reason they are available to the user is because they can be used to convert the raw data coming out of the FIFO into real numbers. Though, if faster processing is needed (taking short-cuts or staying in raw integer math) they don't have to be used.