# LSM303D making a Compass to give zero to north

**URL:** https://forum.pololu.com/t/lsm303d-making-a-compass-to-give-zero-to-north/16972
**Category:** Sensors
**Created:** [March 19, 2019, 3:45pm UTC](https://forum.pololu.com/t/lsm303d-making-a-compass-to-give-zero-to-north/16972 "2019-03-19T15:45:08Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![NoClue](https://forum.pololu.com/letter_avatar_proxy/v4/letter/n/7feea3/32.png) [@NoClue](https://forum.pololu.com/u/NoClue)
#### Post date: [March 19, 2019, 3:45pm UTC](https://forum.pololu.com/t/lsm303d-making-a-compass-to-give-zero-to-north/16972/1 "2019-03-19T15:45:08Z")

</div>

Hi,

I am trying to make my LSM303D a Compass. I’m not using specifically made PCB that my company ordered. I have found some tips online, but it uses two axis and it changes when I pitched the PCB. this is the code is use now.

```auto
    ACC_X_LSB = 0x28; // x 
    ACC_X_MSB = 0x29;
    ACC_Y_LSB = 0x2A; // y
    ACC_Y_MSB = 0x2B;
    ACC_Z_LSB = 0x2C; // z
    ACC_Z_MSB = 0x2D;    

    MAG_X_LSB = 0x08; // x
    MAG_X_MSB = 0x09;
    MAG_Y_LSB = 0x0A; // y
    MAG_Y_MSB = 0x0B;
    MAG_Z_LSB = 0x0C; // z
    MAG_Z_MSB = 0x0D;
    
}
int GetValue(int MSB, int LSB)
{
    int combined = 256 * MSB + LSB;
    return combined;
}

void lsm303d_update_values() {

   
  
    
    x_mag = GetValue(lsm303d_read_register(MAG_X_MSB), lsm303d_read_register(MAG_X_LSB));
    y_mag = GetValue(lsm303d_read_register(MAG_Y_MSB), lsm303d_read_register(MAG_Y_LSB));
    z_mag = GetValue(lsm303d_read_register(MAG_Z_MSB), lsm303d_read_register(MAG_Z_LSB));
       
   
    
    pose_float_2axis = (atan2((float)y_mag,(float)x_mag)*180)/3.14159;

```

---

<div class="post-metadata">

### Author: ![JonathanKosh](https://forum.pololu.com/user_avatar/forum.pololu.com/jonathankosh/32/6692_2.png) [@JonathanKosh](https://forum.pololu.com/u/JonathanKosh)
#### Post date: [March 19, 2019, 10:06pm UTC](https://forum.pololu.com/t/lsm303d-making-a-compass-to-give-zero-to-north/16972/2 "2019-03-19T22:06:34Z")

</div>

Hello.

It sounds like you are using our [LSM303D carrier](https://www.pololu.com/product/2127) and trying to make a tilt-compensated compass. We have an Arduino library for that board, which has [example code for getting tilt-compensated heading](https://github.com/pololu/lsm303-arduino/blob/master/examples/Heading/Heading.ino). (A link to the [GitHub page](https://github.com/pololu/lsm303-arduino) for that library can be found under the [Resources tab](https://www.pololu.com/product/2127/resources) of that sensor’s product page.)

You might find that library especially useful if you are reading your LSM303D from an Arduino. If you are not, you could probably use it as a guideline for writing your own code.

-Jon

---

<div class="post-metadata">

### Author: ![Jim\_Remington](https://forum.pololu.com/letter_avatar_proxy/v4/letter/j/a3d4f5/32.png) [@Jim\_Remington](https://forum.pololu.com/u/Jim_Remington)
#### Post date: [March 20, 2019, 12:09am UTC](https://forum.pololu.com/t/lsm303d-making-a-compass-to-give-zero-to-north/16972/3 "2019-03-20T00:09:52Z")

</div>

To make a useful compass, you will need to individually calibrate the magnetometer, and of course, correct for the magnetic declination in your area. The best procedure is summarized and applied in this post: [Correcting the Balboa magnetometer](https://forum.pololu.com/t/correcting-the-balboa-magnetometer/14315)
