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.
- Has anyone made their Spark Fun LSM303DLH work properly?
- 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);
}