LSM303C 6DoF Hookup Guide

Contributors: .Brent.
Favorited Favorite 1

Installing the Arduino Library

Download and Install the Library

Note: This code/library has been written and tested on Arduino IDE version 1.8.9. Otherwise, make sure you are using the latest stable version of the Arduino IDE on your desktop.

If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE. If you have not previously installed an Arduino library, please check out our installation guide.

Visit the GitHub repository to download the most recent version of the libraries, or click the link below to manually install:

The example Arduino code allows you to do things like read the magnetometer in all 3 axis, read the accelerometer in all 3 axis, and read the temperature of the die in Fahrenheit and Celsius.

Minimalist Example

Now, you can now run the example sketches. Open FileExamplesSparkFun LSM303C 6 DOF IMU Breakout Arduino LibraryMinimalistExample. This sketch is a simple as possible other than a little error checking. We'll be using the example in I2C mode.

The setup function configures the Arduino's serial port to 57600 baud and configures the LSM303C to some reasonable defaults.

LSM303C myIMU;

void setup() {

  Wire.begin();//set up I2C bus, comment out if using SPI mode
  Wire.setClock(400000L);//clock stretching, comment out if using SPI mode

  Serial.begin(57600);//initialize serial monitor, maximum reliable baud for 3.3V/8Mhz ATmega328P is 57600

  if (myIMU.begin() != IMU_SUCCESS)
    Serial.println("Failed setup.");

The loop function sequentially prints out the x, y, and z vales measured by the accelerometer, then the gyroscope, and then the magnetometer. This is followed up by the temperature of the die in degrees Celsius and Fahrenheit. All values are rounded to 4 digits past the decimal point.

void loop()
  //Get all parameters
  Serial.print(" X = ");
  Serial.println(myIMU.readAccelX(), 4);
  Serial.print(" Y = ");
  Serial.println(myIMU.readAccelY(), 4);
  Serial.print(" Z = ");
  Serial.println(myIMU.readAccelZ(), 4);

  // Not supported by hardware, so will return NAN
  Serial.print(" X = ");
  Serial.println(myIMU.readGyroX(), 4);
  Serial.print(" Y = ");
  Serial.println(myIMU.readGyroY(), 4);
  Serial.print(" Z = ");
  Serial.println(myIMU.readGyroZ(), 4);

  Serial.print(" X = ");
  Serial.println(myIMU.readMagX(), 4);
  Serial.print(" Y = ");
  Serial.println(myIMU.readMagY(), 4);
  Serial.print(" Z = ");
  Serial.println(myIMU.readMagZ(), 4);

  Serial.print(" Degrees C = ");
  Serial.println(myIMU.readTempC(), 4);
  Serial.print(" Degrees F = ");
  Serial.println(myIMU.readTempF(), 4);

Here is some sample output:

 X = 56.7017
 Y = 42.7856
 Z = 946.2891

 X = nan
 Y = nan
 Z = nan

 X = -0.2051
 Y = 0.0527
 Z = 0.0742

 Degrees C = 24.5000
 Degrees F = 76.1000

You may have noticed that gyro data is printed despite there not being a gyro on this board. If a value is not available, the library functions will return nan (not a number). In this case the LSM303C doesn't have a gyroscope, but it still returns a value for a consistent IMU interface. Any IMU library that implements the SparkFunIMU abstract class can be swapped out without having to change the code that uses the library. If at some point in the future it is determined that a project needs more or less degrees of freedom (and is coded with error checking) the code change is trivial. Change #include "SparkFunLSM303C.h" to #include "SparkFun<some other sensor>.h", and LSM303C myIMU; to <some other sensor> myIMU. If for whatever reason the sensor doesn't have valid data ready for a sensor that does exist, it will also return nan. This indicates that the value is undefined.

Configure Example

By default, the easy to configure example is configured exactly the same as the minimalist example. We'll be using the example in I2C mode. Here is the code that differentiates the two examples, the setup:

void setup() {

  Wire.begin();//set up I2C bus, comment out if using SPI mode
  Wire.setClock(400000L);//clock stretching, comment out if using SPI mode

  Serial.begin(57600);//initialize serial monitor, maximum reliable baud for 3.3V/8Mhz ATmega328P is 57600

  if (myIMU.begin(
  ///// Interface mode options

  ///// Magnetometer output data rate options

  ///// Magnetic field full scale options
  ///// Magnetometer block data updating options

  ///// Magnetometer X/Y axes ouput data rate

  ///// Magnetometer Z axis ouput data rate

  ///// Magnetometer run mode

  ///// Acceleration full scale

  ///// Accelerometer block data updating

  ///// Enable X, Y, and/or Z axis

  ///// Accelerometer output data rate
    ) != IMU_SUCCESS)
    Serial.println("Failed setup.");
    while(1);  // Stop here 

Configuring for SPI Mode

This setup function exposes all of the configuration options necessary to read the magnetometer and accelerometer. See the datasheet for more advanced configuration. To change a configuration option, uncomment the desired option in that section, and remove or comment out all other options in that section. For example, if you wanted to use the SPI interface, you would change that section to look like the following.

  ///// Interface mode options

After uploading the sketch, open the serial monitor or your favorite terminal emulator and you should start seeing something similar to the following repeating over and over once per second.

 X = 64.5752
 Y = 31.4941
 Z = 943.1152

 X = -0.2085
 Y = 0.0425
 Z = 0.0972

 Degrees C = 25.3750
 Degrees F = 77.6750

In the setup that this data capture came from, the breakout board was sitting roughly flat on a desk. This orients the z-axis parallel to the earths gravitational field of 1,000 mg. This is seen by the z-axis value of around 943. If the LSM303C were in free fall, the z-axis component would be 0 g, because it wouldn't be accelerating. Since the sensor isn't in free fall, it is measuring an effective acceleration of 1 g in the positive z direction up out of the table.