0

adxl343 slow reading
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

adxl343 slow reading

by lavericklavericklaverick on Fri May 22, 2020 4:31 am

Hi,

Im using an adxl343 attached to a pi zero and using the adafruit libs.

Ive made a loop that reads the sensor 100 times, adds the values to an array then calculates the average time per loop.

If i read the sensor it takes around 1700microseconds per loop, without reading it (just adding time to an array) i get 34microseconds.

Im just wondering if anyone knows how to speed this up? i want to achieve the ful 3.2kHz datarate that the board says it can do, im currently getting around 500Hz.

Many thanks

Code: Select all | TOGGLE FULL SIZE
print("Importing Libs")
import time
import board
import busio
import adafruit_adxl34x
import numpy
from datetime import datetime
#Define board
i2c = busio.I2C(board.SCL, board.SDA)
accelerometer = adafruit_adxl34x.ADXL343(i2c)
#Set rate and range
adafruit_adxl34x.Range.RANGE_16_G
# adafruit_adxl34x.DataRate.RATE_0_10_HZ
adafruit_adxl34x.DataRate.RATE_3200_HZ

n = 100
data = []
dataAR = []      #array where data is to be stored
print("Starting Process")

startTime = datetime.now().microsecond
for x in range(0,n):
   data = list(accelerometer.acceleration)
   # data = accelerometer.acceleration
   data.append(datetime.now().microsecond)
   dataAR.append(data)
   # dataAR.append(accelerometer.acceleration)
   
print((datetime.now().microsecond - startTime) / n)   

#Write data to file
a = numpy.asarray(dataAR)
numpy.savetxt("datadump.csv", a, delimiter=",")
print("Done")

lavericklavericklaverick
 
Posts: 15
Joined: Mon May 06, 2019 6:58 pm

Re: adxl343 slow reading

by siddacious on Mon May 25, 2020 8:51 pm

You're likely hitting the limits of the speed of the I2C bus; I check the library and there doesn't look to be anything wasting time.
I happened to read this while working on another accelerometer so you can see in this screenshot that at a clock speed of about 335Khz, just reading 6 bytes of acceleration data from the sensor takes nearly 250uS.
accel_read_timing.png
accel_read_timing.png (60.42 KiB) Viewed 27 times

Indeed the datasheet even mentions the I2C bus effectively limiting the data rate to around 800Hz:
343_datasheet_i2c.png
343_datasheet_i2c.png (53.52 KiB) Viewed 27 times


from page 16 of https://www.analog.com/media/en/technic ... DXL343.pdf
With that in mind, I would max out your I2C clock frequency if you haven't already.

If you want to use the higher data rates without dropping samples, the datasheet suggests using SPI with a clock speed of 2MHz or greater (page 13).

Good luck! I'd like to hear how successful you are.

siddacious
 
Posts: 246
Joined: Fri Apr 21, 2017 3:09 pm

Re: adxl343 slow reading

by lavericklavericklaverick on Tue May 26, 2020 9:00 pm

hi, thanks so much for your response, ill give maxing out the i2c rate and then switching to spi if that doesnt work, will post how i get on.

Thanks again,

Andrew

lavericklavericklaverick
 
Posts: 15
Joined: Mon May 06, 2019 6:58 pm

Re: adxl343 slow reading

by lavericklavericklaverick on Wed May 27, 2020 5:41 am

hi, i managed to change the rate on the pi using the config.txt and setting it to 400kHz, this seems to have increased my sensor rate from 500 to 800Hz (1800us to 1500us).

I remember seeing somewhere that you need to add some resistors or something to the sda and clk lines to increase the speed too (?) but i cant find any information about it anywhere.

lavericklavericklaverick
 
Posts: 15
Joined: Mon May 06, 2019 6:58 pm

Re: adxl343 slow reading

by lavericklavericklaverick on Wed May 27, 2020 7:49 am

I found a adxl345 library for python which i should be able to use for the 343, however there is no documentation on how to use it in the code, does anyone know of any examples or docs on how to use this library? https://github.com/nagimov/adxl345spi
thanks again!

lavericklavericklaverick
 
Posts: 15
Joined: Mon May 06, 2019 6:58 pm

Re: adxl343 slow reading

by tannewt on Wed May 27, 2020 1:28 pm

To speed up I2C buses you need to lower the pull up resistance to produce 1s faster. SPI is a better protocol for faster speeds because it doesn't rely on pulls or addresses.

I covered info on these buses in my stream last week. I start talking about it here: https://youtu.be/htY1-RkLtiw?t=1991

tannewt
 
Posts: 1639
Joined: Thu Oct 06, 2016 8:48 pm

Re: adxl343 slow reading

by lavericklavericklaverick on Thu May 28, 2020 12:01 pm

thanks for the info, i think i need to switch to spi but i dont think my programming skills are good enough to make it from scratch for the sensor im using. if you know if there is one and could point me in the right direction id really appreciate it :)

thanks again

lavericklavericklaverick
 
Posts: 15
Joined: Mon May 06, 2019 6:58 pm

Re: adxl343 slow reading

by siddacious on Thu May 28, 2020 12:32 pm

If you're interested, I would gladly help you add SPI to the existing 34x CircuitPython library. We would really appreciate the help :)

I'm specifically 112% willing as the 34x library was one of my first and needs some love. I can walk you through the whole process and help test

siddacious
 
Posts: 246
Joined: Fri Apr 21, 2017 3:09 pm

Re: adxl343 slow reading

by lavericklavericklaverick on Thu May 28, 2020 12:59 pm

great, cheers mate, this will be a good learning experience for me :)
How would you like to go about exchanging information?

lavericklavericklaverick
 
Posts: 15
Joined: Mon May 06, 2019 6:58 pm

Re: adxl343 slow reading

by siddacious on Thu May 28, 2020 1:16 pm

@-ing me (@siddacious) in #circuitpython on the adafruit discord ( https://adafru.it/discord ) would be a good way to get a rough plan. Then you can open an issue on the github repo to discuss/document, then a draft PR once you start working on it. If for whatever reason you're allergic to discord, you can skip that step

siddacious
 
Posts: 246
Joined: Fri Apr 21, 2017 3:09 pm

Please be positive and constructive with your questions and comments.