# How to get the velocity with an acceleration sensor (ADXL345)?

I want to calculate the speed with the help of an acceleration sensor, such as a ADXL345.

I thought that it should be integrated, but according to the information that I have received, it seems that this method is not correct.

Do you have any suggestions?

It is possible, but, as stated in this answer and in the links below, the errors can make the results somewhat inaccurate, and as such it is not advisable to use solely an ADXL345 to calculate velocity - unless you just want a rough approximation.

From How to use the ADXL345 accelerometer sensor, there is this note upon the determination of velocity (emphasis is mine):

The velocity along each axis can be determined by integration of the acceleration values. The double-integration of the acceleration values can determine the displacement of the object along each axis. It should be noted that integration and double-integration of acceleration values can be done only using scientific computing tools like the SciPy library in Python scripts. Even then, we have to consider errors as there can be multiple sources of error like an inconsistent sampling of data, errors in sampled data, errors due to computing, etc. Generally, displacement or velocity values can be predicted to limited accuracy and to a limited range in a user program with the ADXL345 sensor.

From Tejas Turakhia's post on Is it possible to calculate velocity and displacement from ADXL345?

It is possible to measure velocity and displacement with an accelerometer. But I advise not to use accelerometer to calculate neither the velocity nor distance, due to integral being only approximate in the code.

Because the integral will be only approximation you’ll quickly get an error in your distance and speed. Especially in distance, since to calculate that, you have to use double integral.

First acceleration is change in speed in unit time:

a = (v - v0)/t

From this we can derive that:

v = v0 + at;

To calculate the velocity, you have to periodically take measurements from the accelerometer and multiply exactly by the time difference and add it to the current acceleration:

v = v+at

The more frequently you’ll take the samples, the less error you’ll have. However, because you can’t take infinite amount of measurements between two units of time, the velocity eventually will generate an error. Most likely it won’t even go back to initial state 0.

To calculate the distance, we just use the following equation:

s = vt

And again, periodically you must calculate the distance traveled and add to the previously calculated distance:

s = s+vt

The same problem applies as to calculating velocity when calculating the distance. Because you can’t have infinite measurements between, your integral will be an approximation and will generate an error.

In practice because you would use double integral, the distance will generate the error VERY quickly, mainly because, the speed will never reach 0. It will always be something close to 0, and your distance will just drift in either direction. The more samples you’ll take and the more precise integral you’ll have the less drift you’ll have.

To avoid the drift in distance and velocity, you might want to consider using GPS together with the accelerometer. To fuse those sensors you can either use simple complementary filter or kalman filter.

• One big point to mention is that you have to null out the effect of gravity. Even sitting stationary the sensor is going to measure 1g in some direction even though you are not actually accelerating. What's worse, if the accelerometer rotates then the direction that it measures the 1g in will change. So, you have to not only initially calibrate out gravity but also track your rotation as part of that nulling process. Apr 14 at 2:32

I agree with you, you integrate.

$$a = \frac{dv}{dt}$$

so

$$v = \int{a \cdot dt}$$

If you have a sequence of $$\N\$$ measured acceleration samples $$\a_0 \cdots a_{N-1}\$$, taken at intervals of $$\\Delta t\$$ seconds, and you know the velocity $$\v_0\$$ at the instant of the first one, then the discrete version of the above integral is:

\begin{aligned} v_N &= v_0 + \sum_{n=0}^{N-1}{a_n\Delta t} \\ \\ &= v_0 + \Delta t\sum_{n=0}^{N-1}{a_n} \\ \\ \end{aligned}

This can be computed in real time, as a running sum, as samples come in:

1. $$\v=v_0\$$ (probably zero)

2. Wait for sample $$\a\$$

3. $$\v = v + a\Delta t\$$

4. Go to 2

Of course, as others have pointed out, it's an approximation, and any variation of acceleration between samples is not accounted for. Therefore errors accumulate over time, and unless you have some way of "resetting" (for instance when you know that velocity has returned to zero), you can't rely on this approach for any great accuracy.

One thing you could do is just do a differential on the data for starters. The problem is the error will build up so filtering with a low pass filter could help.

The real way to do this is to intagrate it with a kalman filter to do state estimation, which is a lot of work.

• You mean to do an integral on the data, yes? Apr 14 at 3:05