Search code examples
iosswiftaccelerometer

Accelerometer Data keeps on fluctuating


I keep my phone at same place in wooden floor.But the value keeps on changing every time I call the below code snippet

if let accelerometerData = motionManager.accelerometerData {                    
    let z:String="x:"+String(accelerometerData.acceleration.x)+" y:"+String(accelerometerData.acceleration.y)+"z"+String(accelerometerData.acceleration.z)
    print("\(z)")               
}

Here is the code that I used to initialize and kick start the accelerometer

//Accelerometer
motionManager = CMMotionManager()
motionManager.startAccelerometerUpdates()

Solution

  • Basically, When you use Accelerometer only, there is earth's gravity which influences z value of accelerometer. But DeviceMotion tries to compensate for this influence of gravity (i.e.tries to give more accurate value of acceleration.z) by performing some algorithm on raw data received from sensor.

    But this algorithm is not 100% accurate and that is why even if you put your device on flat surface like table, you get some non-zero value. This inaccuracy is very low in percentage but still you will get result which cannot be simply ignored.

    You may want to perform enough trials and get average values of them to compensate for it. Again, this average value will not solve the problem completely. For example, when device is moving(and not stable such when on table) you may get more inaccurate values.