My first encounter with a GPS device left me wondering why it didn't provide a compass heading when I pointed it in different directions. Then I remembered that the system pinpoints location, and I couldn't get a heading without moving to a new position. What I wanted was a sensor that would detect the Earth's magnetic field. Though it's easy to envision such sensors in handheld devices, I can see many applications in autonomous robots and vehicles that need GPS coordinates and magnetic-heading information.
You can buy magnetometers, but they're not always easy to use, because to the math needed to convert sensor outputs into useful information and the effects of "soft iron" and "hard iron" on magnetometer response. To help simplify magnetometer use for equipment designers, Freescale Semiconductor offers Xtrinsic eCompass software that works with the company's accelerometers and magnetometer to provide applications with accurate magnetic-bearing information. The eCompass hardware incorporates a three-axis accelerometer and a three-axis magnetometer. The accelerometer measures the orthogonal components of the earth's gravity, and the magnetometer measures components of Earth's magnetic field.
Because a design would place both sensors on a circuit board, their outputs change depending on the board's orientation and its local physical environment. According to Freescale, if the PCB remains flat, a processor could compute a compass heading from the arctangent of the ratio of the two horizontal magnetic field components. In most cases, though, a PCB will have an arbitrary orientation, so the compass heading becomes a function of all three accelerometer readings and all three magnetometer readings. For more technical information and the math involved, download the application note, "Implementing a Tilt-Compensated eCompass using Accelerometer and Magnetometer Sensors," Freescale document AN4248.
Although the math looks complicated at first glance -- how's your vector algebra these days? -- Freescale has done most of the work and made it available as free eCompass code.
A four-element model supports hard iron interference correction, and Freescale recommends it for designers who will use the Xtrinsic MAG3110 sensor in applications with minimal soft iron distortion and small amounts of processor RAM.