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

Software

To use the Adafruit 9DOF breakout, install the following libraries.

Open up the Arduino library manager:

Search for the Adafruit Sensor library and install it

Search for the Adafruit LSM303 library and install it

Search for the Adafruit L3GD20 library and install it

Search for the Adafruit 9DOF library and install it

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
Download: file
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
Download: file
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
Download: file
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:
This guide was first published on Feb 07, 2014. It was last updated on Feb 07, 2014.
This page (Software) was last updated on Sep 18, 2020.