LSM303D 3D - Heading compensation and Arduino boards

Hello everyone, I bought a 3D LSM303D with the idea of being able to locate the Earth’s north regardless of the inclination of the sensor. I wanted to confirm if this is possible with this sensor, taking into account the inclination compensation, since with other devices like the MPU9250 I have not succeeded.

My objective is to know pitch, roll and yall with respect to the north, compensating yall when pitch and roll are not zero and the sensor is not horizontal with respect to the ground.

I am programming on Arduino NANO 16MHz. Is this possible easily with this sensor? What libraries do you recommend?

I have seen the library and I want to understand that it is possible, but to be sure, and to know the experience of other forum members with this sensor.

Also, is this library compatible with other Arduinos over 16MHz?

Thanks in advance.

Yes, it is possible to make a tilt-compensated compass using the LSM303D. Pololu has published code to do that for similar sensors.

For best results, it is absolutely essential to calibrate the magnetometer properly. How to do that, with examples, is described in the forum post linked below, which also has code for tilt compensation.

Just about any microprocessor can be used to read out the LSM303D data.

Hello, Juan.

You can use our LSM303 Arduino library’s Heading program, which calculates a tilt-compensated heading and outputs the result to the Arduino Serial Monitor. Note, like Jim mentioned, you will need to calibrate the magnetometer first in order to get useful results. There are many ways to calibrate a magnetometer (and to varying degrees of accuracy), though a quick way to get some basic calibration is to use our Calibrate example sketch.

We have not specifically tested our Arduino library with the Arduino Nano, but we do not expect any issues.


I am still waiting for the LSM303D 3D to come home for testing.

Using the calibrate.ino program from the official library seems easy. I have been reading the content you mentioned to me but I think it is for a balboa robot. And although I understand the idea, I am not very clear about the steps, how to run the program on the computer to take the data? What data exactly to modify in the library or script so that the LSM303D 3D is well calibrated? It is true that I have yet to see that part in more depth, perhaps later it will not be so complicated, but it would be incredible to have a noobs-proof tutorial like me on some issues, to reach a decent calibration.

Thanks Jim_Remington and JonathanKosh, keep talking

The calibrate.ino program may work well enough for you, so start there.

To get the very highest possible accuracy from your sensor requires more sophisticated approaches, described in this tutorial: