Uncategorized

adxl355 problem

Asked 
Active yesterday
Viewed 102 times
0

I’m trying to read accelerometric data from the evaluation board EVAL-ADXL355-PMDZ. The board is connected to a raspberry pi 4 that runs on raspbian. The circuit is cabled into the standard SPI pins (4-wires) in the raspberry pi but the chip selection (CS) is connected into one of the free pins (12).

To test the board I wrote the following code that uses spidev to make the comunication with the chip and RPi.GPIO to make the chip selection:

#!/usr/bin/env python3.7

import time
import spidev
import RPi.GPIO as gpio

pin = 12

gpio.setwarnings(False) # stop warnings when the script runs multiple times
gpio.setmode(gpio.BCM)
gpio.setup(pin, gpio.OUT)

spi = spidev.SpiDev()
spi.open(0,0)
spi.mode = 3
spi.max_speed_hz = 5000000

READBIT = 0x01
WRITEBIT = 0x00

def check_adxl355(pin):
    '''gets true if DEVID_MST is 0x1D'''
    address  = 0x01 << 1 | READBIT
    gpio.output(pin, gpio.LOW)
    id_ = spi.xfer2([address ,0])
    gpio.output(pin, gpio.HIGH)
    return id_[1] == 0x1D

def configure_adxl355(pin):
    '''configure the accelerometer '''
    RANGE = 0x2C << 1 | WRITEBIT
    gpio.output(pin, gpio.LOW)
    o_ = spi.xfer2([RANGE, 0x01]) # RANGE_2G
    gpio.output(pin, gpio.HIGH)

    POWER_CTL = 0x2D << 1 | WRITEBIT
    gpio.output(pin, gpio.LOW)
    o_ = spi.xfer2([POWER_CTL, 0x06]) 
    gpio.output(pin, gpio.HIGH)

print("ADXL355 : {}".format(check_adxl355(pin)))
configure_adxl355(pin)

# read data from the ADXL355
AXIS_START = 0x08 << 1 | READBIT
while 1:
    gpio.output(pin, gpio.LOW)
    axisBytes = spi.xfer2([AXIS_START, 0, 0, 0, 0, 0, 0, 0, 0, 0])[1:] # read 9 bytes
    gpio.output(pin, gpio.HIGH)

    X = (axisBytes[0] << 16 | axisBytes[1] << 8 | axisBytes[2]) >> 4
    Y = (axisBytes[3] << 16 | axisBytes[4] << 8 | axisBytes[5]) >> 4
    Z = (axisBytes[6] << 16 | axisBytes[7] << 8 | axisBytes[8]) >> 4

    print(">> {} {} {}".format(X, Y, Z))

    time.sleep(1)

In general what it does is to configure the communication between the accelerometer and the raspberry pi, check if the ADXL355 is connected to the pin 12 by locking at the the mems id register (check_adxl355), configure the range of the accelerometer (configure_adxl355) and reading samples from the FIFO register. When running the previous code I have as the results:

pi@raspberrypi:~/adxl355spi $ ./adxl355.py
ADXL355 : True
>> 0 0 0
>> 0 0 0
...

the first block looks ok as it reports that the accelerometer was found, after that the configuration of the accelerometer is not checked, and finaly the the block that read the accelerometric data (X, Y, Z) returns only 0 even though the accelerometer is moving.

Can anyone spot a problem when reading the accelerometric data?

  • Did you try out the example code from github.com/nuclearfutureslab/adxl355-pi? The author mentioned that his solution should work, so I’m guessing it might work for you too. Maybe you can spot your mistakes more easily when comparing it to a running solution. Or at least you might be able to exclude the possibility of a broken sensor. – raphael_mav Dec 5 at 15:33
  • Yes I try with those examples, but they didn’t work, and also they does’t use pins different than CS, that is the reason I am using RPi.GPIO – Juan Dec 6 at 9:32
  • @Juan, I don’t understand why you set spi.mode = 3. ADXL355 datasheet says (CPOL) = 0 and clock phase (CPHA) = 0, so spi.mode should be 0. (On the other hand, for ADXL345, CPOL/CPHA = 1/1, so spi.mode should be set to 3). – tlfong01 Dec 9 at 2:46    

2 Answers

0

I have worked with these sensors, they can be a real pain!

Have a look over here. Someone already went through the trouble of creating a library in python for using this sensor combined with the SPI bus on the RPi. Basically this is exactly what you need. You can copy your gpio.output(pin, gpio.LOW) and gpio.output(pin, gpio.HIGH) statements into the read_data function of that library or just connect it to pin 24 on the pi instead (like the library doc suggests) and see if that works.

Also from the adxl355.py file I can see that you are interpreting your data differently:

# Join data
x_data = (x_data[0] >> 4) + (x_data[1] << 4) + (x_data[2] << 12)
y_data = (y_data[0] >> 4) + (y_data[1] << 4) + (y_data[2] << 12)
z_data = (z_data[0] >> 4) + (z_data[1] << 4) + (z_data[2] << 12)

# Apply two complement
if x_data & 0x80000 == 0x80000:
    x_data = ~x_data + 1

if y_data & 0x80000 == 0x80000:
    y_data = ~y_data + 1

if z_data & 0x80000 == 0x80000:
    z_data = ~z_data + 1

Maybe you’re just interpreting your data wrong, try the above and see if that fix it. Otherwise I would suggest switching to use that library. Even if it’s temporary it can help you figure out what is wrong with your code.

0

In order to debug such a problem you need start by checking the hardware communication level because SPI protocol is sensitive to long wires [>1m] and you need to know if everything if as it should at this level. Use a logic analyzer like saleae.

Also be aware that ADXL sensors are known for having problems.

I’ve lost a couple of nights, years ago, with this two problems.

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: