pololu / lsm303-arduino

Arduino library for Pololu LSM303 boards
http://www.pololu.com/catalog/product/2124
MIT License
140 stars 137 forks source link

Arduino library for Pololu LSM303 boards

Version: 3.0.1
Release date: 2016-08-17
www.pololu.com

Summary

This is a library for an Arduino-compatible controller that interfaces with LSM303D, LSM303DLHC, LSM303DLM, and LSM303DLH 3D compass and accelerometer ICs on Pololu boards. It makes it simple to read the raw accelerometer and magnetometer data from these boards:

The library also includes a function for computing the tilt-compensated heading for those looking to use the LSM303 as a tilt-compensated compass.

Getting started

Hardware

A LSM303 carrier can be purchased from Pololu's website. Before continuing, careful reading of the product page as well as the chip datasheet is recommended.

Make the following connections with wires between the Arduino and the LSM303 board:

5V Arduino boards

(including Arduino Uno, Leonardo, Mega; Pololu A-Star 32U4)

Arduino   LSM303 board
-------   ------------
     5V - VIN
    GND - GND
    SDA - SDA
    SCL - SCL

3.3V Arduino boards

(including Arduino Due)

Arduino   LSM303 board
-------   ------------
    3V3 - VIN
    GND - GND
    SDA - SDA
    SCL - SCL

Software

If you are using version 1.6.2 or later of the Arduino software (IDE), you can use the Library Manager to install this library:

  1. In the Arduino IDE, open the "Sketch" menu, select "Include Library", then "Manage Libraries...".
  2. Search for "LSM303".
  3. Click the LSM303 entry in the list.
  4. Click "Install".

If this does not work, you can manually install the library:

  1. Download the latest release archive from GitHub and decompress it.
  2. Rename the folder "lps-arduino-xxxx" to "LSM303".
  3. Drag the "LSM303" folder into the "libraries" directory inside your Arduino sketchbook directory. You can view your sketchbook location by opening the "File" menu and selecting "Preferences" in the Arduino IDE. If there is not already a "libraries" folder in that location, you should make the folder yourself.
  4. After installing the library, restart the Arduino IDE.

Examples

Several example sketches are available that show how to use the library. You can access them from the Arduino IDE by opening the "File" menu, selecting "Examples", and then selecting "LSM303". If you cannot find these examples, the library was probably installed incorrectly and you should retry the installation instructions above.

Serial

This program continuously reads the accelerometer and magnetometer, communicating the readings over the serial interface. You can display the readings with the Arduino Serial Monitor.

Example output:

A:    192  -1040 -17168    M:   -512     27    144
A:    288  -1040 -17232    M:   -511     26    143
A:     16  -1104 -17216    M:   -511     27    144

See the comments in this sketch for some notes on how to convert the raw sensor values to units of g and gauss.

Calibrate

This program is similar to the Serial example, but instead of printing the most recent readings, it prints a running minimum and maximum of the readings from each magnetometer axis. These values can be used to calibrate the heading() functions and the Heading example after moving the LSM303 through every possible orientation.

Heading

This program uses readings from the accelerometer and magnetometer to calculate a tilt-compensated compass heading (in degrees relative to a default vector), which is communicated serially and can be displayed with the Arduino Serial Monitor. The default vector is chosen to point along the surface of the PCB, in the direction of the top of the text on the silkscreen. (This is the +X axis on the Pololu LSM303D carrier and the -Y axis on the Pololu LSM303DLHC, LSM303DLM, and LSM303DLH carriers.) See the comments if you want to use a different reference vector.

For the most accurate results, you should replace the values of m_min and m_max assigned in the setup() function with your own values obtained from the Calibrate example.

Other library applications

These programs make use of the LSM303 library but are not included in the library archive or repository.

Library reference

1 The automatic detection might fail if you do not use the Pololu boards' default accelerometer address, so you should specify your particular device if you change the state of the SA0 pin.

2 This function will not work for reading TEMP_OUT_H_M and TEMP_OUT_L_M on the LSM303DLHC. To read those two registers, use readMagReg() instead.

3 If the magnetometer data registers are read using register address constants without a specific device prefix (e.g. OUT_Y_H_M), these functions will automatically use the correct register addresses depending on the device type.

Version history