Go Down

Topic: LSM303 DLH Heading not working properly? Digital Compass Spark Fun LSM303 DLH (Read 1 time) previous topic - next topic


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:
Code: [Select]

// 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:
// set up the button LED:
// start the Wire library and enable the compass:

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) {
else { // if in normal mode, read the heading:
int heading = compass.heading();
Serial.println("Heading: " + String(heading) + " degrees");


Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


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

Go Up