Problems with QMC5883L magnetometer readings

Hey everyone,

I've been using the forums for a while, but this is the first time I've ever actually posted something. I am trying to use a QMC5883L (this one specifically: Amazon.com) for a project where I need a semi-accurate (+/- 5 degrees) compass reading.

I bought these and they were actually working great, but then I encountered a weird problem. Out of nowhere, they started acting very weird, giving random readings even while stationary. The environment that they were in (on an RC car) has not changed and they were working both while stopped and in motion.

I am using this library: GitHub - dthain/QMC5883L: Driver for QMC5883L chip found in many GY-271 boards.

Like I said, the fact that it was working and then - without anything changing - stopped working is puzzling me. Has anyone experienced something similar?

Here is an example of the compass heading calculated from the magnetometer readings while at rest and facing generally North. I have also attached the sample code from the library.

compass.ino (439 Bytes)

The calibration procedure built into that library is nearly useless, especially if the magnetometer is mounted near to motors or magnetic materials. Exposure of the sensor for even a short time to a strong magnet can cause the inner workings to be magnetized and may permanently damage it.

For starters, I strongly recommend to calibrate the magnetometer by following this tutorial: Tutorial: How to calibrate a compass (and accelerometer) with Arduino | Underwater Arduino Data Loggers

An example of using that procedure for a particularly difficult case is posted here: https://forum.pololu.com/t/correcting-the-balboa-magnetometer/14315

1 Like

Ah. That makes sense why the readings suddenly became useless. Thank you for the help!