Magnetometer in smartphones — what it is, types and working principle

Author: IT Sectr Published: 2026-03-22 Reading time: 10 min

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 — a Hall effect sensor that measures magnetic field along three axes with a range of ±1300 µT.
  • Digital compass based on a magnetometer requires calibration — without it, error reaches 30–40 degrees.
  • Android API uses Sensor.TYPE_MAGNETIC_FIELD to access data in microteslas (µT).
  • Sensor Fusion combines magnetometer with gyroscope and accelerometer for accurate orientation.
  • Hard Iron and Soft Iron — types of magnetic distortions caused by the phone’s metal casing and speakers.

What is a magnetometer in a smartphone

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.

Types of magnetometers in mobile devices

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).

ParameterValue
TechnologyAMR / Hall Effect
Range±1200 — ±1300 µT
Power consumption0.5–2 mA
Size1.5×1.5×0.7 mm
Sensitivity0.15 µT/LSB

Why the magnetometer is important for a smartphone

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.

How the Hall effect sensor works

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).

AMR sensor structure

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.

Z-axis measurement

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.

How is a magnetometer different from a gyroscope

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.

Sensor Fusion with magnetometer

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.

kotlin
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

Magnetic distortions: Hard Iron and Soft Iron

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.

Calibration algorithm

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.

kotlin
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
    )
}

Software distortion correction

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.

Interference mitigation recommendations

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.

Programmatic access to the magnetometer in Android

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.

Registration and processing

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.

kotlin
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)
    }
}

Applications of the magnetometer in mobile apps

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.

  • Navigation — determining azimuth of movement in Google Maps, Yandex.Maps, MAPS.ME. Compass accuracy directly affects route quality.
  • Metal detectors — apps for finding metal objects use the magnetometer to detect magnetic field anomalies.
  • Geophysics and education — measuring magnetic field for educational projects. The sensor allows studying the Earth’s magnetic field in schools.
  • VR and 360 video — head orientation in VR headsets is corrected by the magnetometer to compensate for gyroscope drift.

Magnetometer in automotive navigation

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%.

Magnetometer in navigation apps

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.

Future of magnetometers

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

How to calibrate the magnetometer on a phone?

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.

Why does the compass on my phone show the wrong direction?

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.

Can I use my phone as a metal detector?

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.

What is the difference between TYPE_MAGNETIC_FIELD and TYPE_MAGNETIC_FIELD_UNCALIBRATED?

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.

How to get azimuth from magnetometer data?

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

  • Magnetometer — a three-axis magnetic field sensor that works as a digital compass in mobile devices.
  • AMR technology based on the Hall effect allows measuring the Earth’s field with 0.15 µT/LSB accuracy.
  • Hard Iron and Soft Iron — magnetic distortions caused by phone components, requiring figure-8 calibration.
  • Android API provides TYPE_MAGNETIC_FIELD and TYPE_MAGNETIC_FIELD_UNCALIBRATED with data in µT.
  • Sensor Fusion combines magnetometer with gyroscope and accelerometer for accurate (1–3 degree) orientation.
  • Applications include navigation, AR apps, metal detectors, and gyroscope drift correction in VR.
  • Modern sensors consume from 0.35 mA at 200 Hz with a size of 1.5×1.5 mm.

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.

Discuss the project

Read also