Arduino Code

Wiring for Flora

Flora uses conductive thread or aligator clips, connect up the 3V/SDA/SCL/GND pins to the matching pins in the north-west quarter of the Flora board. We suggest alligator clips to test, then sew in with thread once it works!

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_LSM9DS0

To begin reading sensor data, you will need to download the Adafruit_LSM9DS0 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_LSM9DS0 and check that the Adafruit_LSM9DS0 folder contains Adafruit_LSM9DS0.cpp and Adafruit_LSM9DS0.h

Place the Adafruit_LSM9DS0 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_LSM9DS0 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_LSM9DS0

Restart the IDE!

Load Demo Sketch

Now you can open up File->Examples->Adafruit_LSM9DS0->lsm9doftest and upload to your Arduino wired up to the sensor
The LSM9DS0 sensor must be wired up to the board for the demo to work!
Then open up the Serial console at 9600 baud to read the raw data output in 'counts'. This is data directly from the sensor, and isn't in any particular units.
This output is not terribly useful for most people - its simpler but does not have adjusted output units. Instead, we suggest the sensorapi demo. Upload that to the Arduino and open up the Serial console again.

This time, you'll get the outputs in m/s*s, gauss an degrees-per-second
We suggest using this Adafruit_Sensor interface version, 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_LSM9DS0 object with:
Download: file
Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0();  // 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_XM & SDO_G -> 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_LSM9DS0 lsm = Adafruit_LSM9DS0(LSM9DS0_XM_CS, LSM9DS0_GYRO_CS);
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_LSM9DS0 lsm = Adafruit_LSM9DS0(LSM9DS0_SCLK, LSM9DS0_MISO, LSM9DS0_MOSI, LSM9DS0_XM_CS, LSM9DS0_GYRO_CS);

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 LSM9DS0 ... check your connections */
    Serial.print(F("Ooops, no LSM9DS0 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.LSM9DS0_ACCELRANGE_2G);
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_4G);
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_6G);
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_8G);
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_16G);
  
  // 2.) Set the magnetometer sensitivity
  lsm.setupMag(lsm.LSM9DS0_MAGGAIN_2GAUSS);
  //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_4GAUSS);
  //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_8GAUSS);
  //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_12GAUSS);

  // 3.) Setup the gyroscope
  lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_245DPS);
  //lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_500DPS);
  //lsm.setupGyro(lsm.LSM9DS0_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, in degrees C
This guide was first published on Aug 04, 2014. It was last updated on Aug 04, 2014. This page (Arduino Code) was last updated on Jun 16, 2019.