Close Menu
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
Facebook X (Twitter) Instagram
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us
Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
How To Electronics
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
How To Electronics
Home » Interfacing ADXL345 Accelerometer with Raspberry Pi Pico
Raspberry Pi Raspberry Pi Pico Projects

Interfacing ADXL345 Accelerometer with Raspberry Pi Pico

Mamtaz AlamBy Mamtaz AlamUpdated:August 3, 20236 Comments5 Mins Read
Share Facebook Twitter LinkedIn Telegram Reddit WhatsApp
Interfacing ADXL345 Accelerometer with Raspberry Pi Pico
Share
Facebook Twitter LinkedIn Pinterest Email Reddit Telegram WhatsApp

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.ComponentsQuantityPurchase Link
1Raspberry Pi Pico1Amazon | AliExpress
2ADXL345 3-Axis Accelerometer1Amazon | AliExpress
3Breadboard1Amazon | AliExpress
4Connecting Wires1Amazon | 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:

  1. Axes: 3-axis (X, Y, Z)
  2. Sensitivity: User-selectable, ±2g, ±4g, ±8g, or ±16g
  3. Resolution: 10-bit or 13-bit, user-selectable
  4. Output data rate (ODR): 0.10 Hz to 3200 Hz
  5. Communication interfaces: I2C (up to 400 kHz) and SPI (4-wire and 3-wire, up to 5 MHz)
  6. Supply voltage range: 2.0 V to 3.6 V
  7. Current consumption: 23 µA to 130 µA, depending on output data rate and measurement mode
  8. Operating temperature range: -40°C to +85°C
  9. Package: 3 mm × 5 mm × 1 mm LGA

Pinout of ADXL345

The ADXL345 3-axis Digital Accelerometer Module has 8 pins.

ADXL345 Pinout

  1. GND: Ground connection for the module.
  2. VCC: Power supply input, typically 3.3V, but it can accept a voltage range from 2.0V to 3.6V
  3. CS (Chip Select): Used for SPI communication. When pulled low, it enables SPI communication; when pulled high, it switches to I2C communication.
  4. 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.
  5. 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.
  6. SDO (Serial Data Out): Master In Slave Out (MISO) line for SPI communication.
  7. SDA (Serial Data): Serial Data (I2C)/Serial Data Input (SPI 4-Wire)/Serial Data Input and Output (SPI 3-Wire).
  8. 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.

ADXL345 Raspberry Pi Pico

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)

ADXL345 Raspberry Pi Pico MicroPython

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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(0, sda=Pin(8), scl=Pin(9), 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, 6)
    x, y, z = ustruct.unpack('<3h', data)
    return x, y, z
 
# Main loop
init_adxl345()
while True:
    x, y, z = read_accel_data()
    print("X: {}, Y: {}, Z: {}".format(x, 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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(0, sda=Pin(8), scl=Pin(9), 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, 6)
    x, y, z = ustruct.unpack('<3h', data)
    return x, y, z
 
# Calculate the magnitude of acceleration
def calc_accel_magnitude(x, y, z):
    return math.sqrt(x**2 + y**2 + z**2)
 
# Calculate roll angle in degrees
def calc_roll(x, y, z):
    return math.atan2(y, math.sqrt(x**2 + z**2)) * (180 / math.pi)
 
# Calculate pitch angle in degrees
def calc_pitch(x, y, z):
    return math.atan2(-x, math.sqrt(y**2 + z**2)) * (180 / math.pi)
 
# Main loop
init_adxl345()
while True:
    x, y, z = read_accel_data()
    magnitude = calc_accel_magnitude(x, y, z)
    roll = calc_roll(x, y, z)
    pitch = calc_pitch(x, y, z)
    print("X: {}, Y: {}, Z: {}, Magnitude: {:.2f}, Roll: {:.2f}, Pitch: {:.2f}".format(x, y, z, magnitude, roll, pitch))
    time.sleep(0.1)

Upload the code and check the results.

This updated code includes thress new functions, calc_roll , calc_roll,  calc_accel_magnitude, to compute the pitch, roll, and magnitude of the acceleration vector. The pitch and roll are calculated using the atan2 function from the math library and are expressed in degrees. The magnitude is calculated as the square root of the sum of the squared acceleration components.

The main loop reads the accelerometer data, calculates the magnitude, pitch, and roll, and then prints the results every second.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Reddit Telegram WhatsApp
Previous ArticleESP32 UWB Pro – Ultra Wideband Module with Amplifier
Next Article What is Cloud Computing? Everything You Need to Know

Related Posts

ADXL375 Accelerometer with Raspberry Pi Pico & MicroPython

ADXL375 Accelerometer with Raspberry Pi Pico & MicroPython

Updated:July 24, 2025
Interface BMI160 with Raspberry Pi Pico & MicroPython

Interface BMI160 with Raspberry Pi Pico & MicroPython

Updated:February 2, 20253K
Shift Register 74HC595 with Raspberry Pi Pico & MicroPython

Shift Register 74HC595 with Raspberry Pi Pico & MicroPython

Updated:February 2, 202513K
Interfacing XBee Module with Raspberry Pi Pico & MicroPython

Interfacing XBee Module with Raspberry Pi Pico & MicroPython

Updated:February 2, 20253K
Modbus RTU with Raspberry Pi Pico & Micropython

Modbus RTU with Raspberry Pi Pico & MicroPython

Updated:February 2, 20258K
Fever Detector with MLX90640 & OpenCV Raspberry Pi

Thermal Fever Detector with MLX90640 & OpenCV Raspberry Pi

Updated:February 2, 20256K
View 6 Comments

6 Comments

  1. Mahmood S on May 22, 2023 9:43 PM

    How can you get acceleration in m/s^2 with these values?

    Reply
  2. Savaliya Juhijuhi on May 24, 2023 9:39 PM

    I am having this error.
    any solution ?

    C++
    1
    2
    3
    4
    5
    6
    <code>  %Run -c $EDITOR_CONTENT
      Traceback (most recent call last):
        File “”, line 26, in
        File “”, line 16, in init_adxl345
      OSError: [Errno 5] EIO
    </code>

    Reply
  3. Vaidhai on July 17, 2023 2:06 AM

    I am having same error, were you able to resolve it?

    Reply
  4. Vaidhai on July 17, 2023 4:45 AM

    I am getting same error, were you able to resolve it?

    Reply
  5. Theo on July 21, 2023 5:53 PM

    I believe the SDA and SCL pins are wrong in the wiring description; should be pins GP8 and GP9

    Reply
  6. Fenri on November 16, 2024 11:07 AM

    Ich habe nun die Falsche verdrahtungsangabe korregiert und mich nach den Pin Belegung in der Programmierung gerichtet. Jetzt geht es. Es reicht nicht im Programm die Pins auf die Fehlerhaften Pinbelegung um zu stellen, da das Programm diese als Schlecht bezeichnet. Lötet den I2C Bus auf Pin 8 und Pin 9, dann sollte es auch bei euch Laufen.

    Reply

CommentsCancel reply

Latest Posts
ESP32 Fingerprint Attendance System with Live Web Dashboard

ESP32 Fingerprint Attendance System with Live Web Dashboard

June 21, 2026
IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

June 14, 2026
DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

May 10, 2026
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

May 2, 2026
A Guide to Sourcing Obsolete ICs for Vintage Projects

Beyond AliExpress: A Guide to Sourcing Obsolete ICs for Vintage Projects

April 21, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

April 27, 2026
Building a Smart Sensor Node with a BLE Microcontroller

Building a Smart Sensor Node with a BLE Microcontroller

February 26, 2026
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

April 27, 2026
Top Posts & Pages
  • ESP32 Fingerprint Attendance System with Live Web Dashboard
    ESP32 Fingerprint Attendance System with Live Web Dashboard
  • IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
    IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
  • 12V DC to 220V AC Inverter Circuit & PCB
    12V DC to 220V AC Inverter Circuit & PCB
  • Buck Converter: Basics, Working, Design & Application
    Buck Converter: Basics, Working, Design & Application
  • How to use INA226 DC Current Sensor with Arduino
    How to use INA226 DC Current Sensor with Arduino
  • LD2410 Sensor with ESP32 - Human Presence Detection
    LD2410 Sensor with ESP32 - Human Presence Detection
  • ECG Graph Monitoring with AD8232 ECG Sensor & Arduino
    ECG Graph Monitoring with AD8232 ECG Sensor & Arduino
  • Designing of MPPT Solar Charge Controller using Arduino
    Designing of MPPT Solar Charge Controller using Arduino
Categories
  • Arduino Projects (197)
  • Articles (60)
    • Learn Electronics (19)
    • Product Review (15)
    • Tech Articles (28)
  • Electronics Circuits (46)
    • 555 Timer Projects (21)
    • Op-Amp Circuits (7)
    • Power Electronics (13)
  • IoT Projects (205)
    • ESP32 MicroPython (7)
    • ESP32 Projects (82)
    • ESP32-CAM Projects (15)
    • ESP8266 Projects (76)
    • LoRa/LoRaWAN Projects (22)
  • Microcontrollers (38)
    • AMB82-Mini IoT AI Camera (4)
    • BLE Projects (18)
    • STM32 Projects (19)
  • Raspberry Pi (93)
    • Raspberry Pi Pico Projects (57)
    • Raspberry Pi Pico W Projects (12)
    • Raspberry Pi Projects (24)
Follow Us
  • Facebook
  • Twitter
  • Pinterest
  • Instagram
  • YouTube
About Us

“‘How to Electronics’ is a vibrant community for electronics enthusiasts and professionals. We deliver latest insights in areas such as Embedded Systems, Power Electronics, AI, IoT, and Robotics. Our goal is to stimulate innovation and provide practical solutions for students, organizations, and industries. Join us to transform learning into a joyful journey of discovery and innovation.

Copyright © How To Electronics. All rights reserved.
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us

Type above and press Enter to search. Press Esc to cancel.

Ad Blocker Enabled!
Ad Blocker Enabled!
Looks like you're using an ad blocker. Please allow ads on our site. We rely on advertising to help fund our site.