Calibrating 3D compass (LSM303DLH)

I’ve been experimenting with the 3D compass breakout board and am bothered by a number of aspects of the device. First, the data sheet is remarkably unclear as to what absolute accuracy one might expect, despite the claim that each individual device is “factory calibrated” with constants stored in internal flash memory.

It is well known to users that the magnetometer has substantially unequal response and offsets on the 3 axes, such that each device must be corrected by individual experimentation, and Pololu supplies a test program that allows one to determine the correction. According to the manufacturer one can achieve an overall accuracy of about 2 degrees in magnetic heading, but this requires calibration in the final assembled device.

However, I haven’t seen much discussion about the fact that the linear accelerometer has the same issues.

I modified the test program to print out the raw measurements over a USB link, for each of the six axes and did some tests. Here is what I get for 6 orientations of the accelerometer, stationary on a level surface (min and max values rounded to three significant digits):

Z: -15800 17400
Y: -16300 16100
X: -15300 16500

These should all correspond to +/- 1 g, but the maximum difference in the readings is about 12% and about 7% from the average.

Given that the accelerometer is used to determine “down” so that a tilt-corrected magnetic north heading can be obtained, it is hard to imagine that the manufacturer’s stated claim of 2 degree accuracy can be reached without extensive effort in calibrating all six axes. There is a very brief discussion of this (as applied to the accelerometer) in the LSM303DLH application notes, suggesting that a full 12-parameter solution is required, i.e. rotation matrix, scale and offset. That should in fact be applied to both sensors so that 24 constants need to be determined.

Any thoughts?

Edit: After posting, I found this link to an amazing paper and bit of software that works with raw data:

Running MagCal.exe on 373 raw magnetometer measurements with random orientations produced the following transformation (more later).

These calibration parameters have been created with the software MagCal
from the PLAN Group (
It is based on the article titled ‘Complete tri-axis magnetometer
calibration in the magnetic domain’.

The hard iron bias equals:

-0.3153 0.0906 -0.0038

The combined matrix equals:

1.0231 -0.0142 0.0087
-0.0142 1.0453 0.0030
0.0087 0.0030 0.9070

Hello, Jim.

Thanks for sharing that calibration software. It looks like it could be a useful tool for getting more accurate results from the LSM303DLH; we would be interested to hear how the data from it ends up working for you.

- Kevin