Software

LSM303DLHC and L3GD20 Drivers

You will need to have all of the following libraries available in your /libraries folder for Arduino to make use of the Adafruit 9DOF breakout:
For information on the Adafruit Sensor Library and the Unified Sensor Drivers used for all of these sensors, feel free to have a look at our related learning guide: Using the Adafruit Unified Sensor Driver

Downloading Libraries from Github

If you aren't familiar with Github, the easiest way to install the libraries is to click below for each zip file

Click these buttons to download a .zip file containing everything in the repository, and then place the files in the Arduino Sketch Folder '/libraries' sub-folder. You should end up with a structure like this:

  • arduinosketches/libraries/Adafruit_9DOF
  • arduinosketches/libraries/Adafruit_L3GD20_U
  • arduinosketches/libraries/Adafruit_LSM303DLHC
  • arduinosketches/libraries/Adafruit_Sensor

If you're new to installing libraries, check out our super-awesome detailed tutorial on how to install Arduino libraries

Adafruit_9DOF Helper Functions

The Adafruit_9DOF.cpp file (from Adafruit_9DOF) includes the following helper functions that are useful when working with typicaly 9DOF projects

bool accelGetOrientation ( sensors_event_t *event, sensors_vec_t *orientation )

This function reads the LSM303DLHC accelerometer data (supplied via the 'event' variable), and converts it into equivalent pitch (x) and roll (y) values, populating the supplied 'orientation' variables .pitch and .roll fields accordingly.

Arguments
  • event: The sensors_event_t variable containing the data from the accelerometer
  • orientation: The sensors_vec_t object that will have its .pitch and .roll fields populated
Returns
  • true if the operation was successful,
  • false if there was an error
Example
See the 'pitchrollheading' example in the Adafruit_9DOF library for an example of how to use this helper function
sensors_event_t accel_event;
sensors_vec_t   orientation;

/* Calculate pitch and roll from the raw accelerometer data */
accel.getEvent(&accel_event);
if (dof.accelGetOrientation(&accel_event, &orientation))
{
  /* 'orientation' should have valid .roll and .pitch fields */
  Serial.print(F("Roll: "));
  Serial.print(orientation.roll);
  Serial.print(F("; "));
  Serial.print(F("Pitch: "));
  Serial.print(orientation.pitch);
  Serial.print(F("; "));
}

bool magGetOrientation ( sensors_axis_t axis, sensors_event_t *event, sensors_vec_t *mag_orientation )

This function populates the .heading field in mag_orientation with the correct angular data (0-359°). Heading increases when rotating clockwise around the specified axis.

Arguments
  • axis: The given axis (SENSOR_AXIS_X, SENSOR_AXIS_Y, or SENSOR_AXIS_Z)
  • event: The raw magnetometer sensor data to use when calculating out heading
  • orientation: The sensors_vec_t object where we will assign an 'orientation.heading' value
Returns
  • true if the operation was successful,
  • false if there was an error
Example
See the 'pitchrollheading' example in the Adafruit_9DOF library for an example of how to use this helper function
sensors_event_t mag_event;
sensors_vec_t   orientation;
  
/* Calculate the heading using the magnetometer */
mag.getEvent(&mag_event);
if (dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation))
{
  /* 'orientation' should have valid .heading data now */
  Serial.print(F("Heading: "));
  Serial.print(orientation.heading);
  Serial.print(F("; "));
}

bool magTiltCompensation ( sensors_axis_t axis, sensors_event_t *mag_event, sensors_event_t *accel_event )

This function uses the accelerometer data provided in accel_event to compensate the magnetic sensor measurements in mag_event to compensate for situations where the sensor is tilted (the pitch and roll angles are not equal to 0°).

Arguments
  • axis: The given axis (SENSOR_AXIS_X, SENSOR_AXIS_Y, or SENSOR_AXIS_Z) that is parallel to the gravity of the Earth
  • mag_event: The raw magnetometer data to adjust for tilt
  • accel_event: The accelerometer event data to use to determine the tilt when compensating the mag_event values
Returns
  • true if the operation was successful,
  • false if there was an error
Example
sensors_event_t accel_event;
sensors_event_t mag_event;

...

// Get a sensor event from the accelerometer and magnetometer
accel.getEvent(&accel_event);
mag.getEvent(&mag_event);

if (dof.magTiltCompensation(SENSOR_AXIS_Z, &mag_event, &accel_event))
{
  // Do something with the compensated data in mag_event!
}
else
{
  // Oops ... something went wrong (probably bad data)
}

Example Sketch

If you run the pitchrollheading sketch in the examples folder, you can see a practical example using these helper functions above, which should result in output similar to the image below:
Last updated on 2016-10-13 at 02.18.10 PM Published on 2014-02-07 at 02.32.39 PM