LSM303 DLH Heading not working properly? Digital Compass Spark Fun LSM303 DLH

Hi there,

We are using the book 'Making Things Talk' as a guidance to get heading from the Spark Fun LSM303DLH digital compass.
The Accelerometer seems to be ok, but the Magnetometer data we get is strange:

North: around 275
East: around 305
South: around 275
West: around 245

So it's not useful at all. At least the values in between smoothly change.

  1. Has anyone made their Spark Fun LSM303DLH work properly?
  2. Does anyone know what might have gone wrong? How to solve this?

We are pretty sure our cables are right.

Our Code:

// include the necessary libraries:
#include <Wire.h>
#include <LSM303DLH.h>
#include <Button.h>

const int modeButton = 2; // pushbutton for calibration mode
const int buttonLed = 3; // LED for the button

// initialize the compass library
LSM303DLH compass;
// initialize a button on pin 2 :
Button button = Button(modeButton,BUTTON_PULLDOWN);
boolean calibrating = false; // keep track of calibration state

void setup() {
// initialize serial:
Serial.begin(9600);
// set up the button LED:
pinMode(buttonLed,OUTPUT);
// start the Wire library and enable the compass:
Wire.begin();
compass.enable();
}

void loop() {
// if the button changes state, change the calibration state
// and the state of the LED:
if(button.isPressed() && button.stateChanged()){
calibrating = !calibrating;
digitalWrite(buttonLed, calibrating);
}

// if you're in calibration mode, calibrate:
if (calibrating) {
compass.calibrate();
}
else { // if in normal mode, read the heading:
compass.read();
int heading = compass.heading();
Serial.println("Heading: " + String(heading) + " degrees");
}
delay(100);
}

What are you supposed to do during calibration?

did you get the device to read properly we are having the same problems and getting some strange numbers at random