How To Connect Adxl345 To Raspberry Pi Pico

About the project

In this tutorial learn how to connect the ADXL345 to the Raspberry Pi Pico to get real acceleration values.

Project info

Difficulty: Easy

Platforms: MicroPython

Estimated time: 1 hour

License: GNU Lesser General Public License version 3 or later (LGPL3+)

Items used in this project

Hardware components

ADXL345 ADXL345 x 1

Software apps and online services

MicroPython MicroPython


Before getting started, please consider subscribing or supporting channel by donating in the link down below to allow us to produce more content!




1-) Step One: Physical Connection

Setup pins as follows for I2C connection, you only need 4 jumper wires. You can get the sensor and Raspberry Pi pre-soldered or solder them yourself. This is sufficient enough to start getting sensor values! Be sure not to mix power connections as this can fry the sensor.

2-) Step Two: Code

Run the following code in MicroPython:

from machine import Pin, I2C
import time
import ustruct

# Constants
ADXL345_ADDRESS = 0x53 # address for accelerometer
ADXL345_POWER_CTL = 0x2D # address for power control
ADXL345_DATA_FORMAT = 0x31 # configure data format
ADXL345_DATAX0 = 0x32 # where the x-axis data starts

# Initialize I2C
i2c = I2C(0, sda=Pin(0), scl=Pin(1), 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
while True:
x, y, z = read_accel_data()
print(x, y, z) # raw values from sensor
print("X: {}, Y: {}, Z: {}".format(x*0.0039, y*0.0039, z*0.0039))

# if you do get OSError: [Errno 5] EIO, try unplug and plug
# if you do set different resolution 0.0039 may not be the constant (check data sheet)

If you set everything up properly you should start seeing values in x, y, and z in units of g (9.81 m/s^2).

Some takeaways from the code:
  • Gives you acceleration values up to 16g. You can adjust this by changing the bytearray you pass into the DATA_FORMAT register.
  • You can calibrate your sensor for more accurate results. ShillehTek has a video on this already here.
  • If you do get OSError trying unplugging and plugging the Pico in.
  • 0.0039 is retrieved from the ADXL345 datasheet, this value changes depending on the resolution you select
  • You can extend this and calculate the angle with some trigonometry, another tutorial will be made on this topic.


Please consider subbing to the channel if this helped you in any way, would help us create more content for you! Let me know if you have any questions. Thanks!


Photo of mahmood-m-shilleh


Mechanical and Software Engineering Background. University at Buffalo 2019 Texas A&M 2021 I make data pipelines for my day job. Outside of work, I participate in online communities regarding Full Stack Engineering, Microelectronics, and more. You can find more details about me on my Youtube Channel. Feel free to reach out!


Leave your feedback...