Go Down

Topic: Magnetometer sensor and arctan (Read 610 times) previous topic - next topic


I am in need of your combined wisdom please.
I am trying to use a 'triple axis compass magnetometer sensor'
I ordered some HMC5883L but what was delivered actually uses a clone QMC5883L.
Once I found the data sheet for that at least I could talk to it!
The problem is it does not give anything like accurate readings - although they are consistant.
I ended up driving my toy in a circle (or 2) and taking readings as I went.
When graphing these they show a typical arctan graph - which means that at some points a very little change in physical direction results in a large change in heading (an asymptote?).
ie there is very little movement needed to get from 0-45

This cant be right if people are using these to drive drones or things.
Alas these are the only sensors I have access to at the moment so I have nothing to compare this against.
Has anybody else done a simple circle test with a magnetometer to tell me if this is typical behaviour ?

The sensor returns typical x,y,z values
and the formulae I am using is
  float heading = (atan2((int)y,(int)x));
  headingDegrees = (heading * C_rad_to_deg);

Thanks JC


Oct 30, 2017, 04:27 am Last Edit: Oct 30, 2017, 04:31 am by jremington
You MUST calibrate a magnetometer before attempting to use it as a compass. Great overview on calibration here.

Note that atan2() expects float or double as arguments, not integers.
Code: [Select]
float heading = (atan2((int)y,(int)x));


Thanks for the feedback.  That 'overview' of calibration is too heavy for my brain.
Furthur investigation shows some alternatives
see https://github.com/kriswiner/MPU6050/wiki/Simple-and-Effective-Magnetometer-Calibration

But my favourite and the easiest to implement is at
In the magmaster program press 'How to use the results' then 'Show Arduino Example code', just paste this out and fill in your values

This did change my graph but it is still not 'smooth'



The closed source program Magmaster can correct only a limited range of errors, because so few data points are collected and only at certain magnetometer orientations.

The open source program Magneto will work much better, and will work with any magnetometer.

Go Up