LSM9DS0 Hookup Guide
Using the Arduino Library
Those two basic and advanced tutorials show off everything that the SFE_LSM9DS0 library can do. If you're stumped on how to use the library, though, here are some of its key concepts and functions:
To enable the library, you'll need to include it, and you also need to include the SPI and Wire libraries:
language:c #include <SPI.h> // Included for SFE_LSM9DS0 library #include <Wire.h> #include <SFE_LSM9DS0.h>
Make sure the SPI and Wire includes are above the SFE_LSM9DS0.
The constructor creates an instance of the LSM9DS0 class. Once you've created the instance, that's what you'll use to control the breakout from there on. This single line of code is usually placed in the global area of your sketch.
The constructor tells the library three things: whether you're using I2C or SPI, and the addresses of the gyroscope and accelerometer/magnetomter sensors. If you're using I2C, those address are the 7-bit address defined in the datasheet
language:c // SDO_XM and SDO_G are both grounded, so our addresses are: #define LSM9DS0_XM 0x1D // Would be 0x1E if SDO_XM is LOW #define LSM9DS0_G 0x6B // Would be 0x6A if SDO_G is LOW // Create an instance of the LSM9DS0 library called `dof` the // parameters for this constructor are: // [SPI or I2C Mode declaration],[gyro I2C address],[xm I2C add.] LSM9DS0 dof(MODE_I2C, LSM9DS0_G, LSM9DS0_XM);
If you're using SPI, the gyro and accel/mag addresses should be the Arduino pin connected to each chip-select pin (CSG and CSXM).
language:c #define LSM9DS0_CSG 9 // CSG connected to Arduino pin 9 #define LSM9DS0_CSXM 10 // CSXM connected to Arduino pin 10 LSM9DS0 dof(MODE_SPI, LSM9DS0_CSG, LSM9DS0_CSXM);
Your LSM9DS0 class object can be named like any other variable. In our examples we called it dof -- short and sweet.
Once you've created an LSM9DS0 object, you can start using it! The first step is initializing the sensor, by using the
begin() function. You can either call this function with no parameters, to get a good, default init:
language:c // Initialize LSM9DS0, setting gyro scale to 245 DPS, accel to 2g, and mag to 2Gs. // Also set data rates to 95 Hz (gyro), 50 Hz (accel), and 50 Hz (mag). dof.begin();
Or, give it a whole set of parameters that set each sensors scale and data rate.
language:c // Initialize LSM9DS0, setting gyro scale to 500 DPS, accel to +/-16g, and mag to 12Gs. // Also set data rates to 760 Hz (gyro), 1600 Hz (accel), and 100 Hz (mag) (lots of data!) dof.begin(G_SCALE_500DPS, A_SCALE_16G, M_SCALE_12GS, G_ODR_760_BW_100, A_ODR_1600, M_ODR_100);
There are a variety of options for the scale and data rate selections. Consult SFE_LSM9DS0.h to find out more.
Reading and Interpreting the Sensors
What good is the sensor if you can't get any data from it!? Here are the functions you'll need to get acceleration, rotation speed, and magnetic field strength dat from the library.
readAccel(), readGyro(), and readMag()
These three functions --
readMag() -- poll the LSM9DS0 to get the most up-to-date readings from each of the three sensors.
The read functions don't take any parameters, and they don't return anything, so how do you get that data? After the function runs its course, it'll update a set of three class variables, which will have the sensor data you so desire.
readAccel() will update
readGyro() will update
readMag() will update
mz. Here's an example:
language:c dof.readAccel(); // Update the accelerometer data Serial.print(dof.ax); // Print x-axis data Serial.print(", "); Serial.print(dof.ay); // print y-axis data Serial.print(", "); Serial.println(dof.az); // print z-axis data
Those values are all signed 16-bit integers, meaning they'll range from -32,768 to 32,767. That value doesn't mean much unless you know the scale of your sensor, which is where the next functions come into play.
calcAccel(), calcGyro(), and calcMag()
The library keeps track of each sensor's scale, and it implements these helper functions to make translating between the raw ADC readings of the sensor to actual units easy.
calcMag() all take a single parameter -- a signed 16-bit integer -- and convert to their respective units. They all return a float value, which you can do with as you please.
Here's an example of printing calculated gyroscope values:
language:c dof.readGyro(); // Update gyroscope data Serial.print(dof.calcGyro(dof.gx)); // Print x-axis rotation in DPS Serial.print(", "); Serial.print(dof.calcGyro(dof.gy)); // Print y-axis rotation in DPS Serial.print(", "); Serial.println(dof.calcGyro(dof.gz)); // Print z-axis rotation in DPS
The library also implements functions to individually set a sensor's scale or data rate. For more help using the library, check out comments in the example code, or delve into the library code itself.