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

About: We are a group of makers. We work in IoT, IOS app, android app, embedded design, sensor design, raspberry pi, arduino, beaglebone, particle electron, particle photon, Bluetooth.

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..!!

Step 2: 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:

The python code for ADXL345 can be downloaded from our GitHub repository- Dcube Store

Here is the link for the same :

https://github.com/DcubeTechVentures/ADXL345..

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

https://pypi.python.org/pypi/smbus-cffi/0.5.1

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 in Dcube Store.

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)

time.sleep(0.5)

# 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.