# How to convert accelerometer data into G with the ADXL345 digital accelerometer

Am totally new to electronics and the datasheet is very confusing to me, so can anyone please guide me through the specification of the accelerometer? So that i can have a better understanding on the accelerometer and to be able to convert the accelerometer data into G.

import smbus
import time

# Get I2C bus
bus = smbus.SMBus(1)

# Select bandwidth rate register, 0x2C(44)
#       0x0A(10)    Normal mode, Output data rate = 100 Hz
bus.write_byte_data(0x53, 0x2C, 0x0A)
# Select power control register, 0x2D(45)
#       0x08(08)    Auto Sleep disable
bus.write_byte_data(0x53, 0x2D, 0x08)
# 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)

# Read data back from 0x32(50), 2 bytes
# X-Axis LSB, X-Axis MSB

# Convert the data to 10-bits
xAccl = ((data1 & 0x03) * 256) + data0
if xAccl > 511 :
xAccl -= 1024

# Read data back from 0x34(52), 2 bytes
# Y-Axis LSB, Y-Axis MSB

# Convert the data to 10-bits
yAccl = ((data1 & 0x03) * 256) + data0
if yAccl > 511 :
yAccl -= 1024

# Read data back from 0x36(54), 2 bytes
# Z-Axis LSB, Z-Axis MSB

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


These are the code.

You need to multiply the resulting acceleration measurement with the corresponding scale according to table 1 in the datasheet. The resulting value is the acceleration in mg. This means if you configure the range as +-4g fullscale you would have the to multiply e.g. the x-axis by ~4.