# Unexpected YAW angle from magnetometer

I have some doubts about a magnetometer application.

I am trying to get YAW angle value from magnetometer data and so I am doing the following:

mag_x_data

mag_y_data

yaw=arctang(mag_y_data / mag_x_data);


After successfully calibrating it, if I work in the xy plane without moving it, I get successful results.

The problem arises when, for example, I move the magenetometer in the x axis (rotating the magnetometer in x axis - ROLL angle) and leaving YAW still, the result of the YAW changes. I understand that this should not happen.

Am I getting it wrong?

What should I do to obtain the YAW values, ​​independent of the position of the X and Y axis, with the magnetometer data?

When I refer to YAW I am talking about rotations in the z axis. Assuming the xy plane corresponds to the surface plane, and the z axis is colinear to Earth's gravity.

• Earth's magnetic field doesn't work in X,Y,Z that way. "North" dips (i.e. points north and downwards) at about 45 degrees where I am, maybe different where you are. Either your magnetometer has a lot of processing internally to remove that and give clean X,Y,Z or it doesn't. And if it doesn't, roll can cross-couple that dip into yaw. Oct 6, 2020 at 22:24
• @MariaC This is a Q&A format site, please make sure you follow the format, thanks. Upvote answers that helped and when someone finally does answer your question, check the box next to that answer Oct 7, 2020 at 21:14

If the magnetometer is ideal (or the mag_x and mag_y signals are calibrated), and the earths magnetic field vector is the largest signal that the magnetometer is seeing, then yes I would expect the above equations to work.

If the mag_x and mag_y signals from the magnetometer are raw, it's likely that there is a gain and an offset in the signals that are intrinsic to most magnetometers that could be affecting your results. Most magnetometers look like this (for x and y):

$$\ Actual Mag_x*gain_x + offset_x = Mag_x\$$
$$\ Actual Mag_y*gain_y + offset_y = Mag_y\$$

If the gain and offset varies significantly the yaw value will be difficult to calculate. The problem is, we don't know the gains or offsets, to find them we need a calibration.

Magnetometers can be calibrated, once the magnetometer is calibrated, you should be able to feed in the calibrated channels into your equation and get a reasonable result.

Also make sure that you aren't in an area with magnetic field noise, there could be other sources that you may be picking up instead of the earths magnetic field. Also make sure you are rotating the magnetometer in the same axis (no translation). A magnetometer by itself can have issues if you're using this for navigation, typically a gyro, accelerator and magnetometer (and sometmies GPS) are paired in an IMU to sense location.

There may also be other things at play, but this could well be what the problem is: the magnetic field is only horizontal at the equator; everywhere else there is a vertical component in the magnetic vector.

Rolling the magnometer will show a part of that vertical component in your X and Y values, and this will give you a wrong bearing.

What you would need to counter that is commonly called tilt compensation, using accelerometers (or other sensors). I can't reproduce the math off the top of my head, but it is easily googled; http://www.brokking.net/YMFC-32/YMFC-32_document_1.pdf is a good read.