LSM303 DLH Heading not working properly? Digital Compass Spa

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);
}

There may be two issues I can think of when looking at your results.

1- There may be a flaw in translating the compass’ Raw values to a heading.

What does the “compass.heading()” function actually do?

What are the Raw values?

Is it really reading the correct data out of the compass chip?

2- What is the environment you are testing in?

A compass chip is very sensitive to extraneous magnetic field. These fields can be produced by almost anything that contains iron or has current flowing though conductors.

There are a dozen threads here on getting the LSM303 to work. Have you scanned over them to see if any resemble your problem ? I believe a lot of the problems, once past basic communications SNAFUs, have to do with getting the device properly calibrated in it’s working environment. What have you done for calibration ?

Let me also call for some raw data. Kinda hard to diagnose what’s up w/o it. In the past people have collected the raw data for each axis into a text file which can be attached to a post. If you did a simple 360 deg rotation like you did above while collecting the data, it might prove valuable. Stop for a second or 2 at each cardinal point.

Lastly how did you set the gains, especially for the magnetometer ? Is there any chance the readings are maxing out due to too high a gain ?

ps - WRG to the raw data … a line with each sample X accel, Y accel, Z accel, X mag, Y mag, Z mag with commas separating the values (as shown) would be perfect.

And then if you have the time … a shrubbery ! A nice one, not too expensive.

http://www.id-wall.com/images/large/MP0 … 000204.jpg