Gravity Sensor in Mobile Apps: Isolate the Down Vector
Use the virtual gravity sensor on iOS and Android to know which way is down without juggling raw accelerometer noise.
The gravity sensor is a virtual sensor: there is no chip on the board called "gravity". The OS computes it by fusing the accelerometer with the gyroscope to isolate the gravity component of acceleration. The result is a clean three-axis vector that always points toward the ground, regardless of how the user is moving the device.
Key Takeaways
- Gravity is reported in m/s² and always sums to ~9.81 in magnitude.
- It is derived in software — there is no separate hardware sensor.
- It pairs perfectly with linear acceleration; together they give you a noise-free split of motion vs. orientation.
- iOS exposes it via `CMDeviceMotion.gravity`; Android via `Sensor.TYPE_GRAVITY`.
Gravity Sensor at a Glance
What It Is & How It Works
What it is. The gravity-only component of the accelerometer signal, computed by the OS using sensor fusion. Gives you the device-relative direction of "down" with much less noise than raw accelerometer data.
How it works. The OS subtracts user acceleration from the raw accelerometer reading using the gyroscope. The result is the gravity vector in the device frame — its X/Y/Z components describe how the device is tilted relative to the ground.
Units & signal. iOS exposes gravity in g (multiples of 9.81 m/s²) via `deviceMotion.gravity`. Android exposes it directly in m/s² via `TYPE_GRAVITY`.
What You Can Build With It
Spirit-level apps
Use the X/Y components of gravity to draw a bubble level.
Example: A picture-hanging level included in a home-improvement app.
Tilt-controlled UI
Pan parallax layers or steer a UI element based on tilt without jitter.
Example: Settings depth-of-field background that follows tilt on the home screen.
Orientation override
Detect "is the screen facing the table?" to mute audio or pause video.
Example: A meeting app that mutes when you flip the phone face-down.
AR up-axis correction
Anchor a virtual object so it stays vertical regardless of how the camera is tilted.
Example: A measuring-tape AR feature that keeps lines parallel to the ground.
Permissions & Setup
iOS reads gravity through Core Motion, so it requires a Motion & Fitness prompt. Android requires no runtime permission.
iOS · Info.plist
NSMotionUsageDescription
Android · AndroidManifest.xml
No special permission keys required.
Code Examples
Setup
- Expo: `npx expo install expo-sensors` (use `DeviceMotion`)
- iOS: subscribe to `CMMotionManager.deviceMotion` and read `gravity`
- Android: register for `Sensor.TYPE_GRAVITY` (API 9+)
import { useEffect, useState } from 'react';
import { DeviceMotion } from 'expo-sensors';
export function useGravity() {
const [gravity, setGravity] = useState({ x: 0, y: 0, z: 0 });
useEffect(() => {
DeviceMotion.setUpdateInterval(50);
const sub = DeviceMotion.addListener(({ accelerationIncludingGravity, acceleration }) => {
if (accelerationIncludingGravity && acceleration) {
setGravity({
x: accelerationIncludingGravity.x - acceleration.x,
y: accelerationIncludingGravity.y - acceleration.y,
z: accelerationIncludingGravity.z - acceleration.z,
});
}
});
return () => sub.remove();
}, []);
return gravity;
}Tip: With Newly, you describe the feature you want and the AI agent wires up the sensor, permissions, and UI for you. Try it free.
Best Practices
Prefer gravity over raw accelerometer for tilt
Gravity is already filtered. Using it for tilt UIs gives you stable angles without writing your own low-pass filter.
Use atan2 for angles
Compute pitch / roll as `atan2(y, z)` and `atan2(x, z)` rather than acos — atan2 handles all quadrants correctly.
Cap angle change per frame
For animation, clamp how fast the rendered angle can change so quick flicks don't cause UI jumps.
Pause off-screen
Stop subscribing when the relevant view leaves the screen — gravity updates aren't free, even though they feel cheap.
Common Pitfalls
Confusing gravity with linear acceleration
Gravity ≈ 9.81 m/s² always; linear acceleration ≈ 0 when the device is at rest.
Mitigation: Use gravity for "which way is down", linear acceleration for "is the device moving".
Gravity is unavailable on some Android devices
`TYPE_GRAVITY` is a virtual sensor and may be missing on very old or stripped-down ROMs.
Mitigation: Fall back to a low-pass filtered accelerometer when `getDefaultSensor` returns null.
Forgetting Earth ≠ 1 g everywhere
Magnitude is ~9.81 m/s² at sea level, ~9.78 at the equator, ~9.83 at the poles, less in altitude.
Mitigation: For physics simulations, normalise the vector instead of relying on its magnitude.
When To Use It (And When Not To)
Good fit
- Bubble-level / spirit-level UIs
- Detecting flat-down or flat-up device orientation
- Tilt-driven parallax and animation
- Stabilising AR or camera UIs against gravity
Look elsewhere if…
- Detecting motion or steps — use linear acceleration
- Background usage on iOS (no entitlement)
- Devices without sensor fusion (very old Androids)
- High-frequency physics — gravity updates only at sensor fusion rate
Frequently Asked Questions
Is the gravity sensor a real chip?
No. It's a virtual sensor: the OS computes it from the accelerometer and gyroscope. That's why it is missing on devices without sensor fusion.
How do I get tilt angles in degrees?
Compute roll = atan2(x, z) and pitch = atan2(y, z), then convert from radians to degrees by multiplying by 180/π.
Why does the gravity vector still wiggle slightly?
Even fused gravity has small high-frequency residuals. A 5-sample moving average gives you a perfectly stable level.
Can I use gravity to detect free-fall?
Yes — when the magnitude of the accelerometer (not gravity) drops near zero for >100 ms, the device is in free-fall. Combine with linear acceleration for higher confidence.
Build with the Gravity Sensor on Newly
Ship a gravity sensor-powered feature this week
Newly turns a description like “use the gravity sensor to spirit-level apps” into a real React Native app — permissions, native modules and UI included. Full source code is yours, and you can publish to the App Store and Google Play directly from the dashboard.
Want a deeper dive on the underlying APIs? See Expo Sensors, Apple Core Motion and Android sensor framework.
