Hello and Welcome! If you have a technical question please check out our Forums. If you have an order or shipping question please refer to our Customer Support page. Please see all COVID-19 updates here and thank you for your continued support.

Adventures in Science: Using a Magnetometer as a Compass

We can use digital magnetic field detectors as a compass to give our robot an absolute heading.

Favorited Favorite 0

Time for another Adventures in Science! While finding north has never been particularly tricky for the navigators of yesteryear (see: compass), teaching robots how to navigate is a tad more difficult. While GPS can give you a position, it won't immediately provide heading data without gathering several position points first. To remedy this, we have digital magnetometers, which we can configure to act as a compass.

Detecting magnetic fields can be accomplished through a variety of methods, including measuring the buildup of charge carriers on the edges of semiconductor material, or measuring the deflection of a beam with an attached permanent magnet. As technology progresses, these measuring devices continue to shrink in size, which is how we're able to fit magnetometers in smartphones, along with dozens of other sensors.

To get a heading, we need to calibrate the magnetometer, which involves taking samples several times per second and finding which direction yields the strongest magnetic "north" (or "south"). Every time we reset the sensor, we'll need to perform this calibration, as the ambient magnetic fields change depending on your location and surroundings (e.g. electronics, other magnets). The tutorial below will show you how to use the SparkFun MAG3110 magnetometer breakout with Arduino.

MAG3110 Magnetometer Hookup Guide

October 24, 2016

Get started with the MAG3110 3-Axis Magnetometer and learn how to make your own digital compass that senses the Earth's magnetic fields.

For information on the digital compass shown in the video, see these links:

Any tips for getting better accuracy out of a magnetometer when it comes to finding a heading? Feel free to share in the comments!

Comments 6 comments

  • Awesome!! Shawn... you nailed it like always by bringing up new techniques or by reminding old techniques.

  • As usual, Shawn, great video! I especially liked the joke about "heading"!

    I haven't tinkered with magnetometers, but when I was using a couple of accelerometers about a year ago to get the angle between two parts, I found it useful to include some smoothing and hysteresis to make it easier to use. Also, I included the trick of actually taking six readings, summing them but also keeping track of the high and low values, subtracting off the high and low, and then dividing the sum by 4 (fast and easy -- if you're doing it as "binary", shift right two bits) and then actually using that average as the reading.

    Maybe you can answer an "attitude" question that I haven't had time to chase: are accelerometers affected by magnetic fields? (I have one application I've thought about where I'd like to put the accelerometer on a steel structure using a magnet.)

    • I really like that smoothing technique! Were you using a rolling window or just taking 6 and stopping to smooth each collection of 6?

      As far accelerometers go, I can't say I've run into issues with them misbehaving in magnetic fields. However, that may be something I'll have to perform some experiments for another video or if someone here has more experience than me, perhaps they could chime in.

      • There is a library that can help with a rolling window smoothing technique and dropping the max and min. Check out Rob Tillart's running median library and specifically the "float getAverage(nMedians); // middle n numbers" function. For this application, you would initialize the circular buffer as 6 element long, and then call "getAverage(4)". The library numerically sorts the circular buffer into a temporary buffer and returns the average of the middle 4 values.

        I worked with Rob to get that function implemented (see v0.1.04) when I needed to filter the noise out of a sharp distance sensor that I didn't bother to put a bypass capacitor on. There are newer versions on Rob's github, currently v0.1.14. He has lots of other very useful librarys on github.

  • From a navigation standpoint, east is 90 degrees, south is 180, and west is 270. Great video :)

    • Yeah, I remember reading that in my research. For whatever reason, the Arduino library for the MAG3110 gives West as -90. Oh well.

Related Posts

Recent Posts

As Teensy As Pie


All Tags