IMUs, magnetometers, and sensor quality
Bias, drift, calibration, and the 6-DOF fusion that turns raw chips into a pose estimate. The sensors that complement encoders for everything that flies, walks, or balances.
An IMU (Inertial Measurement Unit) measures linear acceleration, angular velocity, and (often) magnetic field. Combined, you get attitude. Without an IMU, no drone flies, no quadruped balances, no humanoid walks. They cost $3–$10000 depending on grade; the difference is bias, drift, and noise. Knowing what you're paying for — and how to compensate — is the working knowledge.
What's inside an IMU
Three sensor types in modern packages:
- Accelerometer: 3-axis linear acceleration in m/s². At rest, reads gravity (~9.81 m/s² along the down axis).
- Gyroscope: 3-axis angular velocity in rad/s.
- Magnetometer: 3-axis magnetic field vector in microtesla. Earth's field is ~25–65 µT; gives absolute heading reference.
"6-DOF IMU" = accel + gyro. "9-DOF IMU" = accel + gyro + magnetometer. "9-axis IMU" is the same as 9-DOF (sloppy industry naming).
What you can compute from raw data
- Roll, pitch: from accelerometer (when stationary, gravity points "down").
- Heading (yaw): from magnetometer (Earth's field gives an absolute reference).
- Angular rate: directly from gyro.
- Linear motion: from accelerometer minus gravity (after orientation is known).
The catch: each measurement has noise + bias + drift. Naive integration of gyro angular velocity drifts ~1°/sec; integrating accelerometer twice drifts meters per minute. Sensor fusion is required.
Sensor grades
| Grade | Gyro bias drift | Cost | Examples |
|---|---|---|---|
| Consumer / phone | 5–20 °/h | $3–10 | MPU6050, ICM-42688, BNO055 |
| Industrial | 0.5–5 °/h | $50–500 | VectorNav VN-100, ADIS16470 |
| Tactical | 0.01–0.5 °/h | $1k–10k | VectorNav VN-200, Honeywell HG1700 |
| Navigation-grade | < 0.01 °/h | $10k+ | Fiber-optic gyros |
For hobby robotics: consumer-grade (BNO055, ICM-42688) is fine. For drones with VIO: industrial. For autonomous trucks doing dead-reckoning through tunnels: tactical.
The calibration ritual
Every IMU needs calibration before use:
Gyro bias
Place the IMU stationary; average raw gyro readings for 5–10 seconds. Subtract that from every future reading. Repeat after temperature changes (the bias drifts with temperature).
Accelerometer scaling
Place the IMU in 6 orientations (each axis up + down); each reading should be ±9.81 m/s². Compute scale + offset corrections.
Magnetometer hard/soft iron
Wave the IMU around in 3D for 30 seconds. The magnetic field readings should form a sphere centered at the origin. Hard-iron offset → sphere center is shifted. Soft-iron distortion → sphere is an ellipsoid. Compensate with a 3×3 matrix.
Many IMU chips include built-in calibration routines (the BNO055 does it autonomously). Cheaper chips (MPU6050) need software-side calibration.
Sensor fusion: complementary filter
The simplest fusion: combine gyro (fast, drifty) and accelerometer (slow, drift-free for tilt) via a complementary filter:
angle += gyro_rate * dt # integrate gyro
angle = 0.98 * angle + 0.02 * angle_from_accel # bias toward accel
The 0.98/0.02 split is a high-pass on gyro + low-pass on accel — high-frequency components from gyro, low-frequency drift correction from accel. Tune the constant for your IMU.
For 2D (roll/pitch only) this works great. For full 3D + yaw, you need either a magnetometer (and Madgwick / Mahony filter) or an EKF.
Madgwick / Mahony filters
Both are gradient-descent algorithms operating on quaternions. They take accel + gyro (+ optional mag) and output an attitude quaternion. Run at the sensor's update rate (200–1000 Hz).
Madgwick is the standard in 2026 firmware (PX4 uses a variant). Open-source implementations in C exist; ~200 lines for the embedded version.
The Kalman / EKF route
For tighter accuracy, use an EKF that takes IMU as a process model (predict via gyro + accel) and corrects with absolute references (GPS, vision, magnetometer). Standard in autonomous vehicles, drones, AR/VR.
The same EKF math from the SLAM track applies. robot_localization in ROS 2 wraps it for typical use.
Common gotchas
- Gravity vector mismatch: at high g-loads (drone in aggressive turn), accelerometer reads centripetal + gravity; tilt estimate is wrong. Madgwick handles this; naive complementary filter doesn't.
- Magnetic interference: motors, ferrous structures, wiring distort the mag field. Calibrate in the actual mounting position, with motors running.
- Vibration: motor vibration adds noise to accel readings. Mount on rubber dampers; filter at sample time.
- Sample-rate jitter: variable sample interval messes up integration. Use hardware-triggered sampling if possible; if not, timestamp every reading.
- Driver units: some return raw counts, some m/s², some g. Read the datasheet; confirm units; never trust default-config readings.
The 2026 hobby default
For a robot project where you need attitude:
- BNO085 ($25): 9-DOF, on-chip Madgwick fusion, returns quaternions over I²C / SPI. Plug-and-play.
- ICM-42688 ($8): 6-DOF, very low noise; do your own fusion. Used in modern drones (PX4 supports it).
- MPU6050 ($3): legacy, still works; but the chip is 15 years old and its successors are better.
For 90% of hobby work, BNO085 is the right starting point. Spend the $25 to skip the calibration headache.
Exercise
Wire a BNO085 to an ESP32 over I²C. Read quaternion at 100 Hz. Stream to a desktop visualization. Tilt the IMU; watch the on-screen orientation match. Now spin it fast around the yaw axis; observe the magnetometer locking heading without integrator drift. The full sensor-fusion picture in 30 minutes.
Next
Power, batteries, brownouts — the layer that keeps the rest of the electronics alive.
Comments
Sign in to post a comment.