Version: 1.0.0
Release Date: 2011-01-20
http://www.pololu.com/
This is a library for the Arduino that interfaces with the Pololu LSM303DLH 3D compass and accelerometer carrier. It makes it simple to read the raw accelerometer and magnetometer data, and it has a function for computing the tilt-compensated heading for those looking to use this sensor as a tilt-compensated compass.
Download the archive from GitHub, decompress it, and drag the “LSM303DLH” folder to your arduino-00xx/libraries directory. Then restart the Arduino environment, so the libraries can be updated to include the LSM303DLH library and its examples.
The board for the LSM303DLH can be purchased on Pololu’s website.
After receiving the board, make the following connections with wires:
Arduino Uno/Duemilanove LSM303DLH Carrier 5V -> VIN GND -> GND Analog Pin 5 -> SCL Analog Pin 4 -> SDA
Open the example code sketch by going to File→Examples→LSM303DLH→example_name
Program the Arduino, and the accelerometer and magnetometer readings will be output to the serial monitor. Here is an example of what you will see:
A X: -151 Y: 848 Z: -509 M X: 281 Y: -442 Z: 87 A X: -147 Y: 847 Z: -509 M X: 266 Y: -446 Z: 78 A X: -147 Y: 849 Z: -508 M X: 280 Y: -450 Z: 81
For calibration purposes it is useful to record the maximum and minimum magnetometer values you get when moving the compass through every possible orientation.
Program the Arduino, and the tilt-compensated degrees of the heading from the -Y axis will be output to the serial monitor.
vector a - the last reading of the accelerometer values. vector m - the last reading of the magnetometer values. vector m_max - the maximum magnetometer readings found, use for calibration. vector m_min - the minimum magnetometer readings found, use for calibration. LSM303DLH() - Constructor void enable(void) - Turns on the magnetometer and accelerometer void read(void) - Takes a reading and stores the values in the vectors a and m. int heading(void) - Returns the tilt-compensated degrees headed away from the -Y axis int heading(vector from) - Returns the tilt-compensated degrees headed away from from int roll(void) - accelerometer X axis reading int pitch(void) - accelerometer Y axis reading int heave(void) - accelerometer Z axis reading void calibrate(void) - calibrates the compass and accelerometer