# SparkFun Forums

### how to calculate compass heading with magnetometer data?

Have questions about a SparkFun product or board? This is the place to be.

### how to calculate compass heading with magnetometer data?#12044

By NleahciM
#12044
Hi - I just got my micromag3 working yesterday. Can anybody advise on how to interpret the data coming out from it into a useable compass heading? Thanks!

edit: to clarify, my understanding is that to get a compass heading from a 2 axis one you take the arctan of y/x. But that only works when the module is completely flat. I have a 3 axis accelerometer onboard so I guess my question is, how do I couple that with the compass to give an accurate heading at any orientation?

### MicroMag3#12119

By plus10db
#12119
In the simple case (standing perfectly still) you use a 3-axis accelerometer to provide the orientation reference to calculate compass headings from your 3-axis magnetometer. But then, if you were standing perfectly still why would you need a 3-axis magnetometer?
Since the world of interest isn't static in order to make sense of the accelerometer readings you'll need to correlate it to a 3-axis rate gyro. All that sensor fusion and the math to rectify orientation in free space is daunting.
That said, look at video game software for some inspiration. They have solved the orientation issues and a little insight, use quaternion's if you can afford the computational costs.

### Not complicated!#12122

By OldCow
#12122
If the magnetometerâ€™s x- and y-axis are horizontal, then the z-axis is vertical. The â€œmagnetic headingâ€

### Re: MicroMag3#12124

By NleahciM
#12124
plus10db wrote:In the simple case (standing perfectly still) you use a 3-axis accelerometer to provide the orientation reference to calculate compass headings from your 3-axis magnetometer. But then, if you were standing perfectly still why would you need a 3-axis magnetometer?
Since the world of interest isn't static in order to make sense of the accelerometer readings you'll need to correlate it to a 3-axis rate gyro. All that sensor fusion and the math to rectify orientation in free space is daunting.
That said, look at video game software for some inspiration. They have solved the orientation issues and a little insight, use quaternion's if you can afford the computational costs.
Movement will be very slow, so I believe that the accelerometer will provide fairly accurate readings. This is on a 6 legged robot, by the way. Also, I have been thinking I might have it stop moving and completely turn off all motors when taking readings, as I'm worried that the motors will have a large effect on the readings. Thus for now I'm going to assume that the accelerometer is giving perfectly accurate values for the time being.

### 3-xis compass/accelerometer#12126

By plus10db
#12126
Since your robot will be essentially flat you can look at the code from Cypress AN2272. They use different parts but the math is the same ;-)

### #12898

By nils
#12898
doesn't the 3axis magnetometer of PNI give the earth's local magnetic field vector (with a field direction NS)? If so, why would you need anything else (i.e. a gyro or accelerometer)?

### PNI three axis magnetometer#12902

By plus10db
#12902
It sure does, but which one is up? That's where the accelerometer comes in, to tell you the orientation of the system so you can rectify the magnetometer readings.

If you know your robot will always be 'level' you can use a 2-axis sensor and get reasonable results without tilt compensation. That's what I'm doing on my hexapod and I don't shut the motors off for readings, external fields, like my refigerator or metal hand railing, are much bigger problems.

### Re: PNI three axis magnetometer#12909

By JC
#12909
plus10db wrote:It sure does, but which one is up? That's where the accelerometer comes in, to tell you the orientation of the system so you can rectify the magnetometer readings....
And since you need accelerometers you need rate also? So for a robust tilt compensated compass you need obviously 3 axis mag, which requires accelerometers, which requires rates??

I always woundered that if using 3 axis mag you could reduce some of the sensors. Guess not. I'd say the bare min you need would be what is included in Honeywell's 3 axis compass IC kits.

### #12912

By SOI_Sentinel
#12912
Rate, no. You use the accelerometers in a relatively stationary situation to tell where "down" is. If you're not going to be recalibrating in the field/on the move, you won't need the accelerometers. The three axes are useful for determining lattitude, but perhaps the best use has been in applications to help determine altitude without (and more accurately than) GPS.

Another use is that once tuned to your local area, you can use it as an absolute attitude sensor, helping to compensate for gyro drift in a 6 axis IMU. Won't help a lick with the accelerometers, though.

### Necessity to know where's "up": 2 axis acceleromet#12943

By nils
#12943
OK so am I getting this right?: The magnetometer gives you the angle to the earth's field but you do not know if the angle measured is to the horizontal plane or the vertical axis or both. (An angle to north could be caused by turning the device right/left or by tilting it). The compass heading is only "the turn" but not "the tilt" so you need a reference to the horizontal plane to calculate it.

I think a 2-axis accelerometer should be enough. The missing 3rd accelerometer axis creates an ambiguity. Each accelerometer gives you the titlt along one axis, with an ambiguity of the sign of the tilt (right/left or forward/backward). In total, you have two pairs of two possible horizontal planes (4 in total). that differ by 90 degrees.
You know the magnetic vector will never have more than 45 degrees to the real horizontal plane. It is therefore easy to distinguish the three "virtual" planes from the real horizontal plane if you have the magnetometer.

I am not sure that is correct. It just appears to me that given the magnetometer info, you can use just a 2-axis accelerometer and you are done.

### #12964

By SOI_Sentinel
#12964
Yup. A 3 axis accelerometer would only be required in situations where you could be flipped upside down. Even them just a mechanical ball-cage sensor would work to tell you which side is up

### Re: how to calculate compass heading with magnetometer data?#200808

By cnd
#200808
You first need calibration (either using the device calibration method, or, by rotating it in a complete circle to ascertain the full field strengths).

You can then use the X, Y, and Z readings to compute the location of "north" relative to whichever way you're pointing (keeping in mind that "north" changes as much as 30 degrees depending where on earth you are (called declination - see http://www.magnetic-declination.com/), and that it's not horizontal to the ground either (confusingly, called "inclination" - see https://physics.stackexchange.com/quest ... -wh/283782 )

If you picture the X, Y, and Z readings as meaning "where on an imaginary circle is the direction of magnetism - where those 3 circles are on 3 different planes, you can compute the logical position of a vector pointing towards magnetic north in 3 dimensions.

The bad news - there's no simple way to do what you want accurately, and definitely not one that's going to work without trig math on all 3 readings (you can't just use X and/or Y alone).

The good news - you probably don't really need to know an actual "heading" - most applications just need some local relative direction as compared with something you already know.

Beware that strong magnets (such as ones in all modern motors) seriously mess with readings from a long way away (10 or more foot even) - grab an app from your phone store that shows your phone magnetometer readings, and wave a magnet around to see this easily!

This page has detailed with code purportedly working (although not accommodating declination/inclination AFAIK)

Fun fact: pigeons can literally "see" magnetism... but only if the sky is blue (no joke; the blue is part of their eyes' mechanism to see this)