RPi Pico B.Tech/Diploma Beginner

Interfacing ADXL345 Accelerometer with Raspberry Pi Pico

Interfacing ADXL345 3 Axis I2C Digital Accelerometer with Raspberry Pi Pico using MicroPython Code to measure acceleration & pitch, roll

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

About this project

Overview
In this guide, we will explore the process of interfacing the ADXL345 3-axis Accelerometer with the Raspberry Pi Pico using MicroPython code. The Raspberry Pi Pico is a cost-effective, flexible, and powerful microcontroller that pairs well with the ADXL345, a high-resolution and low-power digital accelerometer. Together, these components can be used to develop a wide range of fascinating projects, such as motion-based control systems, orientation sensors, or even creative game controllers.
Throughout this guide, we will offer you detailed instructions on how to connect the Raspberry Pi Pico and the ADXL345 accelerometer. We will also guide you through the process of writing the necessary code to acquire and process the accelerometer data. By the end of this tutorial, you will have gained a solid understanding of both the Raspberry Pi Pico and the ADXL345 accelerometer, as well as the skills needed to create your own custom projects using these components.
You may take a look at the
MPU6050
and
ADXL335
in case you need some other motion sensors.
Components Required
We need the following components for this guide.
S.N.
Components
Quantity
Purchase Link

Raspberry Pi Pico

Amazon
AliExpress

ADXL345 3-Axis Accelerometer

Amazon
AliExpress

Breadboard

Amazon
AliExpress

Connecting Wires

Amazon
AliExpress
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 Pico, 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 Pico
To use the ADXL345 accelerometer with a Raspberry Pi Pico using MicroPython, you’ll need to connect the module to the Pico and write a MicroPython script to communicate with the sensor. Here’s a simple example using I2C communication. First connect as per circuit diagram.
Connect the ADXL345 to the Raspberry Pi Pico as follows:
VCC to 3.3V (Pin 36)
GND to GND (Pin 38)
SDA to SDA (GP20, Pin 26)
SCL to SCL (GP21, Pin 27)
CS to 3.3V (This is to use I2C mode)
SD0 to GND (This sets the I2C address to 0x53)
You can use a breadboard and jumper wires for connection.
MicroPython Code to Read Acceleration Values
Here’s a sample MicroPython code to read the Acceleration Raw Values in the x, y, and z-axis by interfacing the ADXL345 & Raspberry by Pico.

from
machine
import
Pin
I2C
import
time
import
ustruct
# Constants
ADXL345_ADDRESS
=
0x53
ADXL345_POWER_CTL
=
0x2D
ADXL345_DATA_FORMAT
=
0x31
ADXL345_DATAX0
=
0x32
# Initialize I2C
i2c
=
I2C
(

sda
=
Pin
(

)
scl
=
Pin
(

)
freq
=
400000
)
# Initialize ADXL345
def
init_adxl345
(
)
:
i2c
writeto_mem
(
ADXL345_ADDRESS
ADXL345_POWER_CTL
bytearray
(
[
0x08
]
)
)
# Set bit 3 to 1 to enable measurement mode
i2c
writeto_mem
(
ADXL345_ADDRESS
ADXL345_DATA_FORMAT
bytearray
(
[
0x0B
]
)
)
# Set data format to full resolution, +/- 16g
# Read acceleration data
def
read_accel_data
(
)
:
data
=
i2c
readfrom_mem
(
ADXL345_ADDRESS
ADXL345_DATAX0

)
y
z
=
ustruct
unpack
(
'<3h'
data
)
return
y
z
# Main loop
init_adxl345
(
)
while
True
:
y
z
=
read_accel_data
(
)
(
"X: {}, Y: {}, Z: {}"
format
(
y
z
)
)
time
sleep
(
0.1
)
This code initializes the ADXL345, configures it for full-resolution, ±16g measurements, and then reads and prints the acceleration data for the X, Y, and Z axes every 0.1 second. You can adjust the code to use a different sensitivity setting or read the data at a different rate, depending on your application’s requirements.
MicroPython Code to Read Rotational Angle Values
To measure the magnitude, pitch, and roll using the ADXL345 accelerometer, you can modify the MicroPython code. Here’s the updated code to calculate the magnitude, pitch, and roll and print them along with the acceleration data:

from
machine
import
Pin
I2C
import
time
import
ustruct
import
math
# Constants
ADXL345_ADDRESS
=
0x53
ADXL345_POWER_CTL
=
0x2D
ADXL345_DATA_FORMAT
=
0x31
ADXL345_DATAX0
=
0x32
# Initialize I2C
i2c
=
I2C
(

sda
=
Pin
(

)
scl
=
Pin
(

)
freq
=
400000
)
# Initialize ADXL345
def
init_adxl345
(
)
:
i2c
writeto_mem
(
ADXL345_ADDRESS
ADXL345_POWER_CTL
bytearray
(
[
0x08
]
)
)
# Set bit 3 to 1 to enable measurement mode
i2c
writeto_mem
(
ADXL345_ADDRESS
ADXL345_DATA_FORMAT
bytearray
(
[
0x0B
]
)
)
# Set data format to full resolution, +/- 16g
# Read acceleration data
def
read_accel_data
(
)
:
data
=
i2c
readfrom_mem
(
ADXL345_ADDRESS
ADXL345_DATAX0

)
y
z
=
ustruct
unpack
(
'<3h'
data
)
return
y
z
# Calculate the magnitude of acceleration
def
calc_accel_magnitude
(
y
z
)
:
return
math
sqrt
(
x*
*

+
y*
*

+
z*
*

)
# Calculate roll angle in degrees
def
calc_roll
(
y
z
)
:
return
math
atan2
(
y
math
sqrt
(
x*
*

+
z*
*

)
)
*
(

/
math
pi
)
# Calculate pitch angle in degrees
def
calc_pitch
(
y
z
)
:
return
math
atan2
(
math
sqrt
(
y*
*

+
z*
*

)
)
*
(

/
math
pi
)
# Main loop
init_adxl345
(
)
while
True
:
y
z
=
read_accel_data
(
)
magnitude
=
calc_accel_magnitude
(
y
z
)
roll
=
calc_roll
(
y
z
)
pitch
=
calc_pitch
(
y
z
)
(
"X: {}, Y: {}, Z: {}, Magnitude: {:.2f}, Roll: {:.2f}, Pitch: {:.2f}"
format
(
y
z
magnitude
roll
pitch
)
)
time
sleep
(
0.1
)
Upload t

Technologies used

ArduinoRaspberry PiBLEMPU6050PythonI2CSPIMicroPython

Hardware components

  • Arduino
  • Raspberry Pi
  • MPU6050

Software & tools

  • MicroPython

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.