Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.

adafruit lis3dh slow reading
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

adafruit lis3dh slow reading

by lavericklavericklaverick on Mon Jun 15, 2020 5:07 pm

Hi, im using the adafruit_lis3dh module and am getting very slow read times using a pi zero (overclocked to 1ghz)

Im running 2 sensors on spi, ive been playing around with what i read from and get the follow results:

read both sensors = 150Hz
read on sensor and dummy array to simulate the other process = 275Hz
not reading either sensor but using dummy array to simulate other processes = 16Khz

ive looked into the module and it doesnt seem like its doing many steps for the acceleration function.

it doesnt make any difference if i set it to 1344Hz, 400Hz or 10Hz data rate.

Does anyone know how to speed this up? I bought new sensors and have been playing around with my own modules in an attempt to use spi instead of i2c which was too slow but this is even slower! :(

Any advice would be much appreciated, thanks

Code: Select all | TOGGLE FULL SIZE
print("Importing Modules")
import time
import board
import digitalio
import busio
import adafruit_lis3dh
import numpy
from datetime import datetime
import os.path
print("File last modified: %s" % time.ctime(os.path.getmtime(__file__)))

#Define board
spi1 = busio.SPI(board.SCK, board.MOSI, board.MISO)
spi2 = busio.SPI(board.SCK, board.MOSI, board.MISO)
#Define chip select pins
cs1 = digitalio.DigitalInOut(board.D5)  # Set to appropriate CS pin!
cs2 = digitalio.DigitalInOut(board.D6)  # Set to appropriate CS pin!
#Setup each sensor
lis3dh1 = adafruit_lis3dh.LIS3DH_SPI(spi1, cs1)
lis3dh2 = adafruit_lis3dh.LIS3DH_SPI(spi2, cs2)
#Set data rates
lis3dh1.data_rate = adafruit_lis3dh.DATARATE_10_HZ
lis3dh2.data_rate = adafruit_lis3dh.DATARATE_10_HZ
#Set range
# lis3dh.range = adafruit_lis3dh.RANGE_2_G
dataAR = []      #array where data is to be stored
time = []
dtAR = []
n = 100

print("Starting Process")

start =
for x in range(0, n):
   x1, y1, z1 = lis3dh1.acceleration      #read data from sensor1
   # x1, y1, z1 = [1,1,1]      #dummy array
   x2, y2, z2 = lis3dh2.acceleration      #read data from sensor1
   # x2, y2, z2 = [1,1,1]      #dummy array
   data = (x1, y1, z1, x2, y2, z2)
   # data = (x1, y1, z1)
total = - start
print(1000000 * n / total,'Hz')

Posts: 18
Joined: Mon May 06, 2019 6:58 pm

Re: adafruit lis3dh slow reading

by mikeysklar on Mon Jun 15, 2020 7:05 pm

Hello lavericklavericklaverick,

The best results I've found in the forums for speeding up the readings from dual lis3dh sensors have been using a faster i2c. They are setting their speed up to 800KHz. I believe you should be able to set the speed for i2c on your Pi using this:

Code: Select all | TOGGLE FULL SIZE

CircuitPython code i2c speed setting:

Code: Select all | TOGGLE FULL SIZE
i2c_bus = busio.I2C(board.SCL, board.SDA, frequency=800000)

You might be able to set your i2c up to 1,000,000 (1mhz). 800khz is a little strange, but I'm mentioning based on this thread.


Posts: 1243
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.