Tilt Compensated Compass With LSM303DHLC

Introduction: Tilt Compensated Compass With LSM303DHLC

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 Breadboard

1 Resistor 220 Ohm

1 Potentiometer 10k

1 2x16 LCD in 4-bit mode

1 Cardboard case

1 Compass

1 Protractor

Some wires

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

https://www.instructables.com/id/Easy-hard-and-soft-iron-magnetometer-calibration/

Thanks YuriMat!

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:

https://www.arduino.cc/en/Tutorial/HelloWorld

I hope I could inspire you to new Instructables and I look forward to your projects.


Be the First to Share

    Recommendations

    • Jewelry Challenge

      Jewelry Challenge
    • One Board Contest

      One Board Contest
    • Raspberry Pi Contest

      Raspberry Pi Contest

    Comments

    0
    vjosesan
    vjosesan

    Question 8 months ago on Step 2

    Hi.

    Congratulations for your work. I've been searching for this kind of code to make a little satellite rotor and I'd like to use a LSM303 to detect azimuth and elevation values.
    I've watched the "LSM303DHLC_Tilt_Compensated_Compass_LCD_211218.ino" code and I've seen a possible mistake in lines 97 or 102 about normalizing values.

    Line 102 is
    norm_a = sqrt(sq(Ax) + sq(Ay) + sq(Az)); //original code did not appear to normalize, and this seems to help

    should be?
    norm_a = sqrt(sq(Axc) + sq(Ayc) + sq(Azc)); //original code did not appear to normalize, and this seems to help

    Or maybe the line 97 should be without compensated values?

    Thanks for your help. Regards.