MicroMag3 3-axis Magnetometer

General Description
The Micromag3 is a 3-axis magnetometer that measures the strength of magnetic fields in 3 directions. It can measure magnetic fields that are approximately the same strength as the Earthís magnetic field. Common applications include orientation sensing and for sensing local disruptions in the Earthís magnetic field, such as those caused by vehicles. The datasheet summary can be found here.

Cost and availability
The Micromag3 is manufactured by PNI Corporation and is available through their website for $50. (http://www.pnicorp.com/productDetail?nodeId=cMM3) It is also available at Sparkfun for $55.

There is a similar 2-axis magnetometer available for $40-45 from PNI (depending on whether you buy the module only or buy the module on a carrier board) and for $50 from Sparkfun.

Magnetic Fields
I wouldnít know where to start or stop in an overview of magnetism, so feel free to browse Wikipedia if you are interested in the basics.

For sensing purposes, several aspects of magnetism and magnetic fields are important.

For starters, magnetic fields are vector fieldsóthat is, if youíre measuring a magnetic field at a given point, that field will have both a strength and a direction. The Micromag3 can measure the strength of the magnetic field thatís perpendicular to the orientation of a single sensing element. Thus, by measuring magnetic field strength on three perpendicular axes, it is possible to measure the strength and direction of the field in three-dimensional space.

There are probably four main situations in which you are likely to interact with magnetic fields. One, the earth has an inherent magnetic field. Measuring that field on two axes can provide north-south orientation, and measuring that field on 3 axes, in combination with some other sensors, can provide three-dimensional orientation.

Second, large metal objects (such as cars), can disturb the local strength and direction of the earthís magnetic field. Measuring changes in magnetic fields in a location can be used to detect the presence of such objects.

Third, permanent magnets produce magnetic fields (usually much stronger than the earthís magnetic field). Similarly to detecting the presence of vehicles, there may be some possibilities for using a magnetometer to track the position of a known magnet.

Fourth, all electric currents produce magnetic fields perpendicular to the direction of the current (and vice versaóa magnetic field can be used to induce a current, which is how the Micromag3 works). A circuit (such as a solenoid) can be designed to intentionally create a magnetic field, or the current in a circuit may produce noise that interferes with a magnetometerís functioning.

How It Works
The Micromag3 measures magnetic field through magnetoinductance. A description of the circuit and how the sensor works can be downloaded from PNI here, and this 1998 article from Sensors magazine describes a variety of methods for sensing magnetic fields, including magnetoinductance. This1996 article from EDN also compares several methods of measuring magnetic fields.

A magnetoiductive circuit consists of a coil around a ferromagnetic core that is incorporated into a circuit that forms a relaxation oscillator. Charge gradually builds up in the circuit, is rapidly discharged, and then starts to gradually build up again, and so on. The frequency of this oscillation varies with the strength of the magnetic field perpendicular to the coil. In the micromag3, the oscillation that is produced is a square wave, which can be easily read as a digital signal.

The Micromag3 calculates magnetic field strength by comparing two measurements from the same circuit. First one end of the circuit is grounded, and the other oscillates. Then the other end is grounded, and the first oscillates. Subtracting one result from the other provides temperature stabilization and (I think) the direction of the magnetic field.

Below is an example of a magnetoinductive circuit for one axis:

One pin is selected, and the output is an oscillating square wave. The period of 256 cycles of the square wave is measured. To measure on one axis, first pin 1 is selected, and the period measured, then pin 2 is selected and the period measured, and their difference is the final result. This result should be linearly proportional to the magnetic field strength.

Without calibration, this measurement gives a relative but not absolute measurement of magnetic field strength. The Micromag datasheet provides graphs relating output values from the sensor to magnetic field strength. This varies somewhat with changes in temperature, but the variation is small up to approximately 1000 Tesla, and very small below 500 Tesla.

For direction sensing, only relative field strengths between different axes are needed. By convention, the magnetometer is set up so that the positive end of the X axis points north, and the positive end of the Y axis points east.

The benefits of magnetoinductive magnetometers appear to include:

  • a high resolution response to changes in magnetic field; the frequency can change by as much as 100% when moving from a parallel to perpendicular position in relation to a magnetic field.
  • fairly stable measurement, without strong drift over time or sensitivity to temperature (the module specifically includes circuitry for reduction of noise and stabilization over changes in temperature).
  • the ability to function at low voltages (3.3V or 5V), which makes the sensor easier to use in typical prototyping setups.
  • an output (a square voltage wave) that is inherently digital.

Common Applications
Specific applications of magnetometers depend on their strength and sensitivity. Magnetometers that sense the Earths magnetic field can be used as digital compasses and as 3-dimensional orientation sensors, such as in aviation and roboticsóthis appears to be one of the primary intended purposes of the Micromag3. Magnetometers of this strength can also be used to detect the presence of large metal objects, which disrupt the Earthís magnetic field in that location. They can be used to sense vehicles, and are also used in archaeology to non-destructively image features under the ground. Very sensitive magnetic sensors can be used in medical applications for measuring aspects of organ functioning.

How to Use It
The Micromag3 is designed to work with SPI (synchronous peripheral interface), a system in which a master device (in this case an Arduino microprocessor) sends clock pulses to the slave device (the magnetometer) to synchronize the sending and receiving of data. Generally, the master device sends a command, waits until the slave completes its reading, and then reads the result, with the clock used to time when each bit gets sent.

Although the Arduino board contains some built-in hardware and a software library for handling SPI, the magnetometerís SPI is quirky enough that it ended up being easier to write the code to explicitly handle the interface, rather than work with the existing library.

Wiring up the magnetometer is straightforward, as it is already arranged on a board with connectors spaced to fit on a breadboard. The last page of the datasheet explains the pin layout clearly.

Although the magnetometer is designed to run on 3.3V, it can handle 5V. I was mildly paranoid about burning it out, so I put a 200 ohm resistor between the power and the voltage input of the magnetometer, but I donít think it made much difference.

Here is the Arduino code for controlling the magnetometer, and here is a simple Processing code for calculating orientation based on the X and Y axes. The code is set up to take a reading of the X axis, then the Y axis, then the Z axis, and repeat. In my applications, I only ended up using the X and Y axes, because 3-dimensional orientation requires additional correlation with accelerometer data. It is important to note that to get accurate orientation data from X and Y axes, the magnetometer should be as close to level as possible.

The comments in the code explain many of the important considerations and quirks of the magnetometer. In particular, there are three things that were notably different from other SPI protocols and/or required some outside research on my part:

  1. The protocol requires sending one byte of command information to the magnetometer, and the magnetometer returns 2 bytes for the reading value. Each command byte can activate only one axis to be read.
  2. The value that is returned is 2 bytes of data in 2ís complement format. Because the returned value can be negative or positive, the first bit is used to designate the sign. The remaining 7 bits are sent, most significant bit first, to designate the value. Thus, the returned numbers can range from -32768 to 32767.
  3. Every time a command is sent, voltage to a reset pin must be pulsed.

Most references to calculating orientation from the X and Y axes simply state that the orientation is equal to the arctangent of y/x. Clearly, this leads to errors any time x=0.

An online student paper, Autonomous Control of Model Aircraft for Photographic Reconnaissance, by Chris Hansen and Mark Inderhees, (the link is to download a Word document) provided the following equations, which worked well:

For orientation in radians:

  • if x = 0 & y < 0, orientation = pi/2
  • if x = 0 & y > 0, orientation = 3*pi/2
  • if x < 0, orientation = pi - arctan(y/x)
  • if x > 0 & y < 0, orientation = -arctan(y/x)
  • if x > 0 & y > 0, orientation = 2*PI - arctan(y/ x)

Performance and Some Data Analysis
While working with the magnetometer, I found that it tended to respond somewhat slowly to changes in orientation. A known problem with magnetometers is hysteresis, in which the ferromagnetic material used to measure the magnetic field becomes magnetized in response to an external magnetic field. When the external field changes, the materialís now-inherent magnetism affects its response to change. The stronger the applied field, the stronger the hysteresis, and the slower the response.

Also, the magnetometer works for magnetic fields similar in strength to the earthís magnetic field. The application of a magnetic field that is too much stronger can cause the instrument to stop working. When reading reviews of the magnetometer, one of the complaints I saw was that, in robotics applications, the magnetometer failed to give good readings when the motor to run the robot was turned on.

I decided to explore the magnetometerís hysteresis and response to external fields in the following fashion. I physically attached the breadboard holding the magnetometer and its circuit to the dial of a potentiometer (using hot glue). With the base of the potentiometer anchored, rotating the magnetometer causes the potentiometer to rotate, giving reliable potentiometer readings of angular position.

Here is the Arduino code, and here is the Processing code I used to record the data. I recorded the time before and after a given set of measurements was taken, the magnetometer data for three axes (although I only considered two axes), and the potentiometer reading. In each instance, I tried to rotate the potentiometer both quickly and slowly, to get a sense of how the speed of rotation affected the magnetometer response. I saved collected data to a file, which I then copied into Excel for some graphing and analysis.

I saved data under three conditions:

  1. The magnetometer running under Ďstandardí operating conditions, which in this case meant at the computer lab, with a number of computers and other electronics nearby, but nothing specifically creating large magnetic fields.
  2. With a 9V DC battery running about a foot away from the magnetometer.
  3. After the motor was run, again under standard operating conditions.

The speed of response was affected by the amount of time it took to send and receive the SPI commands, measure three axes, and communicate with the computer. It took 280-285 ms to complete one set of measurements (3 axes plus potentiometer), which would provide a bit fewer than 4 measurements per second. This is noticeably slow.

It was difficult to get data while running the motor, because the motor tended to cause the whole system to break. After about a minute, the readings started going horribly awry, and then the computer registered too much of a current draw through the USB cable so stopped transmitting and receiving information. So thatís a serious limitation!

Below are several graphs showing the relationships between the potentiometer and magnetometer results, with the readings scaled and offset to be comparable.

A Slight Detour to Explain Scaling of the Data
The magnetometer provided calculated orientation readings from 0 to 2*pi, and the potentiometer provided measurements from 0 to 1023. The potentiometer needed to be scaled from 0-1023 to the same range as the magnetometer. The potentiometer only covered 7/8 of a complete revolution between 0 and 1023. So it scaled to approximately 0 to 1.75*Pi. In addition, the measurements correlated as 0 to 1023 for the pot, and 2*Pi to 0 for the magnetometer, so the direction needed to be reversed. The equation to scale the pot measurement was 1.75*Pi*(1-potValue).

In addition, there was an offset--the 0 position on the potentiometer did not line up with the 0 position on the magnetometer. First I eyeballed the numbers to identify the magnetometer measurements that had passed 2*Pi and cycled back around to 0. I added 2*Pi to them. Then I calculated the average difference and subtracted that from the magnetometer measurements.

The magnetometer offset equations were:

  • before the motor: offset magnetometer = magnetometer orientation - 1.024
  • during the motor: offset magnetometer = magnetometer orientation - 0.656 (this involved some error, because the magnetometer changed in relation to the pot, as it was affected by the motor's magnetic field)
  • after the motor: offset magnetometer = magnetometer orientation - 0.454


This graph was taken without any specifically added outside sources of magnetic fields. Comparing the difference between measurements against the magnitude of change from one measurement to the next (for example, the change in the pot value from 24998 ms to 25288 ms) doesnít seem to show a clear difference between accuracy in response to quickly or slowly turning the magnetometer. The standard deviation of the differences between the measurements was 0.400, about 7% of the range of measurement, but itís not clear that the error is specifically a lag in response time to changes in orientation.

Some clear hysteresis can be seen in the readings that were taken near a running DC motor:

For the first 30 seconds, the magnetometer readings related to its orientation, but after about 35 seconds, I stopped moving the pot, but the magnetometer values continued to steadily change. Then the whole system went berserk and shut itself down. (This happened a few times; this was the only time I was able to get data saved).

Although the statistics arenít as straightforward, because of the clear change in behavior as the magnetometer responded to the motor, the standard deviation of the differences between the measurements was 0.605, about 11% of the total range of measurement.

I noticed, even after turning the motor off and letting the magnetometer recover for a few minutes, noisiness persisted.

Although there still isnít a clear connection between speed of rotation and error, the overall noisiness of the system was increased. The standard deviation of the difference between the measurements was 0.855, about 16% of the total range of measurement.

The differences can seen from a different perspective using a Bland-Altman plot, which compares the difference between the measurements at any given point with their respective average measurement.

These graphs make it easier to see the change in the quantity of error in the different situations.

There's also a saw tooth slope of error (clearest in the first graph, but probably present in all three.) My guess is that this is a reflection of a slight tilt in the magnetometer, either relative to the potentiometer or relative to the ground. The X and Y axes give an accurate orientation only if the magnetometer is level relative to the Earth's surface. Tilting it introduces error, unless readings from the Z axis are used to correct it.

Example Application Using the Magnetometer:
Orientation as a Way to Control Perspective in a Video

One of my other projects this semester involved exploring different ways to display video that was captured in 360 degrees. I used the magnetometer to play with one method of displaying the video. This Processing code, with the basic Arduino code from above, displays a 360 degree video, with the orientation of the magnetometer controlling the angle at which one views the video.

View video.

Some of the difficulties with speed of response and noise (which could be smoothed, although a simple weighted average hiccupped when the orientation switched over from 2*Pi to 0, which is a smooth transition in display but not numerically) are apparent in the video.

The initial idea was to set up a freestanding screen that could be moved without using a track or other moving parts to track the screenís orientation. Orientation changes for a larger screen might minimize the response time problems because the screen would move more slowly. We ultimately went with a different setup (a potentiometer controlled by rotation of a chair), although we are still working with the idea of tracking the viewerís orientation to control the display.

Conclusions, Ideas for Applications, and Directions for the Future
Overall, I have mixed feelings about this sensor. Itís fun to play with and Iím glad to have an excuse to have it in my toolbox, but from an ITP/prototyping perspective, Iím not sure what sorts of applications make it worth the $50 price tag, especially given its slow response time and overall noisiness and sensitivity to the environment. (In terms of response time, though, I included measurement of the Z axis for thoroughness in my code, but I didnít use it. It should be possible to speed up the response by approximately one third just by skipping that measurement, and there may be other ways to optimize the code.)

The most obvious application remains orientation sensing, which can be valuable in robotic systems.

Although I bought the three-axis version because it was only $5 more than the 2-axis version, I didnít end up using the third axis. The use of a magnetometer in combination with an accelerometer is supposed to provide robust 3-dimensional position and orientation data. (This combination of the two sensors would be a valuable sensor report, if you happen to be in a future sensor workshop and are looking for ideas.)

The uses of magnetometers for detecting metal objects might provide some interesting information, and it might be possible to figure out how to track the location of a single magnet some distance from the magnetometer.

In the example application above, although we didnít end up sticking with the magnetometer as our sensor of choice, I found the idea of using orientation sensing valuable. Often, I find that my ideas about how to design an interaction or piece of media get constrained by the sensors I am familiar with, and I focus on sensing parameters such as proximity, translational movement, force, or acceleration. Working with the magnetometer forced me to deal with an aspect of movement (especially divorced from translational movement) that I donít think about as often and suggested approaches that I might not have otherwise considered.

Overall, I would recommend using this sensor if you need to--if the magnetometer will uniquely measure something that's not accessible using a cheaper and/or more stable sensor, and if the project will be in an environment where external magnetic forces won't overwhelm the sensor.