Introduction: Raspberry Pi - ADXL345 3-Axis Accelerometer Python Tutorial

The ADXL345 is a small, thin, ultralow power, 3-axis accelerometer with high resolution (13-bit) measurement at up to ±16 g. Digital output data is formatted as 16-bit twos complement and is accessible through I2 C digital interface. Itmeasures the static acceleration of gravity in tilt-sensing applications, as well as dynamic acceleration resulting from motion or shock. Its high resolution (3.9 mg/LSB) enables measurement of inclination changes less than 1.0°. Here is it demonstration with raspberry pi using python code.

Step 1: What You Need..!!

Picture of What You Need..!!

1. Raspberry Pi

2. ADXL345


3. I²C Cable


4. I²C Shield for Raspberry Pi


5. Ethernet Cable

Step 2: Connection:

Picture of Connection:

Take an I2C shield for raspberry pi and gently push it over the gpio pins of raspberry pi.

Then connect the one end of I2C cable to ADXL345 sensor and the other end to the I2C shield.

Also connect the Ethernet cable to the pi or you can use a WiFi module.

Connections are shown in the picture above.

Step 3: Code:

Picture of Code:

The python code for ADXL345 can be downloaded from our github repository- ControlEverythingCommunity

Here is the link for the same :

The datasheet of ADXL345 can be found here:

We have used SMBus library for python code, the steps to install SMBus on raspberry pi is described here:

You can also copy the code from here, it is given as follows:

# Distributed with a free-will license.

# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.

# ADXL345

# This code is designed to work with the ADXL345_I2CS I2C Mini Module available from


import smbus

import time

# Get I2C bus

bus = smbus.SMBus(1)

# ADXL345 address, 0x53(83)

# Select bandwidth rate register, 0x2C(44)# 0x0A(10) Normal mode, Output data rate = 100 Hz

bus.write_byte_data(0x53, 0x2C, 0x0A)

# ADXL345 address, 0x53(83)

# Select power control register, 0x2D(45)

# 0x08(08) Auto Sleep disable

bus.write_byte_data(0x53, 0x2D, 0x08)

# ADXL345 address, 0x53(83)

# Select data format register, 0x31(49)

# 0x08(08) Self test disabled, 4-wire interface

# Full resolution, Range = +/-2g

bus.write_byte_data(0x53, 0x31, 0x08)


# ADXL345 address, 0x53(83)

# Read data back from 0x32(50), 2 bytes

# X-Axis LSB, X-Axis MSB

data0 = bus.read_byte_data(0x53, 0x32)

data1 = bus.read_byte_data(0x53, 0x33)

# Convert the data to 10-bits

xAccl = ((data1 & 0x03) * 256) + data0

if xAccl > 511 :

xAccl -= 1024

# ADXL345 address, 0x53(83)

# Read data back from 0x34(52), 2 bytes

# Y-Axis LSB, Y-Axis MSB

data0 = bus.read_byte_data(0x53, 0x34)

data1 = bus.read_byte_data(0x53, 0x35)

# Convert the data to 10-bits

yAccl = ((data1 & 0x03) * 256) + data0

if yAccl > 511 :

yAccl -= 1024

# ADXL345 address, 0x53(83)

# Read data back from 0x36(54), 2 bytes

# Z-Axis LSB, Z-Axis MSB

data0 = bus.read_byte_data(0x53, 0x36)

data1 = bus.read_byte_data(0x53, 0x37)

# Convert the data to 10-bits

zAccl = ((data1 & 0x03) * 256) + data0

if zAccl > 511 :

zAccl -= 1024

# Output data to screen

print "Acceleration in X-Axis : %d" %xAccl

print "Acceleration in Y-Axis : %d" %yAccl

print "Acceleration in Z-Axis : %d" %zAccl

Step 4: Applications:

ADXL345 is a small, thin, ultralow power, 3-axis accelerometer which can be employed in Handsets, Medical instrumentation etc. Its application also includes Gaming and pointing devices, Industrial instrumentation, Personal navigation devices and Hard disk drive (HDD) protection.


About This Instructable



More by Dcube Tech Ventures:Home Automation with Raspberry Pi using Relay boardMotion Control with Raspberry Pi and LIS3DHTR, 3-Axis Accelerometer, using Python3-Axis Accelerometer, ADXL345 with Raspberry Pi using Python
Add instructable to: