Your phone uses gravity to autorotate

Introduction

Have you ever wondered how your phone knows when to switch from portrait to landscape mode? The answer lies in a tiny but powerful sensor called the accelerometer. This sensor detects the direction of gravity, allowing your phone to understand how it's being held and rotate the screen accordingly. Let's explore how this works!

How an accelerometer works

An accelerometer is a device that measures acceleration forces. These forces may be static, like gravity, or dynamic, caused by movement or vibrations. In your phone, the accelerometer senses the pull of gravity and any movements you make. By analyzing this data, your phone can determine its orientation in space.

Why is this proof of gravity?

The accelerometer in your phone always detects a force pulling towards the center of the Earth, no matter where you are. This force is what we call gravity. The fact that your phone can sense this downward pull and use it to orient itself is direct, everyday evidence that gravity is real and measurable.

The mathematics of an accelerometer

The accelerometer measures acceleration along three axes: X (side to side), Y (up and down), and Z (in and out of the screen). When your phone is at rest, the sensor detects gravity as a vector, usually about 9.8 m/s². The direction and magnitude of this vector tell the phone how it's oriented. The math involves calculating the angle of this gravity vector relative to the phone's axes using trigonometry.

// Example: Calculate tilt angle
let angle = Math.atan2(accelY, accelX) * 180 / Math.PI;
        

How autorotate uses it to rotate your phone screen

When you turn your phone, the direction of gravity relative to the phone's axes changes. The accelerometer detects this change, and the phone's software uses it to decide whether to display the screen in portrait or landscape. This is called autorotate. It makes reading, watching videos, and playing games more comfortable and intuitive.

Your accelerometer live

Try it yourself! If you're on a mobile device, the graphic below will show the direction of gravity as detected by your phone's accelerometer in real time.

Waiting for accelerometer data...
Note: Some devices and browsers (such as iPhones with Safari) may not allow access to the accelerometer without user permission, or at all. If the demo does not work, try using a different browser or device.
Also, due to how accelerometer axes are reported, the arrow may appear to point in the wrong direction. This demo now inverts the Y axis so the arrow points down (matching gravity), and left/right is now correctly oriented.

Shoutout to CountryEsq on tiktok for bravely pointing out to a bunch of flat earthers how accelerometers work. You can view the tiktok cmment that lead to this article here