ADXL345 accelero notes

Active today
Viewed 39 times

I’ve been trying to use a Raspberry Pi 3 B+ to get data from an accelerometer, namely ADXL 357 , via I2C but the problem is that the data rate is about 200 Hz or even less. I tried to set the accelerometer to high speed mode and it didn’t help. I also tried to change the baudrate in the rasberry but i wouldn’t go higher than 2.000.000 or something similar.

import smbus
import time
import csv
import RPi.GPIO as GPIO
from datetime import datetime


i2c_adress = 0x1D

acc_range = 0x81 # 0x01->8g, 0x02->20g, 0x03->40g

# i'm putting 0x81 rather than 0x01 to set it to high speed mode rather than fast mode

bus = smbus.SMBus(i2c_ch)

#val = bus.read_i2c_block_data(i2c_adress,reg_range,3)
def configure_raspberry():

def configure_adxl() :


    #set filter


    # set i2c speed to normal and acceleration range

    #set in measurement mode (not standby)

def get_temperature() :

    temp1 = bus.read_byte_data(i2c_adress,0x06)
    temp2 = bus.read_byte_data(i2c_adress,0x07)
    temp = (temp1 << 8) | temp2
    temp = (float)(1852 - temp)/9.05 + 25
    return temp

def get_x_accel() :
    x1 = bus.read_byte_data(i2c_adress,0x08)
    x2 = bus.read_byte_data(i2c_adress,0x09)
    x3 = bus.read_byte_data(i2c_adress,0x0A)
    x = (x1 << 12) | (x2 << 4) | (x3 >> 4)
    if(x & 0x80000) :
        x = (x & 0x7ffff) - 0x80000
    return x

def get_y_accel() :
    y1 = bus.read_byte_data(i2c_adress,0x0B)
    y2 = bus.read_byte_data(i2c_adress,0x0C)
    y3 = bus.read_byte_data(i2c_adress,0x0D)
    y = (y1 << 12) | (y2 << 4) | (y3 >> 4)
    if(y & 0x80000) :
        y = (y & 0x7ffff) - 0x80000
    return y

def get_z_accel() :
    z1 = bus.read_byte_data(i2c_adress,0x0E)
    z2 = bus.read_byte_data(i2c_adress,0x0F)
    z3 = bus.read_byte_data(i2c_adress,0x8)
    z = (z1 << 12) | (z2 << 4) | (z3 >> 4)
    if(z & 0x80000) :
        z = (z & 0x7ffff) - 0x80000
    return z

def convert_in_g(accel) :
    if(acc_range == 0x01) :
        accel = (float)(accel)/0x7ffff*8
    if(acc_range == 0x02) :
        accel = (float)(accel)/0x7ffff*20
    if(acc_range == 0x03) :
        accel = (float)(accel)/0x7ffff*40
    return accel


while True :
        date = datetime.now()

        timestamp = datetime.timestamp(date)
        dateStr = date.strftime("%d-%b-%Y (%H:%M:%S.%f)")

        name = dateStr +'.csv'

        if GPIO.input(8) == GPIO.HIGH :
            f = open(name,'a',newline = "")
            c = csv.writer(f)

            while GPIO.input(8) == GPIO.HIGH :
                temp = get_temperature()
                print("temperature uncalibrated : ",temp)
                x = get_x_accel()
                print("x raw acceleration : ",x)
                x = convert_in_g(x)
                print("x acceleration in g : ",x)
                y = get_y_accel()
                print("y raw acceleration : ",y)
                y = convert_in_g(y)
                print("y acceleration in g : ",y)
                z = get_z_accel()
                print("z raw acceleration : ",z)
                z = convert_in_g(z)
                print("z acceleration in g : ",z)
                date2 = datetime.now()

                timestamp = datetime.timestamp(date2)
 New contributor
  • 200Hz data rate should be possible with 100 kHz I2C, so it’s not the baudrate problem. You are likely using inefficient I2C libraries/drivers. – Dmitry Grigoryev yesterday
  • Can you please tell me how you do you convert baudrate from bit to Hz ? – MOHAMED KOUBAAyesterday
  • I checked the datasheet. They are advertising 200Hz data rate with 100 kHz I2C there. – Dmitry Grigoryevyesterday
  • You are asking us to guess what you are doing. You need to provide detail. – joan yesterday
  • @DmitryGrigoryev but I2C in high speed mode should support up to 3.4 MHz , are the any I2C libraries you can suggest – MOHAMED KOUBAA 23 hours ago



I can only get ADXL357 data rate 200Hz. Can I do better?

Datasheet Spec

(1) ODR (Output Data Rate) is 4kHz. (Table 2, Page 4)

(2) I2C speed standard rate 100kHz, max 3.4MHz. (Page 26)


Rpi3B+, because of a design bug, has a I2C flat rate of ridiculously low standard 100kHz. Yes, you cannot adjust I2C speed for Rpi3B+ stretch.


The official instruction tells you how to set to speed to 400kHz or higher. I have checked with scope that it does not work. I have also read about this bug in other forums, as reported by very experienced users.


Upgrade to Rpi4B, then you can set higher I2C speed, therefore higher ADXL357 output data rate.


(1) ADXL356/ADXL357 3 Axis MEMS Accelerometers Data Sheet – Analog Devices


(2) ADXL345 3-Axis, Digital Accelerometer Data Sheet – Analog Devices


(3) Adafruit 9-DOF accelerometer/magnetometer/gyroscope sensor Forum Discussion

Adafruit 9-DOF or other accelerometer/magnetometer/gyroscope sensor for Raspberry PI 2/3 with Windows IoT

(4) Rpi4B Raspbian 10 buster circuit-python-blinka installation problem (BNO0955 9DOF) Forum Discussion

Rpi4B Raspbian 10 buster circuit-python-blinka installation problem

(5) SparkFun Triple Axis Accelerometer Breakout – ADXL345 $19


(6) SparkFun ADXL345 Hookup Guide


(7) SparkFun Accelerometer, Gyro and IMU Buying Guide


(8) SparkFun Accelerometers Catalog


AdaFruit ADXL345 – Triple-Axis Accelerometer (+-2g/4g/8g/16g) w/ I2C/SPI – $18


(10) AdaFruit ADXL345 Digital Accelerometer Learning Notes


(11) AdaFruit I2C Addresses List



  • So the only solution I have is to buy the new Rpi 4B ? – MOHAMED KOUBAA 23 hours ago
  • Well, I would suggest that we first confirm a couple of things. First you might like to read the following post, to make sure you understand and agree on what I complain about the Rpi3 I2C speed no setting bug. RASPBERRY PI3 I2C BAUD RATE SETTING Postby samtal » 2018-Aug-04 Sat 1:45 pm raspberrypi.org/forums/… – tlfong01 23 hours ago    
  • Second, you might like to read the datasheet to understand and agree on what I am saying about (1) the AXDL357 output data rate maximum of 4k samples per seconds, (2) AXDL can take from 100kHz and higher, to a maximum of 3.4 MHz. – tlfong01 23 hours ago   
  • Thirdly, you might like to add a listing you working program to your question. I can try to see if there is any way of increasing the sampling rate. If I see your program is OK, and that using Rpi4 to test higher I2C speed is likely to be a good thing to do. Then go to step 4. – tlfong01 22 hours ago   
  • Right now I am using a Rpi4B to test some I2C devices. I can try to run your Rpi3 AXDL357 program on my Rpi4, with I2C set to a higher speed and see if can at least run (without the ADXL357). – tlfong01 22 hours ago   
  • I just added the code to the post – MOHAMED KOUBAA 22 hours ago
  • I don’t know how you can do that , but it would really be great – MOHAMED KOUBAA 22 hours ago
  • I must confess that I have been playing with I2C/SPI sensors for one hobbyist year, but I am a MEMS gyro/accelero newbie. You might like to read references (3), (4) – tlfong01 22 hours ago   
  • Bed time. See you tomorrow. – tlfong01 22 hours ago   
  • ADXL357 can also do SPI, up to 10MHz. So you can do minor changes to your program and test again, in SPI mode. – tlfong01 12 hours ago   

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: