Hello everyone, I am trying to calculate the rough heading using the QMC5883L Magnetometer using this line of code (\Heading = atan2(y, x)* 180/PI;). I have not calibrated the magnetometer but the values aren't near completing a full 360-degree rotation when I fully rotate it. Here is my code: \#include <SoftwareWire.h>
SoftwareWire myWire(3,2);
// QMC5883L I2C address
#define QMC5883L_ADDRESS 0x0D
double heading;
// QMC5883L register addresses
#define QMC5883L_X_REG_L 0x00
#define QMC5883L_X_REG_H 0x01
#define QMC5883L_Y_REG_L 0x02
#define QMC5883L_Y_REG_H 0x03
#define QMC5883L_Z_REG_L 0x04
#define QMC5883L_Z_REG_H 0x05
#define QMC5883L_STATUS 0x06
void setup() {
Serial.begin(9600);
myWire.begin();
// Initialize the QMC5883L
myWire.beginTransmission(QMC5883L_ADDRESS);
myWire.write(0x09); // Write to the control register
myWire.write(0b00001101); // Set the measurement mode, data rate, and range
myWire.endTransmission();
}
void loop() {
// Read the raw data from the QMC5883L
myWire.beginTransmission(QMC5883L_ADDRESS);
myWire.write(QMC5883L_X_REG_L);
myWire.endTransmission();
myWire.requestFrom(QMC5883L_ADDRESS, 6);
int16_t x = myWire.read() | (myWire.read() << 8);
int16_t y = myWire.read() | (myWire.read() << 8);
int16_t z = myWire.read() | (myWire.read() << 8);
Serial.print("Heading: ");
Serial.println(heading);
heading = atan2(y, x) * 180 / PI;
}\\
When I rotate the sensor 360 degrees holding the sensor as flat as possible, here are my results in the 'txt' file:
RawMagValues.txt
What's wrong, or is this how a magnetometer works when the sensor isn't calibrated?