loading

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

In this tutorial the interfacing of the ADXL345 sensor module with raspberry pi is demonstrated and its programming using python language has also been illustrated. To read the values of acceleration on all the 3-axis, we have used raspberry pi with an I2C adapter.This I2C adapter makes the connection to the sensor module easy and more reliable.

Step 1: Hardware Required:

The materials that we need for accomplishing our goal includes the following hardware components:

1. ADXL345

2. Raspberry Pi

3. I2C Cable

4. I2C Shield for raspberry pi

5. Ethernet Cable

Step 2: Hardware Hookup:

The hardware hookup section basically explains the wiring connections required between the sensor and the raspberry pi. Ensuring correct connections is the basic necessity while working on any system for the desired output. So, the requisite connections are as follows:

The ADXL345 will work over I2C . Here is the example wiring diagram, demonstrating how to wire up each interface of the sensor.

Out-of-the-box, the board is configured for an I2C interface, as such we recommend using this hookup if you’re otherwise agnostic.

All you need is four wires! Only four connections are required Vcc, Gnd, SCL and SDA pins and these are connected with the help of I2C cable.

These connections are demonstrated in the pictures above.

Step 3: Code for Measurement of Acceleration:

The advantage of using raspberry pi is, that is provides you the flexibility of the programming language in which you want to program the board in order to interface the sensor with it. Harnessing this advantage of this board, we are demonstrating here its programming in the python. The python code for ADXL345 can be downloaded from our github community that is Control Everything Community.

As well as for the ease of the users, we are explaining the code here also:

As the first step of coding you need to download the smbus library in case of python, because this library supports the functions used in the code. So, to download the library you can visit the following link:

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

You can copy the working python code for this sensor from here also:

import smbus

import time

# Get I2C busbus = 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

The part of code mentioned below includes the libraries required for the correct execution of the python codes.

import smbus
import time

The code can be executed by typing the below mentioned command in the command prompt.

$> python ADXL345.py

The output of the sensor is also shown in the picture above for the reference of the user.

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

221views

0favorites

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