Arduino Code

Wiring for Arduino

You can easily wire this breakout to any microcontroller, we'll be using an Arduino. For another kind of microcontroller, just make sure it has I2C or SPI, then port the code - its pretty simple stuff!

Let's start with just I2C interfacing since it requires the fewest # of wires:
  • Connect Vin to the power supply, 3-5V is fine. Use the same voltage that the microcontroller logic is based off of. For most Arduinos, that is 5V
  • Connect GND to common power/data ground
  • Connect the SCL pin to the I2C clock SCL pin on your Arduino. On an UNO & '328 based Arduino, this is also known as A5, on a Mega it is also known as digital 21 and on a Leonardo/Micro, digital 3
  • Connect the SDA pin to the I2C data SDA pin on your Arduino. On an UNO & '328 based Arduino, this is also known as A4, on a Mega it is also known as digital 20 and on a Leonardo/Micro, digital 2

Download Adafruit_LSM9DS1

To begin reading sensor data, you will need to download the Adafruit_LSM9DS1 Library from our github repository. You can do that by visiting the github repo and manually downloading or, easier, just click this button to download the zip

Rename the uncompressed folder Adafruit_LSM9DS1 and check that the Adafruit_LSM9DS1 folder contains Adafruit_LSM9DS1.cpp and Adafruit_LSM9DS1.h

Place the Adafruit_LSM9DS1 library folder your arduinosketchfolder/libraries/ folder.
You may need to create the libraries subfolder if its your first library. Restart the IDE.

We also have a great tutorial on Arduino library installation at:
http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use

Download Adafruit_Sensor

The Adafruit_LSM9DS1 library uses the Adafruit_Sensor support backend so that readings can be normalized between sensors. You can grab Adafruit_Sensor from the github repo or just click the button below.

Install like you did with Adafruit_LSM9DS1

Restart the IDE!

Load Demo Sketch

Now you can open up File->Examples->Adafruit_LSM9DS1->lsm9ds1 and upload to your Arduino wired up to the sensor

Then open up the Serial console at 115200 baud to read the sensor output! You'll get 9 distinct data points, accelerometer x/y/z in meters/s2, magetometer x/y/z in gauss and gyroscope in x/y/x degrees/second

We suggest using this Adafruit_Sensor interface as shown in this demo, since it will let you swap sensors without having to worry about units compatibility. Try twisting and moving the board around to see the sensors change value.

Library Reference

The library we have is simple and easy to use

You can create the Adafruit_LSM9DS1 object with:

Download: file
Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1();  // i2c sensor

I2C does not have pins, as they are fixed in hardware.

If you're using "hardware" SPI, you will have to wire up the pins as follows:

  • SCL -> SPI CLK
  • SDA -> SPI MOSI
  • SDO_AG & SDO_M -> SPI MISO (both together)

You can determine the hardware SPI pins for your Arduino here Then pick two pins for the CS lines

Download: file
Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1(LSM9DS1_XGCS, LSM9DS1_MCS);
If you don't want to use the hardware SPI, you can also try the soft SPI capability, which is bitbanged. You can basically use any pins you like!
Download: file
Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1(LSM9DS1_SCK, LSM9DS1_MISO, LSM9DS1_MOSI, LSM9DS1_XGCS, LSM9DS1_MCS);

Begin!

To initialize the sensor, call lsm.begin() which will check the sensor can be found. It returns true/false depending on these checks. We suggest you wrap begin() in a statement that will check if the sensor was located:
Download: file
if(!lsm.begin())
  {
    /* There was a problem detecting the LSM9DS1 ... check your connections */
    Serial.print(F("Ooops, no LSM9DS1 detected ... Check your wiring!"));
    while(1);
  }

Set Ranges

These chips have tons of registers, we basically provide interface code for the most useful stuff, such as setting the range. Each subsensor has it's own range. Higher ranges have less precision but can measure larger movements!
Set up the ranges with the setup functions:
Download: file
// 1.) Set the accelerometer range
  lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_2G);
  //lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_4G);
  //lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_8G);
  //lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_16G);
  
  // 2.) Set the magnetometer sensitivity
  lsm.setupMag(lsm.LSM9DS1_MAGGAIN_4GAUSS);
  //lsm.setupMag(lsm.LSM9DS1_MAGGAIN_8GAUSS);
  //lsm.setupMag(lsm.LSM9DS1_MAGGAIN_12GAUSS);
  //lsm.setupMag(lsm.LSM9DS1_MAGGAIN_16GAUSS);

  // 3.) Setup the gyroscope
  lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_245DPS);
  //lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_500DPS);
  //lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_2000DPS);
Choose whichever range you like, after you begin() the sensor!

Read data

Read data using the Adafruit_Sensor API by first creating four events, one for each sub-sensor:
Download: file
sensors_event_t accel, mag, gyro, temp;
Then pass these into the getEvent function
Download: file
lsm.getEvent(&accel, &mag, &gyro, &temp);

The data is snapshotted at once, so you can read and manage the data later.

For the Accelerometer event you can read accel.acceleration.x, accel.acceleration.y or accel.acceleration.z which are in meters/second*second.

For the Magnetometer event you can read mag.magnetic.x, mag.magnetic.y or mag.magnetic.z which are in gauss.

For the Gyro event you can read gyro.gyro.x, gyro.gyro.y or gyro.gyro.z, which are in degrees-per-second (dps)

The temperature event data is in temp.temperature, but we don't guarantee that the temperature data is in degrees C

This guide was first published on Feb 01, 2017. It was last updated on Feb 01, 2017. This page (Arduino Code) was last updated on Oct 12, 2019.