I am working on getting vibration data from ADXL359z accelerometer but the data that i get is wrong. I using EVAL-ADXL359z with raspberry pi 4 model B and fetching data via SPI.
So when i fetch the raw values for all 3 axis and keep any 1 axis inline with earths gravity, that value should be stagnant and other 2 axis should show change but all 3 axis values change in a very adhoc manner.
Below is the Arduino code i am using to fetch the data,, it is library for ADXL355 but both the sensor are pin to pin compatible and have same registers.
Also what formula should i use to convert the raw data into g.
/*
ADXL355-PMDZ Accelerometer Sensor Display
Shows the output of a ADXL355 accelerometer.
Uses the SPI library. For details on the sensor, see:
ADXL355 Low Noise, Programmable ±2g, ±4g, and ±8g Accelerometer PMOD [Analog Devices Wiki]
Created 22 June 2018
by Gabriel Vidal
*/
#include <SPI.h>
// Memory register addresses:
const int XDATA3 = 0x08;
const int XDATA2 = 0x09;
const int XDATA1 = 0x0A;
const int YDATA3 = 0x0B;
const int YDATA2 = 0x0C;
const int YDATA1 = 0x0D;
const int ZDATA3 = 0x0E;
const int ZDATA2 = 0x0F;
const int ZDATA1 = 0x10;
const int RANGE = 0x2C;
const int POWER_CTL = 0x2D;
const int REG_RESET = 0x2F;
const int REG_DEVID_AD = 0x00 ; // 0xAD
const int REG_DEVID_MST = 0x01 ; // 0x1D
const int REG_PART_ID = 0x02 ; // 0xE9
const int REG_REVID = 0x03 ;// 0x01
const int REG_STATUS = 0x04;
// Device values
const int RANGE_10G = 0x01;
const int RANGE_20G = 0x02;
const int RANGE_40G = 0x03;
const int MEASURE_MODE = 0x06; // Only accelerometer
// Operations
const int READ_BYTE = 0x01;
const int WRITE_BYTE = 0x00;
// Pins used for the connection with the sensor
const int CHIP_SELECT_PIN = 53;
void setup() {
Serial.begin(9600);
SPI.begin();
// Initalize the data ready and chip select pins:
pinMode(CHIP_SELECT_PIN, OUTPUT);
//Configure ADXL359:
writeRegister(RANGE, RANGE_10G); // 10G
writeRegister(POWER_CTL, MEASURE_MODE); // Enable measure mode
// Give the sensor time to set up:
delay(100);
}
void loop() {
int axisAddresses[] = {XDATA1, XDATA2, XDATA3, YDATA1, YDATA2, YDATA3, ZDATA1, ZDATA2, ZDATA3};
int axisMeasures[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int dataSize = 9;
// Read accelerometer data
readMultipleData(axisAddresses, dataSize, axisMeasures);
// Split data
int xdata = (axisMeasures[0] >> 4) + (axisMeasures[1] << 4) + (axisMeasures[2] << 12);
int ydata = (axisMeasures[3] >> 4) + (axisMeasures[4] << 4) + (axisMeasures[5] << 12);
int zdata = (axisMeasures[6] >> 4) + (axisMeasures[7] << 4) + (axisMeasures[8] << 12);
// Apply two complement
if (xdata >= 0x80000) {
xdata = ~xdata + 1;
}
if (ydata >= 0x80000) {
ydata = ~ydata + 1;
}
if (zdata >= 0x80000) {
zdata = ~zdata + 1;
}
// Print axis
Serial.print("X=");
Serial.print(xdata);
Serial.print("\t");
Serial.print("Y=");
Serial.print(ydata);
Serial.print("\t");
Serial.print("Z=");
Serial.print(zdata);
Serial.print("\n");
// Next data in 100 milliseconds
delay(100);
}
/*
* Write registry in specific device address
*/
void writeRegister(byte thisRegister, byte thisValue) {
byte dataToSend = (thisRegister << 1) | WRITE_BYTE;
digitalWrite(CHIP_SELECT_PIN, LOW);
SPI.transfer(dataToSend);
SPI.transfer(thisValue);
digitalWrite(CHIP_SELECT_PIN, HIGH);
}
/*
* Read registry in specific device address
*/
unsigned int readRegistry(byte thisRegister) {
unsigned int result = 0;
byte dataToSend = (thisRegister << 1) | READ_BYTE;
digitalWrite(CHIP_SELECT_PIN, LOW);
SPI.transfer(dataToSend);
result = SPI.transfer(0x00);
digitalWrite(CHIP_SELECT_PIN, HIGH);
return result;
}
/*
* Read multiple registries
*/
void readMultipleData(int *addresses, int dataSize, int *readedData) {
digitalWrite(CHIP_SELECT_PIN, LOW);
for(int i = 0; i < dataSize; i = i + 1) {
byte dataToSend = (addresses[i] << 1) | READ_BYTE;
SPI.transfer(dataToSend);
readedData[i] = SPI.transfer(0x00);
}
digitalWrite(CHIP_SELECT_PIN, HIGH);
}
have been at it for some time and couldn't figure out the issue.
Any help would be much appreciated.
thanks in advance