Raspberry Pi - ADXL345 3-Axis Accelerometer Python Tutorial

Published

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

1. Raspberry Pi

2. ADXL345

LINK : https://www.controleverything.com/content/Accelorometer?sku=ADXL345_I2CS

3. I²C Cable

LINK : https://www.controleverything.com/content/I2C-Cables?sku=I2CAFF3

4. I²C Shield for Raspberry Pi

LINK : https://www.controleverything.com/content/I2C-Master?sku=TOUTPI2

5. Ethernet Cable

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- ControlEverythingCommunity

Here is the link for the same :

https://github.com/ControlEverythingCommunity/ADXL...

The datasheet of ADXL345 can be found here:

https://s3.amazonaws.com/controleverything.media/c...

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 from ControlEverything.com.

# https://www.controleverything.com/content/Accelor...

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.

Share

    Recommendations

    • Clocks Contest

      Clocks Contest
    • Oil Contest

      Oil Contest
    • Woodworking Contest

      Woodworking Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    2 Questions

    i got this error can anyone help me out
    Traceback (most recent call last):
    File "/home/pi/m4.py", line 10, in
    bus.write_byte_data(0x53, 0x2C, 0x0A)
    File "/home/pi/.local/lib/python2.7/site-packages/smbus/util.py", line 59, in validator
    return fn(*args, **kwdefaults)
    File "/home/pi/.local/lib/python2.7/site-packages/smbus/smbus.py", line 145, in write_byte_data
    raise IOError(ffi.errno)
    IOError: 121

    0

    hey,i got this error,can anyone help me out
    Traceback (most recent call last):
    File "/home/pi/m4.py", line 10, in
    bus.write_byte_data(0x53, 0x2C, 0x0A)
    File "/home/pi/.local/lib/python2.7/site-packages/smbus/util.py", line 59, in validator
    return fn(*args, **kwdefaults)
    File "/home/pi/.local/lib/python2.7/site-packages/smbus/smbus.py", line 145, in write_byte_data
    raise IOError(ffi.errno)
    IOError: 121

    Comments