-
Notifications
You must be signed in to change notification settings - Fork 7
Understanding the calibration of the LSM303 magnetometer (compass)
Calibration
The LSM303 magnetometer is very easy to calibrate if you understand the concept. See above diagram regarding the offset from origin (0, 0) that needs to be corrected. Use the readAxes function to gather data continuously as you spin the breakout board / chip on a flat surface. This will allow you to collect X, Y data as the board turns within the magnetic field. What you are looking for is the max and min values on each axis.
Using the above diagram as an example, you can see the center of the uncorrected calibration run is offset to the left. To find the uncorrected origin X value, calculate (Xmin + Xmax) / 2. In this case, it is approximately (-250 + 200) / 2 = -25. Visually, this makes sense. Y in this case is (-180 + 280) / 2 = 50. This makes the uncorrected origin (-25, 50). To add your calibration data, simple use mag.setOffset(X, Y, Z). In this case, it would be mag.setOffset(-25, 50, 0).
readHeading is not yet tilt compensated, so the Z value is not needed.