Compass sensor Output


I am using a compass sensor (LSM303DLM 3D Compass and Accelerometer Carrier with Voltage Regulators to be precise) to ensure that the robot runs along a straight line. I am interfacing it with Arduino Mega. I looked at the code on github ( and some of the previous posts on this forum but I don’t quite understand the readings output by LSM303DLM.
The readings from the magnetometer are in gauss. So if the robot deviates from its straight line path, the interference with earth’s magnetic field will change and thus change the readings, in that case how will I know on which side it went off track and which motor must be slowed down to bring it on track? (it is a dual motor drive robot and the compass is stationary with respect to the robot) Could someone please explain the output from the compass?


The output of the magnetometer is in units that can be converted to Gauss, if you like.

More importantly there are three axes, X, Y and Z with magnetic field strengths being measured on each axis. From these three measurements you can compute the direction of the Earth’s magnetic field at all times, providing that the magnetic field of the motors does not interfere. This is explained in the documentation provided by Pololu and on many web sites.

Since it is almost certain that the motors will interfere with the compass measurements, you need to do some tests. Mount the robot so that it can’t move but the wheels can spin freely, and record magnetometer readings with the motors off, and while on at varying speeds. If the readings change much with the motors on, you will need to think about how to correct for this.