So I have an accelerometer which I am wanting to use in an IMU.

When the device is tilted but stationary I want the x, y values to be 0, so effectively negate the effect of gravity along the x and y axes of the accelerometer.

I have found lots of conflicting advice on the internet regarding rotation matrices and was wondering if anyone here can provide some clarity.

I understand accelerometers can only measure pitch and roll from their x, y, z so how can I use these values to remove the gravity vector component on the x and y axes?

  • $\begingroup$ what are the x and y axes? $\endgroup$ Commented Mar 3, 2017 at 22:34
  • $\begingroup$ If the accelerometer is placed flat on a table, then x and y would be parallel to the table, and the z axis perpendicular. So, effectively I'm looking to remove the acceleration component in the x and y axes due to gravity when the device is not flat (some roll and pitch). So that when stationary I should get close to 0 values for any arbitrary orientation. $\endgroup$
    – Adam
    Commented Mar 3, 2017 at 22:45
  • 1
    $\begingroup$ This probably doesn't fit your needs, but if you just let the accelerometer go into free-fall, then it won't measure any components from gravity! $\endgroup$
    – tparker
    Commented Mar 3, 2017 at 23:24
  • 1
    $\begingroup$ dear tparker, why? $\endgroup$ Commented Apr 28, 2018 at 11:06

2 Answers 2


This is a hard thing to do. If you know the magnitude of $g$ accurately, you can look at the actual total acceleration observed by your accelerometer and subtract the "known" g. What you are left with is the difference vector. The problem is that the difference between two large vectors that point almost in the same direction is a small vector with a large error on it.

This is why it is preferable to have some independent information about the rotation of the sensor: if you have rotation sensing (not just linear acceleration measurement) you can integrate that to get the angular position; this helps improve the estimate of the orientation, and then it's easier to subtract the gravity vector.

For example this answer assumes you know the rotation (orientation of the sensor) after which things are simple. But when you don't, the problem is very ill posed unless the acceleration is large compared to $g$ (or at least "not small").

Details of the calculation can be found in this paper. If that doesn't answer your question, can you please be more specific in your question about the concept you are stuck on?


Here is the reason that your problem is not easy to solve. For simplicity I will do this in 2D - it should be easy to see how to extend it to 3D (but that doesn't make it better-behaved).

enter image description here

I show two situations where the $a_x$ sensor records the same acceleration, but the $a_y$ sensor records a slightly different value. When the sensor is rotated and stationary, and the values are perfectly known, then

$$a_x^2+a_y^2=g^2\\ a_y = g\sqrt{1-\left(\frac{a_x}{g}\right)^2}$$

Now in the second case (not rotated, and accelerating), the reading from the $a_y$ sensor will be exactly equal to $g$. In the first case, we can only estimate the rotation from the fact that it is not quite g: from trigonometry, we see that

$$\cos\theta = \frac{a_y}{g}$$

so we expect the measured horizontal component of acceleration to be

$$a_x = g \sin\theta = g \sin\cos^{-1}\frac{a_y}{g}$$

if we want to do error propagation, we can start by looking at the derivative of the $\cos^{-1}(x)$ function, which is $-\frac{1}{\sqrt{1-x^2}}$.

For small angles of rotation, when $a_y\approx g$, that error becomes exponentially bigger - in other words, even with an accurate accelerometer it's hard to estimate $\theta$ when the angle is small. And that means that you can't tell the difference between the two cases I drew very well - the uncertainty in $a_x$ will be very large. The problem becomes less important when the acceleration is very large - but for small (compared to $g$) accelerations this really won't work very well.

This is why it is necessary to have some independent indication of orientation / rotation if you want to use an accelerometer for an IMU.

  • $\begingroup$ Thanks. I am using this device as an IMU in a car and measuring accelerations to obtain velocity/position estimates. As such, the device will be mounted flat in the vehicle, and there will only be slight variations in roll and pitch and the car travels up/down hills or along banked corners/surfaces. As such it's not a complete orientation problem, or so I believe? But all I really want to do is correct for these small errors which affect the x and y axes. I believe I should be able to do so with a transformation matrix, ax, ay, az values, roll and pitch. This is what i'm stuck on. $\endgroup$
    – Adam
    Commented Mar 3, 2017 at 23:01

well as i experienced, you could compute rotation matrix from Azimuth(yaw), pitch & roll. computationally it is simple as mentioned by formulas for kinematics everywhere. then simple by multiplying 3*3 rotation matrix with 3*1 accelerometer data to convert the readings and compute new values corresponding to fixed global coordinate system. then you can easily subtract g from newly computed value of z axis.


Not the answer you're looking for? Browse other questions tagged or ask your own question.