Anyone good here at Calibrating the HMC5883L compass module for Arduino Mega. I have tried a number of methods. I'm on a Mac so I'm not sure if that's the issue. I have run the Calibration script numerous times and I keep getting different result. Seems like no matter what I do my compass module seems to be off by a good 15 degrees or so.
I'm using this Calibration Code but either I am using it wrong, I seem to get different results every time I run it which is making it impossible to calibrate the sensor in order to get accurate readings, seems like no matter what I do my sensor is off by a good 15 degrees or so.
/*
Calibrate HMC5883L. Output for HMC5883L_calibrate_processing.pde
Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-magnetometr-hmc5883l.html
GIT: https://github.com/jarzebski/Arduino-HMC5883L
Web: http://www.jarzebski.pl
(c) 2014 by Korneliusz Jarzebski
*/
#include <Wire.h>
#include <HMC5883L.h>
HMC5883L compass;
int minX = 0;
int maxX = 0;
int minY = 0;
int maxY = 0;
int offX = 0;
int offY = 0;
void setup()
{
//Serial.begin(9600);
Serial.begin(115200);
// Initialize Initialize HMC5883L
while (!compass.begin())
{
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);
}
void loop()
{
Vector mag = compass.readRaw();
// Determine Min / Max values
if (mag.XAxis < minX) minX = mag.XAxis;
if (mag.XAxis > maxX) maxX = mag.XAxis;
if (mag.YAxis < minY) minY = mag.YAxis;
if (mag.YAxis > maxY) maxY = mag.YAxis;
// Calculate offsets
offX = (maxX + minX)/2;
offY = (maxY + minY)/2;
/*Serial.print(mag.XAxis);
Serial.print(":");
Serial.print(mag.YAxis);
Serial.print(":");
Serial.print(minX);
Serial.print(":");
Serial.print(maxX);
Serial.print(":");
Serial.print(minY);
Serial.print(":");
Serial.print(maxY);
Serial.print(":");*/
Serial.print(offX);
Serial.print(":");
Serial.print(offY);
Serial.print("\n");
}
Is there some way I'm not using this Calibration Code correctly? From what I understand you run it and just rotate the compass around a few times? But I get different results every time I run it which is making it impossible to calibrate the compass in order to get accurate readings.... and YES I have taken steps to make sure nothing is near the compass that could throw off the readings.
That particular approach is poor. It estates the offset, but not scale factor, for X and Y only. That will work for a compass only if the magnetometer is held perfectly level at all times, and then not very well.
In one of your other posts on this topic, I provided links to much better calibration methods.
jremington:
That particular approach is poor. It estates the offset, but not scale factor, for X and Y only. That will work for a compass only if the magnetometer is held perfectly level at all times, and then not very well.
In one of your other posts on this topic, I provided links to much better calibration methods.
Yes I know but the link you provided was for a PC only calibration method. I'm on a Mac.
Knightriderguy, I have seen your youtube video and I noticed a number of things.
You do not only rotate the magnetometer, you also move it around in a circle. And you seem to do only the x,y axis. And it is in a building, with electrical wires, perhaps concrete with reinforcement.
You need a cleaner environment (with less magnetic disturbance), move the magnetometer slowly around in all 3 axis, while keeping it in the same location.
Koepel:
Knightriderguy, I have seen your youtube video and I noticed a number of things.
You do not only rotate the magnetometer, you also move it around in a circle. And you seem to do only the x,y axis. And it is in a building, with electrical wires, perhaps concrete with reinforcement.
You need a cleaner environment (with less magnetic disturbance), move the magnetometer slowly around in all 3 axis, while keeping it in the same location.
I'm thinking of seriously scrapping the Compass part of the function. If it's having this kind of difficulty getting calibrated inside of a simple house I can't see it retaining any sort of degree of accuracy when installed in an 82 Trans Am lol?
When you put it in a car, you'll have the worst of environments. The normal method of calibrating a magnetic car compass is to slowly drive in circles in an open area free of local disturbances. You need to do the equivalent with the module.
Arctic_Eddie:
When you put it in a car, you'll have the worst of environments. The normal method of calibrating a magnetic car compass is to slowly drive in circles in an open area free of local disturbances. You need to do the equivalent with the module.
lol thanks Eddie, and what a joy treat that will be eh.... Oh well gotta be done when the time comes... for now it will just have to do then as is
It's close, just off by about 10 or 20 degrees but it does point somewhat northerly
The good side is that when done right and no changes in the environment, it's only needed once. My car compass still uses the factory calibration even though I added a 100W ham transmitter. The 8awg power cables are routed as a pair and follow metal surfaces. The magnetic field from one wire is canceled by the other since the current is going the other way in return.
The factory procedure is to actually drive slowly in a circle until receiving the Done message on the display center. It's just sometimes hard to find a parking lot free of buried re-bar and light poles.
This is why you aren't allowed to take large magnets onto planes - if GPS fails they only have
magnetic compass to tell direction and they need a magnetically clean environment. Ships with steel
hull need careful compass calibration. MAGNETIC COMPASS ADJUSTMENT - HONG KONG
Smaller Navy ships, like the destroyer I was on, need to periodically go through a degaussing operation. This requires the ship to go between two long docks out in the bay, Charleston, SC, where it is wrapped with cable about 2" in diameter. An AC current of 1000A is started and slowly decreases to zero over eight hours. That gets it back to the tricky calibration part but at least it's not a floating bar magnetic.