In this Instructable I want to show how to use the LSM303 sensor to realize a tilt compensated compass. After a first (unsuccessful) attempt I dealt with the calibration of the sensor. Thanks to these, the values of the magnetometer have improved significantly. The combination of calibrated values from the magnetometer and accelerometer then resulted in a tilt compensated compass.
What you need:
1 Arduino Uno
1 LSM303DHLC Breakout
1 Resistor 220 Ohm
1 Potentiometer 10k
1 2x16 LCD in 4-bit mode
1 Cardboard case
Step 1: Creating the Raw Data for Calibration
The calibration is done separately for magnetometer and accelerometer each time in the same way.
In a first step, the raw data of the sensor are read out in 12 defined positions (Picture 5.2). Then the correction data are calculated with the help of Magmaster 1.0 (Picture 5.3) and can be evaluated in a corresponding sketch. You can find a very good guide here
The Arduino sketch "LSM303DHLC_Acc_andMag_Raw_Measurements_201218.ino" provides the necessary raw data. For this you can select the source in line 17.
For working with Magmaster 1.0 please close the Serial Monitor window.
Step 2: Creating the Calibrated Measurements
To get the calibrated measurements of magnetometer and accelerometer transfer the values in the transformation matrix and bias in the Arduino sketch "LSM303DHLC_Tilt_compensated_Compas_211218", line 236 - 246 for Magnetometer, 268 - 278 for Accelerometer.
As a check, the sketch also provides a comparison of the raw data and the calibrated sensor values. In addition, you can check the readings with compass and protractor.
Step 3: Adding an LCDisplay
The LC display is used to display the current position relative to the earth's magnetic field. The X-axis of the sensor points to the north, where 0 ° corresponds to the magnetic north. The value increases by turning clockwise to 360 °. The inclination of the sensor is well compensated, but should not exceed 45 °.
The connection of the 16x2 LC display is standard and well explained in the following Arduino tutorial:
I hope I could inspire you to new Instructables and I look forward to your projects.