LSM303 tilt compensation


I use the LSM303 libraries and it works pretty well but only … horizontally. The module is set on the Y axe.

I calibrated the module with “Polulu LSM303 Calibrate” and the compass is accurate to + - 1 degree when it is well in the horizontal. On the other hand, when I tilt the module, the compensation no longer works.

Let A be the height, H the heading. An example of my data:

Heading at 241 °
If A = 0 ° then H = 241 °
If A = 45 ° then H = 212 °
If A = 90 ° then H = 281 °

Heading at 287 °
If A = 0 ° then H = 287 °
If A = 45 ° then H = 240 °
If A = 90 ° then H = 232 °

The code

  #include <LSM303.h>
  LSM303          Compass;
  Compass.m_min = (LSM303::vector<int16_t>){  -530,   -713,   -626};
  Compass.m_max = (LSM303::vector<int16_t>){  +498,   +319,   +244};
  float heading = Compass.heading((LSM303::vector<int>){0, 1, 0});

So, do you have an idea or a solution ?


The heading() function is more sensitive at high angles of tilt (i.e. small changes in angle result in larger changes in heading), but we generally expect to see less than about a few degrees of difference in heading when changing the tilt angle. Can you tell me more about your setup? What Arduino are you using? How are you supplying power? How fast are you changing your tilt angle? Can you share a link to a video that shows how you are rotating your LSM303?

By the way, I do not suspect that this is the source of your issue, but the magnitude of your calibration values seem kind of low. If you are not already, you should ensure that you are pointing your LSM303 in every direction as the Calibrate program is running.


Thanks Jon,

After verification; i don’t understand what’s happening yesterday; but now it’s works ! Maybe a big magnet near my house :slight_smile: ?

I made several calibrations and I realized an error curve according to the inclination of X.
We can indeed see that more X is high; the greater the margin of error. However, just add this curve to offset the tilt and have a more real value.

Calibrations :

min: {  -529,   -696,   -605}    max: {  +509,   +313,   +254}
min: {  -540,   -715,   -597}    max: {  +502,   +340,   +251}
min: {  -465,   -714,   -604}    max: {  +512,   +319,   +252}
min: {  -530,   -714,   -604}    max: {  +512,   +319,   +252}

Do you think i must use the biggest value or only a single line ?

Error report :

I am glad things are working now. If you notice your heading readings not working out too well again in the future, you might check your work environment for things that can cause hard- and/or soft-iron distortions.

Your calibration values should use the minimal and maximal readings of each magnetometer axis; this is what our Calibrate sketch accomplishes.

The error that you are showing is about what we expect.


Thank you Jon !

I’m going to buy the MinIMU-9 V5 because you make good components and you’re very helpful.

1 Like