I am trying to get data from an ADXL345 accelerometer using an ESP32 via SPI communication. I have configured my code following the ADXL345_WE library specifications and have verified the hardware connections, but I am having trouble getting valid data from the sensor.
The problem is that even though I have correctly configured the sensor and established SPI communication, the values I get are always zero, even when I move the sensor.
...
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
DATA_END
3195 Sample(s) missed
RMS : X 0.024839 Y nan Z 0.000000
Beginning capture.
...
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
DATA_END
3195 Sample(s) missed
RMS : X 0.024839 Y nan Z 0.000000
Beginning capture.
Aquí está mi código:
/***************************************************************************
* Example sketch for the ADXL345_WE library
*
* This sketch shows how use SPI for the basic data sketch. Please apply the
* same steps in the other sketches if you want to use SPI.
*
* Further information can be found on:
* https://wolles-elektronikkiste.de/adxl345-teil-1 (German)
* https://wolles-elektronikkiste.de/en/adxl345-the-universal-accelerometer-part-1 (English)
*
***************************************************************************/
#include<ADXL345_WE.h>
#include<SPI.h>
#define CS_PIN 5 // Chip Select Pin
#define NUM_SAMPLES 3200
bool spi = true; // flag indicating that SPI shall be used
xyzFloat samples[NUM_SAMPLES];
ADXL345_WE myAcc = ADXL345_WE(CS_PIN, spi);
const int int2Pin = 22; // interrupt pin
volatile bool dataReady = false;
void setup(){
Serial.begin(921600);
Serial.println("ADXL345 Demo");
if(!myAcc.init()){
Serial.println("ADXL345 not connected!");
}
// Prepare int pin for input
pinMode(int2Pin, INPUT);
/* You can set the SPI clock speed. Default is 5 MHz. */
// myAcc.setSPIClockSpeed(4000000);
/* Choose the data rate Hz
ADXL345_DATA_RATE_3200 3200
ADXL345_DATA_RATE_1600 1600
ADXL345_DATA_RATE_800 800
ADXL345_DATA_RATE_400 400
ADXL345_DATA_RATE_200 200
ADXL345_DATA_RATE_100 100
ADXL345_DATA_RATE_50 50
ADXL345_DATA_RATE_25 25
ADXL345_DATA_RATE_12_5 12.5
ADXL345_DATA_RATE_6_25 6.25
ADXL345_DATA_RATE_3_13 3.13
ADXL345_DATA_RATE_1_56 1.56
ADXL345_DATA_RATE_0_78 0.78
ADXL345_DATA_RATE_0_39 0.39
ADXL345_DATA_RATE_0_20 0.20
ADXL345_DATA_RATE_0_10 0.10
*/
myAcc.setDataRate(ADXL345_DATA_RATE_3200);
// Safety delay
delay(100);
Serial.print("Data rate: ");
Serial.print(myAcc.getDataRateAsString());
/* In full resolution the size of the raw values depend on the
range: 2g = 10 bit; 4g = 11 bit; 8g = 12 bit; 16g =13 bit;
uncomment to change to 10 bit for all ranges.
*/
// myAcc.setFullRes(false);
/* Choose the measurement range.
ADXL345_RANGE_16G 16g
ADXL345_RANGE_8G 8g
ADXL345_RANGE_4G 4g
ADXL345_RANGE_2G 2g
*/
myAcc.setRange(ADXL345_RANGE_4G);
Serial.print(" / g-Range: ");
Serial.println(myAcc.getRangeAsString());
Serial.println();
// Define interrupt routine and move data_ready to adxl int2
attachInterrupt(digitalPinToInterrupt(int2Pin), dataReadyISR, RISING);
myAcc.setInterrupt(ADXL345_DATA_READY, INT_PIN_2);
myAcc.readAndClearInterrupts();
// TODO add calibration routine
}
void loop() {
int missed = 0;
Serial.println("Beginning capture.");
// Begin data acq
for(int cnt = 0; cnt < NUM_SAMPLES; cnt++) {
// Wait for new data
while(!dataReady) {}
dataReady = false;
samples[cnt] = myAcc.getGValues();
if (dataReady) {
// new data received before we were ready to process it => probable miss
missed ++;
}
}
Serial.println("Capture done.");
Serial.println("Removing DC");
processRemoveDC();
Serial.println("DATA_START");
Serial.println("X\tY\tZ");
for(int cnt = 0; cnt < NUM_SAMPLES; cnt++) {
Serial.printf("%f\t%f\t%f\n", samples[cnt].x,samples[cnt].y,samples[cnt].z);
}
Serial.println("DATA_END");
Serial.printf("%d Sample(s) missed\n", missed);
printRMS();
delay(5000);
}
/* Removes the continuous (dc) component
This will operate inplace and edit the acquired data !
*/
void processRemoveDC() {
float meanX, meanY, meanZ = 0;
for (int cnt = 0; cnt < NUM_SAMPLES; cnt++) {
meanX += samples[cnt].x;
meanY += samples[cnt].y;
meanZ += samples[cnt].z;
}
meanX /= (float) NUM_SAMPLES;
meanY /= (float) NUM_SAMPLES;
meanZ /= (float) NUM_SAMPLES;
for (int cnt = 0; cnt < NUM_SAMPLES; cnt++) {
samples[cnt].x -= meanX;
samples[cnt].y -= meanY;
samples[cnt].z -= meanZ;
}
}
/* Computes and print the RMS per axis
*/
void printRMS() {
float rmsX, rmsY, rmsZ = 0;
for (int cnt = 0; cnt < NUM_SAMPLES; cnt++) {
rmsX += pow(samples[cnt].x, 2);
rmsY += pow(samples[cnt].y, 2);
rmsZ += pow(samples[cnt].z, 2);
}
rmsX = sqrt(rmsX/(float)NUM_SAMPLES);
rmsY = sqrt(rmsY/(float)NUM_SAMPLES);
rmsZ = sqrt(rmsZ/(float)NUM_SAMPLES);
Serial.printf("RMS : X %f\tY %f\tZ %f\n", rmsX, rmsY, rmsZ);
}
void dataReadyISR(){
dataReady = true;
}
Here is my wiring:
Here's my realization:
I have tried debugging the code and checking the hardware connections, but have not been able to find the problem. Is there something I am overlooking or any settings I need to adjust to get valid data from the sensor?
Any help or suggestions would be greatly appreciated. thanks!

