This guide has been refactored and updated for more sensors, calibration storage, and more algorithms, including quaternion output. Please visit to read the new guide!
This simple example uses uncalibrated data and doesn't integrate the on board gyroscope, but is fast and easy to use. For a more advanced example, see Sensor Fusion Algorithms later in this guide.
The AHRS example sketchs reads raw data from the board's accelerometer/magnetometer and converts the raw data into easy to understand Euler angles.

It does this with a bit of trigonometry (you remember high school math, right!?), but to save you from dusting off the textbooks or wading through endless application notes, we've wrapped up all of the calculation to convert raw accelerometer and magnetometer data to degrees in a convenient helper function:

    bool getOrientation(sensors_vec_t *orientation)
    The AHRS sketch creates an Adafruit_Simple_AHRS object which takes an accelerometer and magnetometer sensor as input to its constructor. You can actually pass any accelerometer or magnetometer object which supports the Adafruit unified sensor library in to the AHRS algorithm, and the examples use the 9-DOF, 10-DOF, and LSM9DS0 sensors.

    Once the simple AHRS object is created the getOrientation function is called to retrieve the current orientation. Internally the function will read raw data from the accelerometer and magnetomber, converts the data to Euler angles, and spits the three-dimensional orientation data out. If you're interested in the math, you can have a look at the source code on github, but for now we'll just treat it as a mathemagical black box:

    Euler Angles

    Euler angles describe orientation (in degrees) around a single reference point in three-dimensional space.

    Various names are employed for the three angles, but the most common terminology with aircraft is Roll (x), Pitch (y) and Yaw (z).

    The illustration below from the Wikipedia article on Euler angles should illustrate the concept clearly. You normally have both positive and negative angles (-180° to 180°) depending on the direction the airplane is tilted, with 0° in every direction corresponding to the airplane being perfectly aligned with each axis:
    If you run the AHRS sketch, you should see something similar to the following in the Serial Monitor, where the three values after 'orientation' are the Euler angles:
    In this case, we can see that the roll is about 18°, the pitch is about 78° and the heading or yaw is about 32°, and the sketch will keep updating itself with the latest values at whatever speed we've set in the example sketch.

    This guide was first published on Mar 19, 2014. It was last updated on Mar 19, 2014.

    This page (Using AHRS Data) was last updated on Feb 13, 2014.

    Text editor powered by tinymce.