Arduino library for Pololu LSM303 boards
Release date: 2016-08-17
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.
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:
(including Arduino Uno, Leonardo, Mega; Pololu A-Star 32U4)
Arduino LSM303 board ------- ------------ 5V - VIN GND - GND SDA - SDA SCL - SCL
(including Arduino Due)
Arduino LSM303 board ------- ------------ 3V3 - VIN GND - GND SDA - SDA SCL - SCL
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:
If this does not work, you can manually install the library:
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.
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.
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.
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.
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_maxassigned in the
setup()function with your own values obtained from the Calibrate example.
These programs make use of the LSM303 library but are not included in the library archive or repository.
Wire.endTransmission()documentation for return values.
m_maxwith placeholder values.
bool init(deviceType device, sa0State sa0)
device_auto) and the state of the SA0 pin (
sa0_auto), which determines the least significant bit(s) of the I²C slave address (on some devices, and only for the accelerometer in some cases). Constants for these arguments are defined in LSM303.h. Both of these arguments are optional; if they are not specified, the library will try to automatically detect the device and accelerometer address. A boolean is returned indicating whether the type of LSM303 device was successfully determined (if necessary).
void writeReg(byte reg, byte value)
void readReg(int reg)
void writeAccReg(byte reg, byte value)
byte readAccReg(byte reg)
void writeMagReg(byte reg, byte value)
byte readMagReg(int reg)
a. Conversion of the readings to units of g depends on the accelerometer's selected gain (full scale setting). Note that in the LSM303DLHC, LSM303DLM, and LSM303DLH, the acceleration data registers actually contain a left-aligned 12-bit number, so the lowest 4 bits are always 0, and the values in
ashould be shifted right by 4 bits (divided by 16) to be consistent with the conversion factors specified in the datasheets.
m. Conversion of the readings to units of gauss depends on the magnetometer's selected gain (full scale setting).
void setTimeout(unsigned int timeout)
readMag(), in milliseconds, after which they will abort if no data is received. A value of 0 disables the timeout.
unsigned int getTimeout(void)
readMag()has timed out since the last call to
float heading(vector from)
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 TEMPOUTHM and TEMPOUTLM on the LSM303DLHC. To read those two registers, use
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.
init(); reverted argument types in register access functions to allow numeric register addresses; other miscellaneous fixes and optimizations.
a; this makes the values returned by the library more consistent between the LSM303D and older sensors.
enableDefault()behavior changed to be more consistent across devices.
heading()now returns a float instead of an int.
readReg(), which should be usable in place of
readMagReg()in most situations.
timeoutOccurred()now reports whether a timeout occurred since it was last called instead of only on the most recent
getDeviceType()function for programs that need to autodetect devices and distinguish between them.
init()function before using any of the other library functions, typically from within the Arduino
setup()function. While the older library only works with the Pololu boards' default accelerometer slave address of 0011000b, this library allows you to specify the slave address with the