How to view SPI Arduino data from accelerometer within Matlab?

17 views (last 30 days)
I am trying to see the data output of an ADXL345 accelerometer that is connected through SPI to an Arduino. When working within the Arduino IDE I can configure it to show X, Y, and Z values for acceleration, but how do I do this within Matlab? The circuit is set up as described on the Sparkfun website: https://learn.sparkfun.com/tutorials/adxl345-hookup-guide
So far I have added the Arduino as a device and have initiated a connection with the below code:
a=arduino;
dev=spidev(a,'D10')
I am not sure what the next step would be to collect the acceleration data. Any advice is appreciated, thanks!

Answers (1)

Madhu Govindarajan
Madhu Govindarajan on 9 Nov 2017
Here is a link on how to communicate with external sensors using SPI - https://www.mathworks.com/help/supportpkg/arduinoio/examples/communicate-with-spi-device-on-arduino-hardware.html
From this it looks like they are writing a command and immediately reading the SPI device (which might be necessary for your sensor too). Compare both the spec sheets and you should be able to get going easily.
HTH, Madhu
  1 Comment
Damian Krzaszcz
Damian Krzaszcz on 22 Jan 2022
I have a similar problem.
I followed the instructions in the link.
Here's a snippet of my code:
a = arduino ('COM3', 'Uno', 'Libraries', 'SPI');
ADXL345 = device (a, 'SPIChipSelectPin', 'D10');
data = writeRead (ADXL345, [1, hex2dec ('F2'), 0]);
I created an SPI device, then with the writeRead function I tried to read the registers from 0x31 to 0x37, but to no avail.
In the documentation for the sensor I found that to read these registers in the SPI standard, you need to send the value 0xF2 in the byte sent from the Master, and then the X Y Z registers are received from the slave..
How to solve this problem?

Sign in to comment.

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!