Calibrate Compass and Motion Sensors for ARKit 2

Calibrating the compass and motion sensors in iOS 12 is essential for maximizing the world-tracking precision and stability of ARKit 2. Achieving optimal tracking requires a combination of iOS system-level calibration, correct session configuration in code, and programmatic handling of real-time sensor states. This guide outlines the essential steps developers and users must take to ensure the magnetometer, gyroscope, and accelerometer deliver accurate positional data for AR experiences.

1. Enable System-Level Compass Calibration

ARKit relies on Core Motion and the device's internal magnetometer to resolve geographic heading. Ensure the operating system is permitted to calibrate these sensors automatically:

  1. Open Settings on the iOS 12 device.
  2. Navigate to Privacy > Location Services.
  3. Scroll to the bottom and select System Services.
  4. Toggle Compass Calibration to On.

If this setting is disabled, ARKit cannot correct for magnetic declination or local magnetic anomalies, leading to drift when using heading-based alignment.

2. Configure World Alignment in ARKit

In ARKit 2, sensor tracking behavior is dictated by the ARConfiguration provided to the running ARSession. To bind tracking to the compass alongside the accelerometer and gyroscope, configure your session using gravity and heading alignment:

let configuration = ARWorldTrackingConfiguration()
configuration.worldAlignment = .gravityAndHeading
sceneView.session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
  • .gravityAndHeading: Uses both the internal IMU (Inertial Measurement Unit) and the magnetometer. Vector [0, 0, -1] points directly North, and [0, -1, 0] points downward matching gravity.
  • .gravity: If extreme magnetic interference is present, switch alignment to .gravity. This omits the compass and uses only motion sensors to align the Y-axis with gravity, reducing rotational jitter caused by distorted magnetic fields.

3. Prompt Physical Motion for Sensor Calibration

Accelerometers and gyroscopes require physical movement to determine their biases, while the compass needs multi-axis rotation to map local magnetic interference:

  • The Figure-Eight Motion: Instruct the user to gently wave the device in a figure-eight motion. This allows Core Motion to separate the Earth's geomagnetic field from soft-iron and hard-iron distortions produced by the environment or the phone's internal components.
  • Lateral Translation: ARKit's Visual-Inertial Odometry (VIO) requires parallax to initialize. Have the user pan the device slowly across textured surfaces to allow the camera and motion sensors to synchronize tracking frames.

4. Monitor and Respond to Tracking States

Implement the ARSessionObserver delegate methods to monitor when sensors require calibration or when tracking degrades:

func session(_ session: ARSession, cameraDidChangeTrackingState camera: ARCamera) {
    switch camera.trackingState {
    case .notAvailable:
        // Sensors are unavailable or initializing
        displayStatusMessage("Tracking unavailable.")
    case .limited(let reason):
        switch reason {
        case .initializing:
            displayStatusMessage("Move the device to calibrate sensors.")
        case .excessiveMotion:
            displayStatusMessage("Slow down device movement.")
        case .insufficientFeatures:
            displayStatusMessage("Point camera at a well-lit, textured surface.")
        case .relocalizing:
            displayStatusMessage("Calibrating position...")
        @unknown default:
            break
        }
    case .normal:
        hideStatusMessage()
    }
}

When tracking state is .limited(.initializing), prompt the user with on-screen visual cues to move the device smoothly, which completes sensor calibration.

5. Mitigate Environmental Sensor Interference

Motion and compass sensors are sensitive to physical and environmental noise:

  • Avoid Magnetic Fields: Keep the device away from external magnets, heavy metal furniture, or high-draw electronics, which cause sudden shifts in heading calculations.
  • Lighting Conditions: VIO combines optical feature tracking with accelerometer data. Low-light environments force the system to rely too heavily on the IMU, compounding sensor drift over time. Maintain adequate ambient lighting for optimal tracking performance.