Erratic heading readings from AdaFruit_LSM9DS0

I am trying to determine heading (either relative or absolute) using a Flora LMSDS0 9-DOF Accelerometer, a Particle Photon and the the Adafruit_LSM9DS0 and Adafruit_AHRS_Simple libraries.

The Pitch and Roll readings make sense. They give me +/- 90 degrees nicely as I tilt the sensor but the heading readings make no sense.

I’m getting inconsistent ranges for my readings, sometime -90 to -120 for 360 degree rotation, sometimes +20 to -160. It’s all over the map. If I rotate the sensor and then come back to the same orientation I will get +/- 20 degrees on my readings. And many times the whole thing just stops reading and gives me a random static value.

Is this a problem with the math in the library, the sensor, the I2C on the photon. The current Random results are not very helpful.

Is there a good way to calibrate the heading?


To help eliminate I2C comms being the problem, use a scope to verify your signal quality. Possibly adjust your wire connect/pull up resistors to get a nice rising edge on clock and data lines. Although you do have pitch/roll readings ok, so it would be a bit surprising if I2C comms is the culprit here. Perhaps this might help:

I had similar issues, you need to run the Mag calibration routine, and make sure there is no big lumps of metal around. That should fix it.

Basically it needs to know the maximum angles for magnetic flux in your area. I had to increase the number of samples it takes so I had time to actually move the sensor around otherwise it didn’t work properly.

How does one run a mag calibration routine?

I just had a look at the Adafruit lib and there is no calibration routine, the sparkfun LSM9DS1 that i had the exact same issue with does. i’m not sure of the difference between the 2 sensors is, you could try and implement something similar in the adafruit library, the sparkfun one is pretty well documented and should help you get started. im not sure if its possible to use the sparkfun lib with the DS0 with just a few address changes, but its worth a go too

with the sparkfun library all i do is call imu.calibrateMag(); and then physically move the sensor in all different directions, after that i get a perfect 0-359 output that is totally repeatable.

I continue to have no end of frustration with this sensor.

I tried writing my own calibration routine but the problem seems to be something more than that.

At this point, if all I do is serial.print the raw data from the sensor the device connected to either a flora or a particle it will start with one result and freeze there, sometimes run for 30 to 60 seconds reporting data and then freeze, sometimes it will run for a while with other sensors after one freezes (I have tried polling the sensor as slow as every 500ms) . In all cases the system will in under a minute freeze and just keep reporting the same result or none at all.

Is this just a bad sensor or is there something else I can try?

What firmware are you building with? there was i2c issues a while ago.

Also how long are your cables, and are they soldered or just twisted? also what are the pullup values on the breakout board? these things could make the comms fail and lock up

1 Like

Hi, could you resolve the issue?

Hi, I used imu.calibrate and imu.calibrateMag() in setup().

turn on the Core, keep rotating while setup() is end.

After that I cant get perfect heading data. I tested Core with 0, 90, 180, 270, 350 degrees.

You can try this.