Using magnetometers


Magnetic field is measured using a sensor called a magnetometer. The most common type of solid state magnetometer is a Hall effect sensor and it measures the magnetic field normal to the chip surface. It’s very useful to be able to measure the three orthogonal components of magnetic field vector. So typically, three orthogonal Hall effect sensors are packaged in to a single chip along with all the associated electronics and typically these have a simple serial interface that allows them to be easily connected to a micro-controller. So, in order to determine the orientation of a body in space, we use the accelerometers to determine the downward direction, the direction of the gravity vector. And, we use that to determine the magnetic field component in the horizontal plane which we can think of as being the same as the direction of the needle points on a traditional mechanical compass.

To make this tangible, consider again the example of my phone and I have attached a body frame to the phone. The z-axis is out of the screen, y-axis is up the screen and the x-axis is across the screen from left to right. I’m going to align a magnetometer with each of these axes and the result is called a tri-axial magnetometer because there are three magnetometers there.

Let’s talk now about how we process the information from the magnetometer. Once again, I have a world coordinate frame denoted zero. Once again, the z-axis is parallel to the gravity vector. It’s pointing straight upwards. And, the x-axis is aligned with the magnetic north. In this case, I can write the magnetic field vector with respect to the world reference coordinate frame as B, the magnitude of the magnetic field vector which I can look up in a table if I know my location on the planet, and the cosine and sine of the magnetic inclination angle. This vector lies in the x-z plane. It has got no component in the y direction. The rotational transform from the world frame to the body frame is given in terms of the roll, pitch and yaw angles.

We have already talked about how to determine the roll and pitch angles from the accelerometer measurements. The magnetometers in the phone measure the magnetic field in the coordinate frame B and that allows me to write this relationship. And, now we can solve for the yaw angle psi and it is in terms of the roll and pitch angles that we determined from the accelerometer measurements. The measured magnetic field strength, BX, BY and BZ, the known length of the magnetic field vector, capital B, and the known inclination angle, capital I. And both B and I, we can look up if we know our location on the surface of the planet.

The magnetic field of the earth is quite weak. Therefore, magnetometers need to be very sensitive to magnetic field and this makes magnetometers very susceptible to stray magnetic fields that are created by electric currents flowing inside electronic devices and also from electric motors. Roboticists building robots that are operate indoors are often frustrated by the structural steel elements of the building which distort the earth’s magnetic field. There are also power cables in the walls and there are Wi-Fi access points and equipment closets which also create magnetic field which can confuse robots that rely predominantly on magnetic field sensing.

I really like this old picture of the Pioneer ten satellite which is exploring the far reaches of our solar system. There’s a bunch of electronics and a rocket motor on the back of a large dish which is pointed back at Planet Earth. And, on a long slender boom is the magnetometer instrument which is sensing the local magnetic field and they’ve position that is far away as they can from the rest of the electronics in the space craft. And, that’s for exactly this reason that magnetometer is very sensitive to stray magnetic fields.


There is no code in this lesson.

We learn how to use information from three magnetometers to determine the direction of the Earth’s north magnetic pole.

Professor Peter Corke

Professor of Robotic Vision at QUT and Director of the Australian Centre for Robotic Vision (ACRV). Peter is also a Fellow of the IEEE, a senior Fellow of the Higher Education Academy, and on the editorial board of several robotics research journals.

Skill level

This content assumes an understanding of high school-level mathematics, e.g. trigonometry, algebra, calculus, physics (optics) and some knowledge/experience of programming (any language).

More information...

Rate this lesson


Check your understanding


  1. radirobi97 says:

    Dear Professor Peter Corke!

    I have magnetometer in my phone, the z axis is pointing up,same configuration you showed in the video.
    If I have the values of magnetic field in X, Y, Z direction, is it possible to determine the roll and pitch angle?

    Thank you,
    Robert Radi

    1. Peter Corke says:

      Sadly it cannot. The magnetometer tells us everything about a vector, but the direction of a vector can be described by just 2 angles. For magnetic field vector those angles are declination (direction relative to north pole) and inclination (direction into the ground). To get roll and pitch angles, generally defined with respect to down (gravity direction) you need to use an accelerometer.

Leave a comment