LSM303D - A strange problem

Hello everyone, I am a total beginner when it comes to electronics and arduino but I do not lack the will and enthusiasm to learn something;)
I have an LSM303D module, I used the arduino library which you recommend.
I calibrated the sensor first according to your requirements.
I set the compass more or less to the north, fired up the serial monitor and I have these results. All right.

359.80
0.05
0.14
359.80
0.57
0.61
0.61
0.14
0.03
359.97
359.94
359.73
359.85
359.67
0.15
0.11
0.25
0.32
0.53
0.56
359.90
359.63
0.32
359.92
359.45
0.37
0.14
359.98
0.64
0.50
0.27
359.57
359.84
359.73
0.00
0.10
0.46
0.14
0.01
0.46
0.41
360.00
0.24
0.27
359.86
0.31
0.49
0.02
0.22
0.16
0.09
0.03
0.01

I turn the power off and on, I do not move the compass, I start the series and monitor and I have the following results:

92.65
92.65
93.15
93.15
92.72
92.84
92.84
92.87
92.86
92.86
92.99
93.12
93.12
92.68
92.68
92.64
93.03
93.03
93.18
93.25
93.25

I will turn on / off the power several times. Some time they are correct again and indicate north and ~ 0 degrees well. Do you know what could be wrong or what I’m doing wrong?

Here is my code

#include <Wire.h>
#include <LSM303.h>

LSM303 compass;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  compass.init();
  compass.enableDefault();
  
  /*
  Calibration values; the default values of +/-32767 for each axis
  lead to an assumed magnetometer bias of 0. Use the Calibrate example
  program to determine appropriate values for your particular unit.
  */
  compass.m_min = (LSM303::vector<int16_t>){-2813, -2107, -2738};
  compass.m_max = (LSM303::vector<int16_t>){+2724, +2998, +3155};
}

void loop() {
  compass.read();
  
  
float heading = compass.heading((LSM303::vector<int>){0, -1, 0});
  
  
  Serial.println(heading);
  delay(100);
}

Thank you in advance for your help

Hello.

Have you tried looking at the raw readings from the sensor to see if there is some pattern that might suggest what the problem is? You could do that using the Serial.ino example from our LSM303 Arduino library, but it might be worth modifying the Heading example that you are currently using instead so that you can see both the raw readings and the calculated heading at the same time. Could you try doing one of those and posting some sample output here? Some pictures of your physical setup might also be useful.

- Patrick

1 Like

I’m sorry that it is only now writing back … but the connecting cables turned out to be faulty, I replaced them with real copper cables and it worked right away;) I have 0-360 every time…

I still have a question, is there a similar module on offer now that has such a simple configuration and calibration ?? :wink:

We do not have a newer board that has a compass and accelerometer in a single IC, but we do have some IMU boards that combine multiple sensor chips. You might take a look at our MinIMU-9 v5. Just like the LSM303D, we have Arduino libraries available to make interfacing with the sensors easier. There are links to those available on the product page under the “Resources” tab

Here is a link to all of our available accelerometer, gyro, & compass sensors.

- Patrick

1 Like