A magnetometer is a sensor that measures the Earth’s magnetic field strength to determine cardinal directions. In mobile devices, it works as a digital compass, providing accuracy up to 1–2 degrees with proper calibration. According to Bosch Sensortec Application Note, 2024, modern MEMS magnetometers consume only 0.5–2 mA and fit in a 1.5×1.5×0.7 mm package.
Key Takeaways
Magnetometer in a mobile device is a three-axis magnetic field sensor based on the Hall effect. It measures the projections of the magnetic induction vector on the X, Y, and Z axes. Combined with an accelerometer, the magnetometer determines the azimuth — the angle between the north direction and the phone’s axis.
Modern smartphones use MEMS magnetometers based on the anisotropic magnetoresistive effect (AMR) and the Hall effect. AMR sensors (AKM series from Asahi Kasei Microdevices) provide sensitivity of 0.15 µT/LSB and a range of ±1200 µT — enough to measure the Earth’s field (25–65 µT).
| Parameter | Value |
|---|---|
| Technology | AMR / Hall Effect |
| Range | ±1200 — ±1300 µT |
| Power consumption | 0.5–2 mA |
| Size | 1.5×1.5×0.7 mm |
| Sensitivity | 0.15 µT/LSB |
Without a digital compass, navigation apps cannot determine the direction of movement. Google Maps uses the magnetometer to display the map orientation relative to the user’s viewing direction. When the sensor fails, the app only shows the location on the map without the blue direction arrow.
Hall effect is a physical phenomenon of a potential difference arising across the edges of a conducting plate when exposed to a magnetic field. MEMS magnetometers use thin-film structures made of permalloy (NiFe), whose resistance changes under the influence of an external magnetic field (AMR effect).
Each of the three channels (X, Y, Z) consists of four magnetoresistors connected in a Wheatstone bridge. The magnetic field changes the resistance of the resistors by 2–3%, which is converted into voltage by a differential amplifier. A built-in 16-bit ADC digitizes the signal at up to 100 Hz.
To measure the vertical component of the field, an integrated magnetic concentrator (IMC) is used. This is a thin-film NiFe structure that deflects the horizontal field into the vertical plane, allowing a single AMR bridge to measure all three axes.
Magnetometer determines orientation relative to the Earth’s magnetic field (absolute reference), while a gyroscope only measures angular change (relative data). The gyroscope provides accurate data over short intervals but accumulates drift error. The magnetometer does not drift but is susceptible to magnetic interference.
Android combines data from three sensors via SensorManager.getRotationMatrix. The magnetometer is used to correct gyroscope drift over long intervals. Without a magnetometer, the gyroscope accumulates an error of 10–20 degrees in 5 minutes. According to Google ARCore (2024), the fusion algorithm achieves 1–3 degree accuracy with a magnetometer.
val accelerometerReading = FloatArray(3)
val magnetometerReading = FloatArray(3)
val rotationMatrix = FloatArray(9)
val orientationAngles = FloatArray(3)
SensorManager.getRotationMatrix(
rotationMatrix, null,
accelerometerReading,
magnetometerReading
)
SensorManager.getOrientation(
rotationMatrix, orientationAngles
)
// orientationAngles[0] — azimuth in radians
Hard Iron — a permanent magnetic field created by ferromagnetic materials inside the phone: speaker magnets, vibration motor, metal frame. Hard Iron distortion appears as a constant offset (bias) along each axis. Soft Iron — a change in field direction caused by magnetically permeable materials that distort the field lines.
The standard magnetometer calibration procedure is rotating the phone in a figure-8 pattern. During the process, the sensor records points in three-dimensional space. Without distortions, the points lie on a sphere with a radius of 25–65 µT. Hard Iron shifts the sphere’s center, Soft Iron turns it into an ellipsoid. The calibration algorithm computes offset and scaling parameters.
data class CalibrationParams(
val biasX: Float,
val biasY: Float,
val biasZ: Float,
val scaleX: Float,
val scaleY: Float,
val scaleZ: Float
)
fun applySoftIronCorrection(
raw: FloatArray, p: CalibrationParams
): FloatArray {
return floatArrayOf(
(raw[0] - p.biasX) * p.scaleX,
(raw[1] - p.biasY) * p.scaleY,
(raw[2] - p.biasZ) * p.scaleZ
)
}
Developers can implement their own magnetometer calibration by collecting data as the device moves. The ellipsoid fitting algorithm finds Hard Iron and Soft Iron parameters by minimizing the sum of squared deviations from the sphere. The Apache Commons Math library provides a nonlinear least squares implementation (Levenberg-Marquardt) for this task — accuracy after software calibration reaches 0.5 degrees.
To minimize Hard Iron, manufacturers place the magnetometer in the upper part of the phone, away from the speaker and vibration motor. Apple engineers in the iPhone 16 Pro Max (2024) used permalloy foil shielding for the magnetometer, reducing speaker influence by 4 times. The Samsung Galaxy S24 Ultra uses an additional compensation circuit that measures the speaker current in real time to subtract the induced field. Cases with magnetic mounts (e.g., in car holders) create strong distortions — after use, the magnetometer may show the wrong direction until recalibration.
Android provides two sensors: TYPE_MAGNETIC_FIELD (calibrated data in µT) and TYPE_MAGNETIC_FIELD_UNCALIBRATED (raw data with distortion estimate). For navigation apps, it is recommended to use the calibrated sensor — the system automatically applies built-in calibration based on the last figure-8 motion.
SENSOR_DELAY_NORMAL (200 ms) is sufficient for a compass, SENSOR_DELAY_FASTEST (0 ms) — only for specialized research applications. After receiving values, you can convert them to azimuth via getRotationMatrix + getOrientation. It is important to check sensor accuracy via event.accuracy — SENSOR_STATUS_UNRELIABLE means calibration is needed.
override fun onSensorChanged(event: SensorEvent) {
if (event.sensor.type == Sensor.TYPE_MAGNETIC_FIELD) {
val accuracy = event.accuracy
if (accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
showCalibrationHint()
return
}
System.arraycopy(event.values, 0,
magnetometerReading, 0, 3)
}
}
The magnetometer is used not only for the compass. AR applications, including Pokemon GO, use the magnetometer to determine camera orientation relative to the real world. Without a magnetometer, ARCore switches to Screen Axis mode — orientation is determined only by the gyroscope, which reduces accuracy.
In automotive navigation systems based on Android Auto, the magnetometer is used to determine the direction of movement when GPS signal is lost in tunnels and dense urban areas. In combination with odometry data from car wheels (obtained via OBD-II), the magnetometer allows continued navigation for up to 5 minutes without GPS with positioning accuracy of 10–20 meters. According to Google Maps Engineering Blog (2024), the hybrid algorithm reduces route loss in urban canyons by 40%.
Navigation apps use the magnetometer to determine the azimuth of movement — the angle between north and the user’s current heading. In Google Maps, magnetometer data is combined with the GPS track and movement history to display the blue direction arrow. When GPS signal is lost (in tunnels, underground passages), the magnetometer remains the only source of orientation information.
The new generation of 3-axis magnetometers with integrated ASIC (BMM350 from Bosch, 2024) consumes only 0.35 mA and supports up to 200 Hz. Development of quantum magnetometers and NV centers in diamond for mobile devices has not yet left the laboratory stage. It is expected that by 2028, the accuracy of mobile magnetometers will reach 0.01 µT.
Frequently Asked Questions
Draw a figure-8 with your phone in the air — this is the standard gesture for calibrating the digital compass. If the compass still shows incorrectly, check the case: magnetic mounts and metal elements create strong Hard Iron distortions.
Main reasons: magnetic interference from phone speakers and vibration motor, lack of calibration after changing the case, influence of nearby metal objects. Indoors with reinforced concrete structures, the magnetic field is distorted especially strongly.
Yes, metal detector apps use the built-in magnetometer to detect magnetic field anomalies. However, the detection depth does not exceed 10–20 cm, and the device does not distinguish between metal types — it is more of an educational tool.
TYPE_MAGNETIC_FIELD returns data after calibration applied by the system. TYPE_MAGNETIC_FIELD_UNCALIBRATED provides raw values plus a bias estimate, allowing the developer to apply their own calibration.
Use SensorManager.getRotationMatrix with accelerometer and magnetometer data, then getOrientation — the first array element (azimuth) will be in radians. To display the compass, convert to degrees: Math.toDegrees(azimuth).
Summary
We will develop a mobile application turnkey
IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.
Read also