Flight Controller / Sensors

FC9 — Compass Implementation

Magnetic Sensor Mounting Design and Calibration
Contents

    In the previous article, we explained the error models for gyroscopes and accelerometers and their roles in the EKF. In this article, which is FC9, we will cover the compass (magnetic sensor). The compass is the only sensor that provides an absolute reference for the yaw direction, but it is also the sensor most susceptible to electromagnetic interference, requiring the most care in both design and operation.

    Why a compass is necessary for an FC

    Gyroscopes and accelerometers alone cannot provide a long-term absolute reference for yaw (azimuth). Yaw estimation via gyroscope integration suffers from significant drift, resulting in errors of several tens of degrees within minutes. While an accelerometer can determine the direction of gravity (downward), it cannot determine the compass direction (north), so it serves as an absolute reference for roll and pitch, but not for yaw.

    The compass provides an absolute reference for the yaw direction by measuring the geomagnetic vector. Geomagnetism is a stable, global magnetic field, and if measured correctly, it can compensate for long-term yaw drift.

    However, the intensity of the geomagnetic field is very weak, at approximately 25–65 µT (microtesla), and it is easily overwhelmed by the magnetic fields (tens to hundreds of µT) generated by the drone's battery, motors, and ESCs. This is the fundamental difficulty in compass implementation.

    Additionally, when combined with GPS/GNSS, the ArduPilot EKF can also estimate yaw from the GNSS movement direction (GPS CoG, Course over Ground). This is called GPS Yaw, which can reduce reliance on the compass. However, since GPS Yaw is only valid while moving and cannot be used when stationary, complete compass-less operation is currently limited to certain modes.

    Operating principles of magnetic sensors

    Compass ICs measure magnetic fields using the Hall effect or the AMR (Anisotropic Magnetoresistive) effect.

    Hall effect sensors utilize the phenomenon (Hall effect) where a voltage difference is created by the Lorentz force when a magnetic field is perpendicular to a thin conductor carrying current. The QMC5883L uses this method and is widely used due to its low cost.

    AMR sensors utilize the property that the electrical resistance of a nickel-iron alloy thin film changes depending on the direction of the magnetic field. The IST8310 and RM3100 are representative examples; they have higher sensitivity and accuracy than Hall effect sensors and possess characteristics of lower noise.

    We will summarize a comparison of the main compass ICs used in FCs.

    The IST8310 features I2C connection, operating voltages of 1.8V/3.3V, a measurement range of ±1600 µT, and a resolution of 0.3 µT, and it is adopted in many Pixhawk series. The ArduPilot driver is mature and highly reliable.

    The QMC5883L is widely used as a compatible replacement for the HMC5883L, but its accuracy is inferior to the IST8310. It is used in low-cost aircraft and as a compass built into GPS modules.

    The RM3100 is an AMR sensor manufactured by PNI Sensor, and its noise characteristics are excellent. It supports both SPI and I2C, has a noise density less than 1/10th that of the IST8310, and is suitable for industrial applications requiring high precision. The ArduPilot RM3100 driver is already supported.

    Interference sources for the compass and the magnitude of their impact

    We will organize the magnetic field sources that interfere with the compass in order of the magnitude of their impact.

    Motors and ESCs are the largest sources of interference. The current flowing through the motor coils (tens of amperes) creates a powerful magnetic field. Even at a point 10 cm away from the motor, a magnetic field of several hundred µT can be measured, completely overwhelming the geomagnetic field (25–65 µT).

    Batteries and power wiring are paths where large currents flow, and the magnetic field created by the current loop is a problem. If the battery positive and negative cables are separated, it creates a large loop area, radiating a strong magnetic field. Using twisted pairs can significantly reduce the radiated magnetic field.

    DC-DC converters on the FC board radiate magnetic fields from the switching current loop. When mounting a compass on an FC, ensure a minimum distance from the DC-DC converter.

    Ferrous structural materials cause fixed magnetic field interference (hard iron) that distorts the geomagnetic field. Aluminum and carbon fiber are non-magnetic and pose no problem, but steel screws and iron frame parts require caution.

    Since the magnets in the electric motors themselves act as a fixed magnetic field whose relative position does not change with the aircraft's attitude, they can be corrected using airframe-level hard-iron calibration.

    Compass Mounting Position Design

    The most effective interference countermeasure is physical distance. Because magnetic fields attenuate by the cube of the distance, doubling the distance from the interference source reduces the intensity to 1/8.

    An external compass in a GNSS module is the most common solution. u-blox M9N or F9P modules often have an IST8310 or QMC5883L built-in, allowing the GNSS antenna and compass to be housed in the same module and mounted on a mast at the top of the aircraft. Placing it at least 10cm away from the FC unit and as far as possible from the motors provides a much better environment than mounting it on the FC board or near the arms.

    The height of the mast is determined by both the horizontal and vertical distance from the motors. For a quadcopter, mount the mast closer to the center of the aircraft than the extension of the arms, ensuring a minimum distance of 15–20cm from the motors.

    An internal compass configuration, where the compass IC is mounted on the FC board, may be unavoidable in small aircraft. In this case, power noise countermeasures on the FC (independent LDOs, enhanced decoupling) and layout considerations are necessary. Keep it away from DC-DC switching loops, ensure a solid GND plane, and maintain distance from current-carrying traces.

    ArduPilot supports multiple compasses by default. Typically, two are used: an external compass built into the GPS module (COMPASS1) and the internal FC compass (COMPASS2), with the EKF automatically selecting the healthier one. Since the internal compass is prone to interference, it is common to use it with COMPASS_USE2=1 but at a lower priority, relying primarily on the external one.

    Principles of Hard-Iron and Soft-Iron Correction

    Compass calibration requires two types of correction.

    Hard-iron correction compensates for fixed magnetic fields on the aircraft. Constant offsets created by magnetic sources that move with the airframe—such as motor magnets, steel screws, and batteries—are estimated as a vector (bx, by, bz) and subtracted from the compass output. If you plot the compass readings while rotating the aircraft in all attitudes, the points will form a sphere; without hard-iron interference, the sphere is centered at the origin. Hard-iron calibration estimates this shift.

    Soft-iron correction compensates for the effect of soft magnetic materials (aluminum, carbon fiber, etc.) bending the Earth's magnetic field. Soft-iron interference distorts the spherical point cloud into an ellipsoid. This distortion is corrected using a 3x3 matrix. Soft-iron correction is more computationally complex than hard-iron correction and requires a sufficient number of measurement points (usually several hundred or more).

    ArduPilot's compass calibration (Mission Planner's Onboard Calibration) collects measurement points while rotating the aircraft through all attitudes and estimates the hard-iron and soft-iron parameters using the least-squares method. Coverage (the uniformity of the distribution of measurement points) is directly linked to calibration accuracy, and ArduPilot displays convergence status via a Completion Percentage.

    Dynamic Interference from Motor Current and Countermeasures

    Because motor current changes with throttle, dynamic interference occurs that cannot be corrected by hard-iron calibration (static calibration on the ground). It is not uncommon for compass output to change by several tens of µT between hovering and full throttle.

    ArduPilot's CompassMot feature corrects this dynamic interference. It measures the relationship between compass output and throttle value while increasing the throttle, allowing for real-time correction of the compass output based on the throttle during flight. This is an essential procedure for aircraft with high-current motors mounted nearby.

    The setup can be performed automatically via Mission Planner's "Compassmot Calibration." With the aircraft secured, the throttle is increased in stages to measure the compass error at each level. If the CompassMot correction result is 30% or less, it is acceptable for practical use; if it exceeds 50%, it is recommended to reconsider the compass mounting position.

    For high-precision RTK-capable aircraft, configurations that completely replace the compass with GPS Yaw (a method that determines yaw from the baseline vector between two GNSS antennas) are becoming more common. By using the u-blox F9P Moving Base mode and placing antennas with a baseline of 30cm or more on the front and rear of the aircraft, the dynamic interference problem of the compass is fundamentally solved.

    Compass Definition in hwdef.dat

    Compass settings in ArduPilot are defined in hwdef.dat. Here is an example of connecting an IST8310 to I2C1.

    # I2C1バス定義
    PB6 I2C1_SCL I2C1
    PB7 I2C1_SDA I2C1
    
    # コンパスデバイス定義
    COMPASS IST8310 I2C:0:0x0E false ROTATION_PITCH_180
    

    copy**

    ROTATION is set according to the mounting orientation on the board. Compasses built into GNSS modules are often automatically detected via external I2C ports or UART, so explicit definition in hwdef.dat may not be necessary.

    Set the priority of multiple compasses using the COMPASS_PRIORITY parameter. It is recommended to set the external GPS module compass as the highest priority and the internal FC compass as a backup.

    Summary

    The compass is essential as an absolute yaw reference, but it is the sensor most vulnerable to electromagnetic interference. Ensuring physical distance (mounting the GPS module on an external mast) is the most effective countermeasure. Practical accuracy is achieved by combining hard-iron and soft-iron calibration with dynamic compensation via CompassMot. For high-precision aircraft, replacing the compass with GPS Yaw is also a strong option. In the next installment, FC10, we will explain barometer design: selecting pressure sensors and countermeasures against atmospheric pressure disturbances.

    Explore Our Technologies

    KURAGE GCS

    Browser-Based Ground Control System

    KURAGE Vision

    AI Vision & Security Platform

    KURAGE FC

    Custom STM32H743 Flight Controller

    KURAGE Mission Computer

    Autonomy Engine for UAVs and Robotics