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.

This is the datasheet for the ADXL345 digital accelerometer [1]: https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf

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)

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.

But I guess it would be easier if you come up with a specific question or problem you're facing during implementation.

| improve this answer | |
  • \$\begingroup\$ First of all,thank you for the solution you provided. X-axis Y-axis Z-axis -28 -26 218 These are the readings i get from the accelerometer and based on the solution you provided above, do i just simply (-28) * 4/1000 in order to get the x-axis reading in g? \$\endgroup\$ – ckkkk Apr 17 '19 at 12:24
  • \$\begingroup\$ That should be correct if configured as +-4g range \$\endgroup\$ – po.pe Apr 17 '19 at 13:14
  • \$\begingroup\$ Until now only i have realised that the data is in +-2g but still if i just use 218*2/1000 all i get is 0.436g which is still not even close to 1g on the z-axis. Am i doing something wrong? \$\endgroup\$ – ckkkk Apr 17 '19 at 14:53
  • \$\begingroup\$ Are you sure the data you get is correct? Can you show some code? \$\endgroup\$ – po.pe Apr 17 '19 at 18:37
  • \$\begingroup\$ i have pasted the code in the question. \$\endgroup\$ – ckkkk Apr 18 '19 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.