Raspberry Pi Accelerometer using the ADXL345?

An accelerometer is a device that measures acceleration forces. Adding an accelerometer to a Raspberry Pi allows it to detect tilt, vibration, shock, and motion. This enables many exciting projects such as gesture recognition, orientation sensing, vibration monitoring, step counting, and more.

Raspberry Pi Accelerometer using the ADXL345?

The ADXL345 from Analog Devices is a popular, low-power, 3-axis digital accelerometer that is easy to interface with a Raspberry Pi using I2C communication. In this comprehensive guide, we will go through everything you need to connect the ADXL345 to a Raspberry Pi and write a Python program to read the acceleration data.

Overview of the ADXL345 Accelerometer

Here are some key things to know about the ADXL345 accelerometer:

  • 3-axis sensing: It can measure acceleration in 3 axes – x, y and z. This allows it to detect linear acceleration, tilt, rotation, shake and more.
  • Digital output: The acceleration is converted to a digital value, allowing easy interfacing with microcontrollers like the Raspberry Pi.
  • I2C interface: It uses the I2C communication protocol which only requires 2 wires to connect it to the Raspberry Pi.
  • Low power: The ADXL345 has a low power mode to conserve battery life when used in portable projects.
  • Small form factor: It comes in a tiny 3mm x 5mm x 1mm LGA package for compact implementation.
  • Up to ±16 g sensing: The accelerometer can measure a maximum acceleration of ±16 g on all axes which covers most application needs.
  • Tap detection: It has a built-in tap detection feature that signals when a sharp tap is detected.
  • Programmable interrupts: You can configure thresholds and use the interrupts pin to trigger events when set acceleration levels are exceeded.

Hardware Hookup Guide

Connecting the ADXL345 accelerometer to a Raspberry Pi is straightforward thanks to the I2C interface and breakout boards. Here is what you will need:


  • Raspberry Pi
  • ADXL345 accelerometer module
  • Jumper wires
  • Breadboard (optional)

Circuit Diagram

This diagram shows how to wire the ADXL345 breakout to the Raspberry Pi. Only 4 connections are needed:

  • VCC – Connect to Raspberry Pi 3.3V power pin. This pin supplies 3.3V to the accelerometer.
  • GND – Connect to common ground.
  • SCL – Connect to I2C clock line on RPi pin 5.
  • SDA – Connect to I2C data line on RPi pin 3.

With these 4 connections established, we can communicate with the ADXL345 using I2C protocols.

Enabling I2C on Raspberry Pi

Before we can proceed, I2C needs to be enabled on the RPi as it is disabled by default.

Follow these steps to enable I2C:

  1. Run sudo raspi-config
  2. Go to Interface Options
  3. Navigate to I2C and select Yes to enable it
  4. Reboot your RPi

I2C should now be enabled and ready for use.

Software Programming

With the hardware connected, we need to write a Python program to communicate with the ADXL345 via I2C and extract acceleration data from its registers.

We will use the Adafruit CircuitPython library which handles all the low level details, making our job easier. Here are the key steps:

1. Install Adafruit CircuitPython Library

Run this command to install it:


sudo pip3 install adafruit-circuitpython-adxl34x

This library provides a nice class to easily interface with the accelerometer.

2. Import the adafruit_adxl34x module


import adafruit_adxl34x

3. Create ADXL345 object


accelerometer = adafruit_adxl34x.ADXL345()

This initializes communication using default I2C bus and address.

4. Read acceleration data

Simply call accelerometer.acceleration to read a tuple with x, y and z acceleration values in meters per second squared (m/s^2).


x, y, z = accelerometer.acceleration

print(x, y, z)

The readings are given as floating point values.

By putting the above code in a loop, we can continuously monitor acceleration. Scaling can be applied if needed.

This covers the basic working of the ADXL345 accelerometer! Next we will put this knowledge into practice with projects.


Now let’s look at some practical projects to showcase the capabilities of the connected ADXL345 accelerometer:

1. Simple Tilt Sensing

We can detect way the ADXL345 is tilted by observing the acceleration readings on each axis.

For example, when tilting the sensor so that the x-axis is pointing downwards, the x-reading will shift towards -1g or -9.8 m/s^2.

Similarly tilting it so the z-axis points downwards will shift the z-reading.

By comparing the axis readings, we can deduce tilt. This can detect basic gestures.

2. Tap Detection

The ADXL345 has a built-in tap detection feature. By setting the tap threshold and duration values, we can trigger events when a sharp tap occurs – useful for user input.

Here is an example:


Set tap thresholds  



 Configure tap detection  


 Continuously check if tap detected

while True:

   if accelerometer.events[‘tap’]:

      print(‘Tap detected!’)

We are detecting any taps over 0.5 g that last less than 50 ms. Many embedded projects can leverage tap or touch detection for user interaction.

3. Step Counting

By observing sharp vertical spikes in acceleration, we can detect steps while walking or running.

The ADXL345 readings can be processed by a algorithm to increment a step count. This is useful for tracking physical activity.

Here is simplified example:


steps = 0 

while True:

   x, y, z = accelerometer.acceleration

    Check for > 2g spike on z-axis  

   if(z > 2.0):    

      steps += 1

   print(‘Steps:’, steps)


Of course, more complex signal processing would improve accuracy greatly. This demonstrates the basic principle.

4. Gesture Recognition

Expanding on the tilt sensing, we can classify acceleration patterns into defined gestures like shake, tilt left, flip upside down etc.

For example, a shake can be detected by looking for rapid spikes in magnitude of acceleration. More complex quaternions analysis would enable 3D spatial orientation sensing as well.

Combined with machine learning algorithms, we can reliably detect many different gestures. Useful for gesture control projects!

5. Vibration Monitoring

Unexpected vibration can indicate issues in machinery. The ADXL356 allows vibration level monitoring by checking magnitude of acceleration.

By setting relevant thresholds, alerts can be triggered when abnormal vibration occurs – enabling predictive maintenance.

Key Takeaways

  • The ADXL345 is a versatile, low-power 3-axis accelerometer useful for many sensing projects.
  • It communicates via the I2C protocol which is easy to connect to a Raspberry Pi
  • Acceleration in 3 axes can be read by interfacing ADXL345 with Python code
  • Useful applications include gesture detection, orientation sensing, tap detection, step counting and vibration monitoring
  • Combined with algorithms and machine learning, even more complex uses are possible


By following this guide, you learned how to integrate the ADXL345 accelerometer with a Raspberry Pi. The I2C communication protocol and Adafruit CircuitPython library allow easy interfacing.

Acquisition of acceleration data on 3-axes enables projects ranging from basic tilt and tap sensing to complex gesture recognition with machine learning. There are many possibilities to explore!

With the detailed code samples, hardware hookups and project examples covered in this article, you should have all the building blocks to start building your own accelerated Raspberry Pi projects using this versatile accelerometer.

Frequently Asked Questions

Q: Does ADXL345 only work with Raspberry Pi?
A: No, the ADXL345 can work with any microcontroller like Arduino, ESP32 etc. as long as it supports I2C communication. The same code can be ported to other platforms.

Q: How fast can the ADXL345 accelerometer sample data?
A: The maximum data rate is 3200 Hz so it can provide very responsive real-time sensing. The data rate can be adjusted lower as well to reduce power consumption.

Q: Can I connect multiple ADXL345 sensors to single RPi?
A: Yes, you can connect multiple ADXL345 sensor modules to the same RPi by giving each a unique I2C address and selecting them accordingly in software.

Q: Is ADXL345 waterproof?
A: No the module itself is not waterproof so for applications where it could get wet you would need to enclose it properly in a sealed case.

Q: How accurate is the acceleration data?
A: The ADXL345 provides 12-bit resolution measurements so after calibration it can provide very precise acceleration readings for most applications.

Q: Can the ADXL345 be used to detect falls?
A: Yes, by properly configuring the thresholds and monitoring acceleration spikes especially on the Z axis, fall detection is possible with this sensor.

Q: Does the ADXL345 detect rotation?
A: Not directly, but by tracking the shifting acceleration on each axis we can calculate rotation angle around each axis.

Q: Can I connect ADXL345 to Android and iOS devices?
A: Yes, using external adapters the ADXL345 can potentially communicate with smartphones and tablets over protocols like USB and Bluetooth to provide motion sensing.

Q: Is soldering required to use the ADXL345 module?
A: No soldering is needed, it provides a standard 0.1 inch pitch pin header that can be directly plugged into prototyping breadboards or connector cables.

Q: What GPIO pins on the RPi should be avoided when using I2C devices like accelerometer?
A: Avoid assigning GPIO pins 3, 5, 12, 13, 18, 19, 41, 45, 53, 58, 59 to other duties as they can interfere with I2C communications due to overlaps.

Q: How do I distinguish between dynamic motion and static orientation tilt with the accelerometer?
A: Static tilt sensing requires high-pass filtering of acceleration data to remove gravity effects while dynamic motion tracking uses the raw readings. Band-pass filtering allows both.

Q: Can I connect the ADXL345 accelerometer to Arduino instead of Raspberry Pi?
A: Yes, the ADXL345 is fully compatible with all kinds of Arduino boards. You can port the Arduino I2C library and similar code to read acceleration data.

Q: What is the maximum measurement range on each axis of the ADXL345?
A: The accelerometer supports up to ±16 g range on all 3 axes. Higher values can be handled by adjusting device range settings.

Q: Is latency low enough for the accelerometer to be used in VR applications
A: Absolutely, with low latency and up to 3.2 kHz data rate, the ADXL345 is suitable for tracking VR headset motion in real-time.


Leave a Comment