Core Motion Integration: Accelerometer & Gyroscope in iOS Apps
Anomalous orientation data and unrecognized gestures are a common issue when working with iOS sensors. We are an iOS development team with over 5 years of experience integrating Core Motion and 20+ successful projects. We help you obtain accurate data from the accelerometer, gyroscope, and barometer.
Core Motion is the single entry point to iPhone and iPad inertial sensors: accelerometer, gyroscope, magnetometer, barometer, and Motion Coprocessor. It provides direct access to raw data at up to 100 Hz, plus processed Device Motion data with gravity correction and gyroscope drift filtering. Most tasks can be solved at the Device Motion level—no need to implement Madgwick or Mahony filters manually.
Over 5 years, we have completed more than 20 projects integrating Core Motion—from simple levels to complex fitness trackers. Each integration starts with requirement analysis, reference frame selection, and polling frequency setup. We test on real devices and optimize algorithms for minimal battery drain. Contact us to discuss your scenario.
How to Integrate Core Motion for Accelerometer and Gyroscope?
CMMotionManager: One Instance Per App
This is not a recommendation—it’s a requirement. Multiple instances of CMMotionManager in different parts of the app lead to update conflicts and unpredictable behavior. The standard solution is a singleton via a DI container or static property:
final class MotionManager { static let shared = MotionManager() let motion = CMMotionManager() private init() {} } Why Device Motion Over Raw Accelerometer?
Device Motion (startDeviceMotionUpdates) provides three useful components directly:
-
userAcceleration— linear acceleration without gravity. -
attitude— orientation in space (pitch, roll, yaw). -
rotationRate— angular velocity with hardware drift correction.
Raw Accelerometer (startAccelerometerUpdates) returns total acceleration including gravity (≈9.81 m/s² on the Z axis when stationary). To extract movement, an additional filter is needed.
Comparison of accuracy:
| Parameter | Device Motion | Raw Accelerometer |
|---|---|---|
| Orientation accuracy | <1° static | Requires filtering, error up to 5° |
| Implementation effort | Low (ready-made data) | High (custom filter) |
| Battery consumption | Medium (coprocessor) | Medium (CPU) |
Device Motion processes data on the coprocessor—10x better orientation accuracy than processing raw data manually.
let manager = MotionManager.shared.motion manager.deviceMotionUpdateInterval = 1.0 / 60.0 // 60 Hz manager.startDeviceMotionUpdates( using: .xMagneticNorthZVertical, to: .main ) { [weak self] motion, error in guard let motion = motion else { return } let pitch = motion.attitude.pitch // forward/backward tilt (radians) let roll = motion.attitude.roll // left/right tilt let yaw = motion.attitude.yaw // rotation around vertical axis let accel = motion.userAcceleration // linear acceleration without gravity let rotation = motion.rotationRate // angular velocity rad/s } CMAttitudeReferenceFrame.xMagneticNorthZVertical — orientation relative to magnetic north, Z upward. For gaming and AR apps, it's the right choice. For simple gesture detection, use xArbitraryZVertical (no magnetometer, lower power).
How to Detect Gestures with Core Motion?
The shake gesture is built into UIKit but limited. For custom gestures—analyze userAcceleration. Shake pattern: acceleration peaks > 2.5g with alternating signs on one axis within < 500 ms.
var accelerationHistory: [Double] = [] // In the device motion handler: let magnitude = sqrt( pow(motion.userAcceleration.x, 2) + pow(motion.userAcceleration.y, 2) + pow(motion.userAcceleration.z, 2) ) accelerationHistory.append(magnitude) if accelerationHistory.count > 30 { accelerationHistory.removeFirst() } let peakCount = accelerationHistory.filter { $0 > 2.5 }.count if peakCount >= 3 { triggerShakeAction() accelerationHistory.removeAll() } Determining Orientation and Tilt
For level apps, AR markup, camera control: attitude.pitch and attitude.roll are accurate enough (error < 1° in stationary conditions).
Pedometry Without CMPedometer
On devices without CMPedometer support (iPod Touch without Motion Coprocessor)—step detection from accelerometer. Algorithm: low-pass filter on userAcceleration.y, detect peaks > 0.2g with minimum 300 ms interval.
How to Work with CMAltimeter: Barometric Altitude?
CMAltimeter is a separate class for the barometer:
let altimeter = CMAltimeter() guard CMAltimeter.isRelativeAltitudeAvailable() else { return } altimeter.startRelativeAltitudeUpdates(to: .main) { data, error in guard let data = data else { return } let relativeAltitude = data.relativeAltitude.doubleValue // meters from start let pressure = data.pressure.doubleValue // kPa } relativeAltitude is the change in altitude from the start of updates, not absolute sea level. Accuracy: ±0.1 m in stable weather. Used for floor counting in CMPedometer.floorsAscended and for fitness apps (altitude gain/loss on a route).
Case Study: Activity Tracking in a Fitness App
In one project, we needed accurate step counting, floor detection, and arm swing gesture detection. We integrated Device Motion at 50 Hz, used userAcceleration for pedometry and attitude for arm raise detection. Optimization reduced battery drain by 30% compared to raw data processing. The result—the app passed App Store review and received high user ratings.
How to Optimize Battery Consumption When Working with Sensors?
| Scenario | Frequency | Consumption |
|---|---|---|
| Gesture detection | 10–25 Hz | Low |
| Pedometer | 25–50 Hz | Medium |
| Game control | 60 Hz | Medium |
| AR/signal processing | 100 Hz | High |
Do not keep sensors active unnecessarily: call stopDeviceMotionUpdates() in viewDidDisappear or when going to the background (if background data is not needed).
What's Included in Core Motion Integration
- Requirement analysis for the scenario (gestures, orientation, steps, altitude).
- CMMotionManager setup, reference frame selection.
- Implementation of gesture or activity detection with on-device testing.
- Polling frequency optimization for battery saving.
- Integration documentation and post-deployment support.
Timelines
Basic sensor integration (accelerometer, gyroscope, attitude) with a specific applied scenario—3-7 working days. Complex signal processing algorithms (activity detection, gesture recognition, pedometry)—2-4 weeks.
Assess your project—contact us for a consultation on your Core Motion use case. Order integration, and your app will get accurate sensor data.







