# Adafruit 9-DOF IMU Breakout

## Introduction

![](https://cdn-learn.adafruit.com/assets/assets/000/014/019/medium800/sensors_1714_LRG.jpg?1391799881)

Adafruit's 9DOF (9 Degrees of Freedom) breakout board allows you to capture nine distinct types of motion or orientation related data: 3 degrees each of acceleration, magnetic orientation, and angular velocity.  
  
If you also want barometric data, check out the [Adafruit 10DOF breakout](http://www.adafruit.com/products/1604 "Link: http://www.adafruit.com/products/1604"), which adds a BMP180 barometric pressure sensor to measure altitude and temperature.  
  
After testing a lot of combinations of sensors, we settled on the following devices that we think offer the best results and the least amount of hassle:

- **LSM303DLHC** - a **3-axis accelerometer** (up to +/-16g) and a **3-axis magnetometer** (up to +/-8.1 gauss) on a single die
- **L3GD20** - a **3-axis gyroscope** (up to +/-2000 dps)

![](https://cdn-learn.adafruit.com/assets/assets/000/014/020/medium800/sensors_1714header_LRG.jpg?1391799920)

# Related Links
The Adafruit 9DOF board and library reuses the existing Adafruit drivers for the **LSM303DLHC** (accelerometer and magnetometer) and the **L3GD20** (gyroscope).  
  
For information about these particular drivers, consult the following learning guides:  

- [LSM303DLHC Learning Guide](http://learn.adafruit.com/lsm303-accelerometer-slash-compass-breakout/overview "Link: http://learn.adafruit.com/lsm303-accelerometer-slash-compass-breakout/overview")
- [L3GD20 Learning Guide](http://learn.adafruit.com/adafruit-triple-axis-gyro-breakout "Link: http://learn.adafruit.com/adafruit-triple-axis-gyro-breakout")

This breakout is basically just a smooshed together version of both of these so anything you can do with those libraries/guides will follow here. # Adafruit 9-DOF IMU Breakout

## Connecting It Up

![](https://cdn-learn.adafruit.com/assets/assets/000/014/021/medium800/sensors_1714frontpins_LRG.jpg?1391799944)

All of the sensors on the Adafruit 9DOF breakout communicate via a two-pin I2C bus, making it relatively easy to setup with a minimum number of cables:Info: 

Start by breaking the header down so it is 10 pins long. Sometimes we toss in a longer strip, but its easy to break it down, just use pliers or diagonal cutters to snap it down to 10 pins.  
  
Place the **long ends** of the header into a solderless breadboard to keep them steady.

![sensors_header.jpg](https://cdn-learn.adafruit.com/assets/assets/000/014/023/medium640/sensors_header.jpg?1391800348)

Place the 9DOF board right on top of the header so that the **short** pins are sticking thru the hole pads

![sensors_place.jpg](https://cdn-learn.adafruit.com/assets/assets/000/014/024/medium640/sensors_place.jpg?1391800373)

Heat up your soldering iron and once it is ready, solder all 10 pads to the header, making sure to check that there is plenty of solder to make a mechanically strong connection and there's no solder bridging either.

![sensors_solder.jpg](https://cdn-learn.adafruit.com/assets/assets/000/014/025/medium640/sensors_solder.jpg?1391800399)

![sensors_midsolder.jpg](https://cdn-learn.adafruit.com/assets/assets/000/014/026/medium640/sensors_midsolder.jpg?1391800446)

![sensors_lastsolder.jpg](https://cdn-learn.adafruit.com/assets/assets/000/014/027/medium640/sensors_lastsolder.jpg?1391800454)

That's it! you're done, continue on to the wiring step below

![sensors_done.jpg](https://cdn-learn.adafruit.com/assets/assets/000/014/028/medium640/sensors_done.jpg?1391800480)

# Basic Setup (5V Logic, Arduino Uno, etc.)
We'll be using an Arduino UNO here, but the code will work on a Mega or Leonardo just fine. Most other Arduino compatibles should have no problems either but we only support official Arduinos for code.  

- Connect the **SCL** pin on the breakout to the **SCL** pin on your Arduino. On an UNO & '328 based Arduino, this is also known as **A5** , on a Mega it is also known as **digital 21** and on a Leonardo/Micro, **digital 3**  
- Connect the **SDA** pin on the breakout to the **SDA** pin on your Arduino. On an UNO & '328 based Arduino, this is also known as **A4** , on a Mega it is also known as **digital 20** and on a Leonardo/Micro, **digital 2**
- Connect the **VIN** pin on the breakout to **3.3V or 5V** on your Uno (5V is preferred but if you have a 3V logic Arduino 3V is best)  
- Connect the **GND** pin on the breakout to the **GND** pin on your Uno

That's it! With those four wires, you should be able to talk to any of the I2C chips on the board and run any of the example sketches.![](https://cdn-learn.adafruit.com/assets/assets/000/014/022/medium800/sensors_wiring.jpg?1391800057)

# Advanced Setup
While most people probably won't need to use the pins below, we've also broken out a few extra pins for advanced users or for special use cases. If you need to use any of these pins, simply hook them up to a GPIO pin of your choice on the Uno:  

- **GINT** - The interrupt pin on the L3GD20 gyroscope  
- **GRDY** - The 'ready' pin on the L3GD20 gyroscope  
- **LIN1** - Interrupt pin 1 on the LSM303DLHC  
- **LIN2** - Interrupt pin 2 on the LSM303DLHC  
- **LRDY** - The ready pin on the LSM303DLHC

These pins are all outputs from the 9-DOF breakout and are all 3.3V logic, you can use them with 5V or 3V as 3.3V registers 'high' on 5V systems.  
# 3V3 Setup
If you are using an MCU or board with 3V3 logic (instead of the 5V logic used by the Arduino Uno), you can still power the 9-DOF with the VIN pin _or_ you can use the 3Vo pin, which will bypass the on-board 3V3 regulator and level shifting:  

- Connect **Vin or 3Vo** on the breakout to the **3.3V** supply on your target MCU
- Connect **GND** on the breakout to **GND** on the target MCU

Info: 

# Adafruit 9-DOF IMU Breakout

## 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:  

- [Adafruit Unified Sensor Library on Github](https://github.com/adafruit/Adafruit_Sensor)
- [LSM303DLHC Library on Github](https://github.com/adafruit/Adafruit_LSM303DLHC)
- [L3GD20 Library on Github](https://github.com/adafruit/Adafruit_L3GD20_U)
- [Adafruit 9DOF Library on Github](https://github.com/adafruit/Adafruit_9DOF)  

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](http://learn.adafruit.com/using-the-adafruit-unified-sensor-driver)# Software

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

Open up the Arduino library manager:

![](https://cdn-learn.adafruit.com/assets/assets/000/083/992/medium800/sensors_library_manager_menu.png?1573497710)

Search for the&nbsp; **Adafruit Sensor** &nbsp;library and install it

![](https://cdn-learn.adafruit.com/assets/assets/000/083/993/medium800/sensors_adafruitsensor.png?1573497764)

Search for the&nbsp; **Adafruit LSM303** &nbsp;library and install it

![](https://cdn-learn.adafruit.com/assets/assets/000/083/995/medium800/sensors_lsm303.png?1573497919)

Search for the&nbsp; **Adafruit L3GD20** &nbsp;library and install it

![](https://cdn-learn.adafruit.com/assets/assets/000/083/996/medium800/sensors_l3gd20.png?1573498028)

Search for the&nbsp; **Adafruit 9DOF&nbsp;** library and install it

![](https://cdn-learn.adafruit.com/assets/assets/000/084/406/medium800/sensors_9dof.png?1573962751)

[If you're new to installing libraries, check out our super-awesome detailed tutorial on how to install Arduino libraries](http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use "Link: http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use")

# Adafruit\_9DOF Helper Functions

The **Adafruit\_9DOF.cpp** file (from [Adafruit\_9DOF](https://github.com/adafruit/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**

Danger: 

```auto
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** Danger: 

```auto
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** ```auto
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:  
![](https://cdn-learn.adafruit.com/assets/assets/000/014/031/medium800/sensors_Screen_Shot_2014-02-07_at_20.52.47.png?1391802924)

# Adafruit 9-DOF IMU Breakout

## Design Files

# Datasheets & Files

- [LSM303 Datasheet](http://www.adafruit.com/datasheets/LSM303DLHC.PDF "Link: http://www.adafruit.com/datasheets/LSM303DLHC.PDF")
- [L3GD20 Datasheet](http://www.adafruit.com/datasheets/L3GD20.pdf "Link: http://www.adafruit.com/datasheets/L3GD20.pdf")
- [EagleCAD PCB files on GitHub](https://github.com/adafruit/Adafruit-9-DOF-and-10-DOF-PCBs)
- [Fritzing object in Adafruit Fritzing library](https://github.com/adafruit/Fritzing-Library/)

# Dimensions (Inches)
![](https://cdn-learn.adafruit.com/assets/assets/000/014/029/medium800/sensors_dims.gif?1448049966)

# Schematic
![](https://cdn-learn.adafruit.com/assets/assets/000/014/030/medium800/sensors_schem.png?1391801367)

# Adafruit 9-DOF IMU Breakout

## F.A.Q.


## Related Guides

- [Adafruit STEMMA Speaker](https://learn.adafruit.com/adafruit-stemma-speaker.md)
- [Scroll an SMS Text Message on your RGB Matrix](https://learn.adafruit.com/scroll-an-sms-text-message-on-your-rgb-matrix.md)
- [LSM303 Accelerometer + Compass Breakout](https://learn.adafruit.com/lsm303-accelerometer-slash-compass-breakout.md)
- [AHRS for Adafruit's 9-DOF, 10-DOF, LSM9DS0 Breakouts](https://learn.adafruit.com/ahrs-for-adafruits-9-dof-10-dof-breakout.md)
- [Lighting LED Nets with WLED and xLights](https://learn.adafruit.com/lighting-led-nets-with-wled-and-xlights.md)
- [Adafruit Triple Axis Gyro Breakout](https://learn.adafruit.com/adafruit-triple-axis-gyro-breakout.md)
- [Halloween Scream Box](https://learn.adafruit.com/halloween-scream-box.md)
- [Adafruit MPR121 12-Key Capacitive Touch Sensor Breakout Tutorial](https://learn.adafruit.com/adafruit-mpr121-12-key-capacitive-touch-sensor-breakout-tutorial.md)
- [Storage humidity and temperature monitor](https://learn.adafruit.com/storage-humidity-and-temperature-monitor.md)
- [Getting Started with FLORA](https://learn.adafruit.com/getting-started-with-flora.md)
- [Adafruit LIS3DH Triple-Axis Accelerometer Breakout](https://learn.adafruit.com/adafruit-lis3dh-triple-axis-accelerometer-breakout.md)
- [Introducing Pro Trinket](https://learn.adafruit.com/introducing-pro-trinket.md)
- [Multi-Cell LiPo Charging](https://learn.adafruit.com/multi-cell-lipo-charging.md)
- [AR1100 Resistive Touch Screen Controller Guide](https://learn.adafruit.com/ar1100-resistive-touch-screen-controller-guide.md)
- [Adafruit Proto Tripler PiCowbell](https://learn.adafruit.com/adafruit-proto-tripler-picowbell.md)
