Sensor – ADXL345 – 3-Assige versnellingsmeter


ADXL345 module met achterkant


ADXL345 is een klein, laag vermogen, 3-assige versnellingsmeter met een hoge resolutie (13-bit) meting tot ± 16 g. Digitale uitgang van gegevens is opgemaakt als 16-bit. Uit te lezen via SPI of I2C.
Voor het meten van: statische (tilt) en dynamische (bewegingen/schokken) versnelling en detectie van: tap, dubbel tap, vrije val en inactief, het wordt in veel apparaten toegepast, waaronder in mobiele telefoons en tablets (voor schermrotatie e.d.)

tilting xy en zwaartekracht


Accelerometers are devices that are capable of measuring the acceleration they experience relative to free-fall,  the same acceleration living beings feel. As a consequence, accelerometers are incapable of measuring the acceleration of gravity, but can be used to measure the upwards acceleration that counters gravity when at rest. This acceleration is measured as 1g on the z-axis, when both pitch and roll angles are zero, but when the sensor is tilted either the x-axis or the y-axis experiences a component of the upward acceleration, whose magnitude depends on the tilt angle.


Obtaining the pitch and roll angles is then a matter of being able to read the accelerometer, convert these readings to the g unit (1g = 9.8 m/s²), and apply the corresponding equations. The process of obtaining and converting the accelerometer readings depends on the accelerometer you are using, in my case, the ADXL345 in its basic configuration, provides 10-bit resolution for ±2g, but has several other ranges (±2g, ±4g±8g±16g)  and resolutions (from 10 to 13 bits depending on the range) . Generalizing, the formula used to calculate the acceleration from the accelerometer readings is:

G_{Accel} = Raw_{Accel} \cdot \dfrac{Range}{2^{Resolution - 1}}

Once we have the correct acceleration components, we can proceed to calculate the different angles using the following equations:

pitch = \arctan{\left(\dfrac{G_y}{\sqrt{G_{x}^2 + G_{z}^2}}\right)}     roll =\arctan{\left( \dfrac{-G_x}{ G_{z}}\right)}

For more information about where these equations come from, you can read the documentation I include at the end of this post. As you can see, the denominator of the pitch equation is defined to be always positive, so the equation itself only provides [-90, 90] range, which is exactly what is expected for the pitch angle. In contrast, the roll equation provides [-180, 180] range. It is important to take into account that when the pitch angle is 90º, the surge axis (roll) is directly aligned with the gravity vector, thus we cannot measure the roll angle anymore, this is what is called Gimbal Lock.

Also, be aware that the roll equation is undefined when both G_x and G_z are equal to zero, and that for each possible value of the calculation done inside the arctan function there are two valid solutions, not only on the roll but also on the pitch equation. These problems can be easily solved in code by using the function atan2, which eliminates the angle calculation ambiguity by taking into account the quadrant.


At this point we already have a fully functional pitch & roll estimation system, but if we experiment with it we will discover that the readings fluctuate quite a bit and this may be very annoying for some applications. Removing these short-term fluctuations can be achieved by means of what is called a Low-Pass filter. This type of filter attenuates the higher frequencies of the signal, thus providing a smoother reading. The Low-Pass filter is easily implemented by using the following equation:

y_{t} = \alpha \cdot x_{t} + (1 - \alpha) \cdot y_{t - 1}

Where y_t is our filtered signal, y_{t-1} the previous filtered signal, x_t the accelerometer reading and \alpha the smoothing factor. It probably may seem obvious, but filtering should be done to the accelerometer readings before calculating the angles, instead of to the angles themselves. Regarding the smoothing factor, the lower we set it, the more it will take for the angle to stabilize, so we should not set it too low because then we could lose real-time behaviour. With this I mean that the reading will not correspond to the real angle until it stabilizes, and this could take some time.



1SCL (serial clock)
2SDA (serial data)
3SD0 (serial data output)
6CS (Chip Select)
7VCC 3.3v+


Sluit de sensor aan volgens onderstaand overzicht:

arduino adxl345 schema

Arduino pin:ADXL345 pin:
A4SDA (serial data)
A5SCL (serial clock)
3.3v+VCC 3.3v+


Sluit de Accelerometer module aan zoals hierboven aangegeven.

Het resultaat

arduino adxl345 output

Raspberry Pi

Connect the sensor like:

The I2C ADXL module will be found with adress 0x53:


Use the adafruit library to easy read out your sensor.

Raspberry Pi Library

Python code to use the ADXL345 triple-axis accelerometer over I2C with a Raspberry Pi or BeagleBone Black.


To install the library from source (recommended) run the following commands on a Raspberry Pi or other Debian-based OS system:

Alternatively you can install from pip with:

Note that the pip install method won’t install the example code.

Download github: