I've been trying to use a Raspberry Pi 3 B+ to get data from an accelerometer, namely an 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_ch=1
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():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(8,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(18,GPIO.OUT)
def configure_adxl() :
#reset
bus.write_byte_data(i2c_adress,0x2f,0x52)
#set filter
bus.write_byte_data(i2c_adress,0x28,0x01)
# set i2c speed to normal and acceleration range
bus.write_byte_data(i2c_adress,0x2c,acc_range)
#set in measurement mode (not standby)
bus.write_byte_data(i2c_adress,0x2d,0x00)
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
configure_adxl()
configure_raspberry()
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)
time.sleep(0.08)
while GPIO.input(8) == GPIO.HIGH :
GPIO.output(18,GPIO.HIGH)
temp = get_temperature()
print("temperature uncalibrated : ",temp)
print("_________________________________________________________")
print("\n")
x = get_x_accel()
print("x raw acceleration : ",x)
x = convert_in_g(x)
print("x acceleration in g : ",x)
print("\n")
y = get_y_accel()
print("y raw acceleration : ",y)
y = convert_in_g(y)
print("y acceleration in g : ",y)
print("\n")
z = get_z_accel()
print("z raw acceleration : ",z)
z = convert_in_g(z)
print("z acceleration in g : ",z)
print("\n\n\n\n")
date2 = datetime.now()
timestamp = datetime.timestamp(date2)
c.writerow([timestamp,x,y,z])
f.close()
GPIO.output(18,GPIO.LOW)