Raspberry Pi B.Tech/Diploma Beginner

Measuring Acceleration with ADXL345 Accelerometer & Raspberry Pi

Interfacing ADXL345 3-axis Digital Accelerometer with Raspberry Pi & Python Code to measure Acceleration in x, y, z axis & also pitch, roll

  • Suitable forB.Tech/Diploma Raspberry Pi students
  • Build time1-2 weeks
  • LevelBeginner
  • Available atAjvika Labs, Kakinada

About this project

Overview
In this guide, we’ll learn how to connect the
ADXL345 accelerometer
to the
Raspberry Pi
and measure
acceleration
using
Python
. The
Raspberry Pi
is a small, powerful computer, and the
ADXL345
is a sensor that can
detect motion
. When combined, they make a great pair for all sorts of fun projects like
motion alarms
or interactive games.
We’ll walk through the steps to connect the two devices, then show you how to write simple
Python code
to read data from the
accelerometer
. By the end, you’ll know how to use the
Raspberry Pi 4
and
ADXL345
together and be ready to start your own projects.
Components Required
We need the following components for this guide.
S.N.
Components
Quantity
Purchase Link

Raspberry Pi

Amazon
AliExpress
SunFounder

ADXL345 3-Axis Accelerometer

Amazon
AliExpress
SunFounder

Breadboard

Amazon
AliExpress
SunFounder

Connecting Wires

Amazon
AliExpress
SunFounder
ADXL345 Digital Acccelerometer
The
ADXL345
is a 3-axis accelerometer developed by Analog Devices. This sensor is designed to accurately measure acceleration in the X, Y, and Z axes, making it suitable for various applications. It is particularly popular in the fields of robotics, gaming, virtual reality, wearables, and IoT devices, among others.
This accelerometer is capable of detecting both static acceleration, such as the force of gravity, and dynamic acceleration resulting from motion or vibrations. The ADXL345 has adjustable sensitivity, enabling it to cater to a range of applications with different acceleration requirements.
The ADXL345 communicates with microcontrollers using either I2C or SPI interfaces, which makes it compatible with a wide range of development platforms, such as Arduino, Raspberry Pi , and others. Thanks to its low power consumption and small form factor, the ADXL345 is ideal for battery-powered and space-constrained applications.
Specifications of ADXL345
The ADXL345 is a triple-axis accelerometer with the following key specifications:
Axes: 3-axis (X, Y, Z)
Sensitivity: User-selectable, ±2g, ±4g, ±8g, or ±16g
Resolution: 10-bit or 13-bit, user-selectable
Output data rate (ODR): 0.10 Hz to 3200 Hz
Communication interfaces: I2C (up to 400 kHz) and SPI (4-wire and 3-wire, up to 5 MHz)
Supply voltage range: 2.0 V to 3.6 V
Current consumption: 23 µA to 130 µA, depending on output data rate and measurement mode
Operating temperature range: -40°C to +85°C
Package: 3 mm × 5 mm × 1 mm LGA
Pinout of ADXL345
The ADXL345 3-axis Digital Accelerometer Module has 8 pins.
GND:
Ground connection for the module.
VCC:
Power supply input, typically 3.3V, but it can accept a voltage range from 2.0V to 3.6V
CS (Chip Select):
Used for SPI communication. When pulled low, it enables SPI communication; when pulled high, it switches to I2C communication.
INT1 (Interrupt 1):
This pin can be configured to output interrupt signals for certain events like activity/inactivity detection, single/double-tap detection, or free-fall detection.
INT2 (Interrupt 2):
This pin can be configured to output interrupt signals for certain events like activity/inactivity detection, single/double-tap detection, or free-fall detection.
SDO (Serial Data Out):
Master In Slave Out (MISO) line for SPI communication.
SDA (Serial Data):
Serial Data (I2C)/Serial Data Input (SPI 4-Wire)/Serial Data Input and Output (SPI 3-Wire).
SCL (Serial Clock):
Serial Communications Clock. SCL is the clock for I2C, and SCLK is the clock for SPI.
Interfacing ADXL345 Accelerometer with Raspberry Pi 4
To use the
ADXL345 accelerometer
with a
Raspberry Pi 4
using
Python
, you’ll need to make the necessary
hardware connections
and then write a
Python script
to communicate with the sensor. Let’s start by understanding the connection procedure. Follow the steps below to connect the
accelerometer
to your Raspberry Pi 4.
Connect the ADXL345 to the Raspberry Pi Pico as follows:
VCC from ADXL345 to 3.3V on Raspberry Pi 4 (Pin 1)
GND from ADXL345 to GND on Raspberry Pi 4 (Pin 6)
SDA from ADXL345 to SDA (GPIO 2, Pin 3) on Raspberry Pi 4
SCL from ADXL345 to SCL (GPIO 3, Pin 5) on Raspberry Pi 4
Python Code for Measuring Acceleration & Tilt Angles
Now lets write a
Python Code
to interface
ADXL345
with Raspberry 4 and
measure Acceleration
in X, Y, Z axis as well as
Tilt Angles
. We haven’t used any
library
for this code. You need to enable I2C Communication first to use this code.
This
Python script
is for interfacing with the ADXL345 accelerometer using the
I2C communication
protocol. The ADXL345 is a 3-axis accelerometer, and this script allows you to read
acceleration data
from it and calculate the
tilt angles
(
pitch and roll
) based on the acceleration values.
Copy the following code and paste it on your Thonny IDE Editor Window.

import
smbus
import
time
import
math
# ADXL345 I2C Address
ADXL345_I2C_ADDR
=
0x53
# ADXL345 Registers
POWER_CTL
=
0x2D
DATA_FORMAT
=
0x31
DATAX0
=
0x32
DATAX1
=
0x33
DATAY0
=
0x34
DATAY1
=
0x35
DATAZ0
=
0x36
DATAZ1
=
0x37
class
ADXL345
:
G
=
9.81
# Earth's gravity in m/s^2
def
__init__
(
self
bus_num
=

)
:
self
bus
=
smbus
SMBus
(
bus_num
)
# Set range to +/- 16g and FULL_RES bit
self
bus
write_byte_data
(
ADXL345_I2C_ADDR
DATA_FORMAT
0x0B
)
# Turn on the accelerometer
self
bus
write_byte_data
(
ADXL345_I2C_ADDR
POWER_CTL
0x08
)
def
read_acceleration
(
self
)
:
# Read acceleration data
bytes
=
self
bus
read_i2c_block_data
(
ADXL345_I2C_ADDR
DATAX0

)
# Convert to 2's complement and then to m/s^2
=
(
bytes
[

]
<<

bytes
[

]
)
if
&
(

<<

)
:
=
(

<<

)
=
(
/
(

*
*

)
)
*

*
self
G
y
=
(
bytes
[

]
<<

bytes
[

]
)
if
y
&
(

<<

)
:
y
=
y
(

<<

)
y
=
(
y
/
(

*
*

)
)
*

*
self
G
z
=
(
bytes
[

]
<<

bytes
[

]
)
if
z
&
(

<<

)
:
z
=
z
(

<<

)
z
=
(
z
/
(

*
*

)
)
*

*
self
G
return
(
y
z
)
def
get_tilt_angles
(
self
)
:
y
z
=
self
read_acceleration
(
)
pitch
=
math
degrees
(
math
atan2
(
y
math
sqrt
(
x*
*

+
z*
*

)
)
)
roll
=
math
degrees
(
math
atan2
(
z
)
)
return
pitch
roll
if
__name__
==
"__main__"
:
accelerometer
=
ADXL345
(
)
while
True
:
y
z
=
accelerometer
read_acceleration
(
)
pitch
roll
=
accelerometer
get_tilt_angles
(
)
(
f
"Acceleration - X: {x:.3f} m/s^2, Y: {y:.3f} m/s^2, Z: {z:.3f} m/s^2"
)
(
f
"Pitch: {pitch:.2f}°, Roll: {roll:.2f}°"
)
time
sleep
(
0.5
)
Code Explanation

import
smbus
import
time
import
math
These lines import three Python modules:
smbus
: Provides functions to communicate with devices over I2C, a communication protocol.
time
: Used for various time-related tasks. In this code, specifically for introducing delays.
math
: Provides access to mathematical functions. Here, it’s used for trigonometry and angle conversion.

# ADXL345 I2C Address
ADXL345_I2C_ADDR
=
0x53
# ADXL345 Registers
POWER_CTL
=
0x2D
DATA_FORMAT
=
0x31
DATAX0
=
0x32
This section defines constants:
ADXL345_I2C_ADDR
: The I2C address of the ADXL345 accelerometer.
The next lines (like
POWER_CTL
DATA_FORMAT
, etc.) are register addresses within the ADXL345 device. These registers store configurations and data. For instance,
POWER_CTL
is the address for the power control register, which can be used to turn the accelerometer on/off.

class
ADXL345
:
G
=
9.81
# Earth's gravity in m/s^2
This line starts the definition of a class called
ADXL345
. Within this class, a constant
G
representing Earth’s gravitational force (9.81 m/s^2) is defined. This constant will be used later for acceleration calculations.

def
__init__
(
self
bus_num
=

)
:
self
bus
=
smbus
SMBus
(
bus_num
)
# Set range to +/- 16g a

Technologies used

ArduinoRaspberry PiBLEPythonI2CSPI

Hardware components

  • Arduino
  • Raspberry Pi

Software & tools

  • Thonny

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.