LSM303D Arduino Library with #2127 - North Issue


I recently purchased the LSM303D 3D Compass and Accelerometer Carrier with Voltage Regulator Item #2127 for a robotic project that I just started. First I want to say that its a great board and I am very happy with it. I am having a problem with the Arduino Library posted on the resource page that I can not seem to resolve no matter what I have tried. Specifically when the board is posted due north I get some wild readings from the library. It tends to oscillate around 0 deg, ranging from 345 to 145 at the worse. I tried several work arounds but no luck. Was hoping that you all could help with the problem. Just for reference I see the same issue when I run the example code. Any help would be appreciated.

Thanks in advance

Have you calibrated the accelerometer and magnetometer?
My favorite procedure is described here (for magnetometers, but it works for accelerometers as long as the device is held still while each measurement is taken): … ation.html

Hey Jim. Thanks for getting back to me. Yes I calibrated the magnetometer per the instructions for the LSM303 technique to find the min-max values, i.e., using the cal routine from the library.

I did track the problem with the problem with the large variation around North. The issue that I had was that I was getting a variation of about ± 1 degrees around where its suppose to be (checked against a compass) even when the LSM303 was stationary. In order to get a stable reading I was averaging to such a large extent that when it hit around 0/360 the averages were going crazy. So the question now goes to how to get a more stable and accurate reading. I did find an article on the using what is called Yamartino_method for wind speed averaging or possibly a median filter. Any suggestions?


In my experience, +/- 1 degree is the normal sensor noise. Averaging will certainly help, but how much improvement you can expect is hard to estimate. There are a couple of tilt sensors on the market that claim 0.01 degree accuracy, but those appear to average over thousands of readings.

I’ve been looking for a while now, and so far, there don’t seem to be any consumer-grade sensors that do better than about 1 degree in single-reading accuracy, and that is after very careful calibration in a disturbance-free environment.

Incidentally, if the 0/360 degree transition is giving you a headache, note that there is a procedure called the “circular mean” that solves the problem. See this page:

Hi Jim,

I apologize for not getting back to you sooner but I got sidetracked with fixing some issues with the FreeIMU code and adding butterworth filters and getting the MPU-9150 library working with the 6050 library. The info that you that posted is exactly what I needed - needed a baseline since I am working in kind of the dark on whats normal and or not. I quickly checked out the link that you sent me and should help quite a bit. Just for reference I extracted the heading code from the LSM303 library to make it work with any combo - right now only tested with MPU-9150. Variation in compass readings is larger than with the LSM303 - seems to be related to the amount of noise in the system - 9150 board I am using has more noise in the accel and mag signal than the freeimu board I am using. Just purchased the AltIMU-10 to add to my collection of IMUs and see how it works with the AHRS code.

Still trying to understand the reference vector used in the code on how that should be aligned with magnetometer axis to get the correct heading, right now off 90 deg. Will figure it out, i hope.

Thanks for all your help.