I've installed the HMC5883L library and loaded the sample code:
#include <Wire.h>
#include <HMC5883L.h>
HMC5883L compass;
void setup()
{
Serial.begin(9600);
// Initialize Initialize HMC5883L
Serial.println("Initialize HMC5883L");
while (!compass.begin())
{
Serial.println("Could not find a valid HMC5883L sensor, check wiring!");
delay(500);
}
// Set measurement range
compass.setRange(HMC5883L_RANGE_1_3GA);
// Set measurement mode
compass.setMeasurementMode(HMC5883L_CONTINOUS);
// Set data rate
compass.setDataRate(HMC5883L_DATARATE_30HZ);
// Set number of samples averaged
compass.setSamples(HMC5883L_SAMPLES_8);
// Set calibration offset. See HMC5883L_calibration.ino
compass.setOffset(151,-626);
}
void loop()
{
Vector norm = compass.readNormalize();
// Calculate heading
float heading = atan2(norm.YAxis, norm.XAxis);
// Set declination angle on your location and fix heading
// You can find your declination on: http://magnetic-declination.com/
// (+) Positive or (-) for negative
// For Bytom / Poland declination angle is 4'26E (positive)
// Formula: (deg + (min / 60.0)) / (180 / M_PI);
float declinationAngle = (-10.0 + (49.0 / 60.0)) / (180 / M_PI);
heading += declinationAngle;
// Correct for heading < 0deg and heading > 360deg
if (heading < 0)
{
heading += 2 * PI;
}
if (heading > 2 * PI)
{
heading -= 2 * PI;
}
// Convert to degrees
float headingDegrees = heading * 180/M_PI;
// Output
Serial.print(" Heading = ");
Serial.print(heading);
Serial.print(" Degress = ");
Serial.print(headingDegrees);
Serial.println();
delay(100);
}
I have the SDA in GPIO4 (D2 on the nodelua) and SCL GPIO5 (D1 on the nodelua). It is getting data from the compass, and it *kind of* changes when I rotate the compass. Here's some data:
Heading = 4.70 Degress = 269.33
Heading = 4.71 Degress = 269.73
Heading = 4.71 Degress = 269.97
Heading = 4.71 Degress = 270.00
Heading = 4.70 Degress = 269.24
Heading = 4.69 Degress = 268.99
Heading = 4.71 Degress = 269.84
Heading = 4.76 Degress = 272.81
Heading = 4.91 Degress = 281.09
Heading = 5.10 Degress = 292.35
Heading = 5.26 Degress = 301.51
Heading = 5.34 Degress = 306.19
Heading = 5.37 Degress = 307.90
Heading = 5.40 Degress = 309.59
Heading = 5.41 Degress = 309.98
Heading = 5.42 Degress = 310.46
Heading = 5.42 Degress = 310.60
Heading = 5.42 Degress = 310.49
Heading = 5.42 Degress = 310.54
Heading = 5.42 Degress = 310.38
Heading = 5.42 Degress = 310.28
But that's rotating the module 180 degrees. Does anyone have any idea why it's giving me messed up data?