You turn the phone and the screen straightens itself. You run and a watch counts your steps. You open a map and the arrow points where you are looking. None of that is known by a single component: it is achieved by a system that combines three cheap sensors, each with its own error, and a body of mathematics that treats doubt as just another piece of data. The result is an estimate of orientation and motion that, in your pocket, works almost every time. Almost.
A mass at the end of a microscopic spring
The accelerometer in a phone is a MEMS device: a silicon structure a few square millimetres in size, built with the same lithographic techniques as a chip. Inside, a proof mass hangs from springs that are also made of silicon. When the phone accelerates, the mass shifts relative to the substrate; interlocking combs of facing electrodes detect that movement as a change in electrical capacitance. The variation is tiny — on the order of attofarads — so the circuit turns it into a bitstream proportional to acceleration using a sigma-delta modulator.
Consumer parts usually use electrostatic feedback: instead of letting the mass move freely, the circuit applies a force to bring it back to the centre and measures the current required. That improves linearity and bandwidth. The typical range goes from ±2 g to ±16 g, with noise densities of micrograms per root hertz. And here the first misunderstanding appears: the accelerometer cannot tell gravity from real acceleration. Einstein’s equivalence principle forbids it. At rest on a table it reads 1 g upwards; in free fall, zero. To the software, gravity is an extremely useful reference, but also a source of confusion when the user runs, brakes or takes a lift.
The gyroscope measures a force that does not exist
A MEMS gyroscope does not spin like a top. It contains a mass vibrating at tens of kilohertz, held by a feedback loop that keeps it in resonance. When the device rotates, the Coriolis force — the same effect that bends winds across the atmosphere — pushes that mass perpendicular to the direction of vibration. Electrodes measure the deflection, and angular velocity comes out of it. The trick is elegantly physical: it does not detect the rotation, it detects its inertial consequence on a mass that is already moving.
The problem is that the sensor also picks up things that are not rotation: manufacturing mismatch (quadrature error), thermal gradients, mechanical stress and noise. All of that becomes a bias that changes with temperature and over time. To obtain an angle you have to integrate angular velocity, and the integral of a small bias over minutes turns into a large error. A consumer gyroscope can drift from one to several degrees per minute; in virtual reality, where latency is measured in milliseconds, that drift shows. The phone compensates with factory calibration and thermal models, but it cannot remove the problem: the gyroscope is smooth and precise in the short term, and blind in the long term.
The compass that gets it wrong on your desk
The magnetometer measures Earth’s magnetic field, which in Europe is around 50 microteslas. That is a feeble field compared with the magnet in a speaker, a case clasp or a fan motor. Hall or magnetoresistive (AMR) sensors detect that component, but they suffer from two classic distortions. Hard iron is a constant field added to the terrestrial one, coming from magnetised parts of the phone itself; it is corrected by subtracting a vector. Soft iron deforms and rotates the field in an orientation-dependent way, and requires a correction matrix. That is why compass apps ask you to wave the phone in a figure eight: they are collecting samples to estimate those parameters.
Even so, the magnetometer is only reliable outdoors and away from metal structures. Inside a car, near a computer or in a building with steel beams, the reading can be off by tens of degrees. Its value lies in providing an absolute heading reference: without it, orientation accumulates without an anchor and the error grows relentlessly. With it, the system can correct heading against magnetic north and then apply declination to reach true north.
Three incomplete witnesses and one probabilistic judge
None of the three sensors is enough. The accelerometer gives tilt (roll and pitch) from gravity, but any linear acceleration contaminates it. The gyroscope gives a smooth, fast rotation, but it drifts. The magnetometer gives an absolute heading, but it is easily distorted. Sensor fusion is the process that combines those signals to obtain a better estimate than any of them alone.
The simplest version is the complementary filter: the gyroscope is integrated for fast motion and corrected with the accelerometer and magnetometer over the long term. It is cheap and works well in many devices, but it does not reason about uncertainty. The Kalman filter does. It keeps a state vector — usually an orientation quaternion and the gyroscope bias — together with a covariance matrix that represents how much it distrusts each variable. In the prediction step it integrates angular velocity and lets uncertainty grow; in the correction step it compares the expected orientation with the one suggested by gravity and the magnetic field, and adjusts the state by weighting each source according to its reliability.
Because the relationship between the state and the measurements is nonlinear, an extended Kalman filter (EKF) is used, or variants such as Madgwick and Mahony, designed for microcontrollers with few resources. The quaternion avoids the gimbal lock of Euler angles and propagates with four multiplications instead of trigonometric matrices. The filter can also detect when a measurement is suspicious: if the magnitude of the magnetic field departs from the expected value, it reduces its weight or discards it. That gate is the difference between a compass that goes crazy next to a magnet and one that simply ignores the bad data.
From raw data to the gesture the app understands
All this computation does not happen on the main processor. Phones include a sensor hub: a very low-power core, often a DSP, that samples the sensors, runs the filters and wakes the system only when needed. Android exposes those signals through the sensor HAL and the SensorManager API, with sampling modes from tens to hundreds of hertz and with batching: the hub accumulates samples and delivers them in blocks so the CPU does not wake for every reading. Sensors such as the pedometer or motion detector are wake-up sensors: they can interrupt the processor from a low-power state.
Those signals build features that look like magic. The pedometer applies a high-pass filter to remove gravity, detects periodic peaks and estimates cadence; distance is computed with a stride model that depends on height and pace. Pedestrian dead reckoning, or PDR, goes further: it integrates acceleration twice to estimate displacement, corrects with the magnetometer heading and anchors the result on the map. Double integration is treacherous: a constant acceleration error produces a quadratic position error. That is why serious systems combine the IMU with GPS, with the car’s wheels or with beacons, and use each source to correct the others.
In a car, that fusion is what allows position to be estimated in a tunnel with no coverage: wheel speed and the gyroscope hold heading and progress for a few hundred metres. In virtual reality glasses, the same kind of filter predicts the pose a few milliseconds into the future to compensate for panel latency. In a drone, the combination of accelerometer, gyroscope, barometer and sometimes vision keeps position without being pushed off course by the wind. The idea is always the same: no sensor is the truth; the system builds an operational truth from several partial witnesses.
When the system gets it wrong
Fusion does not eliminate errors, it distributes them. A phone resting on a magnetic car mount can believe it is turning while standing still. A watch worn loosely mistakes arm impacts for steps. A phone heating up on the dashboard sees the gyroscope bias change faster than its thermal model predicts. And a calibration performed in an iron-rich environment is worthless in another. Engineers know this and add anomaly detectors, periodic recalibrations and confidence margins; they also accept that the estimate should degrade gracefully instead of failing all at once.
The next time the screen turns by itself, remember what has just happened under the glass: a silicon mass trembling, a comb of electrodes measuring minute capacitances, three sensors arguing and a filter weighing their versions. Your phone does not know where it is or where it is looking. It estimates it, with an uncertainty you almost never see and which, when it gets things right, looks a lot like knowledge.






