I have tried different libraries , and the used the pinout as described in the manual. when I use with other board it works well.
and the output is
I aslo tried ADXL345_WE library to no avail
HTML:
#include <SPI.h>
#define CS_PIN 10 // Chip Select Pin for ADXL345
// ADXL345 Register Addresses
#define ADXL345_DEVID 0x00
#define ADXL345_POWER_CTL 0x2D
#define ADXL345_DATA_FORMAT 0x31
#define ADXL345_DATAX0 0x32
#define ADXL345_DATA_RATE 0x2C
// ADXL345 Data Rate Codes
#define ADXL345_DATA_RATE_3200 0x0F
// ADXL345 Range Codes
#define ADXL345_RANGE_4G 0x01
SPISettings adxlSpiSettings(8000000, MSBFIRST, SPI_MODE3);
void setup() {
Serial.begin(2000000);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
SPI.begin();
// Check if ADXL345 is connected
if (readRegister(ADXL345_DEVID) != 0xE5) {
Serial.println("ADXL345 not connected!");
while (1)
;
}
// Set data rate to 3200 Hz
writeRegister(ADXL345_DATA_RATE, ADXL345_DATA_RATE_3200);
// Set range to ±4g and enable FULL_RES
writeRegister(ADXL345_DATA_FORMAT, 0x08 | ADXL345_RANGE_4G);
// Set the ADXL345 to measurement mode
writeRegister(ADXL345_POWER_CTL, 0x08);
}
void loop() {
int16_t x, y, z;
// Read acceleration data
readAcceleration(&x, &y, &z);
// Convert raw acceleration data to m/s^2
float x_accel_m_s2 = x * 3.9 / 1000.0 * 9.80665;
float y_accel_m_s2 = y * 3.9 / 1000.0 * 9.80665;
float z_accel_m_s2 = z * 3.9 / 1000.0 * 9.80665;
Serial.print("X: ");
Serial.print(x_accel_m_s2);
Serial.print(" m/s^2, Y: ");
Serial.print(y_accel_m_s2);
Serial.print(" m/s^2, Z: ");
Serial.print(z_accel_m_s2);
Serial.println(" m/s^2");
delay(10);
}
void writeRegister(byte reg, byte value) {
SPI.beginTransaction(adxlSpiSettings);
digitalWrite(CS_PIN, LOW);
SPI.transfer(reg | 0x40);
SPI.transfer(value);
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
}
byte readRegister(byte reg) {
byte value;
SPI.beginTransaction(adxlSpiSettings);
digitalWrite(CS_PIN, LOW);
SPI.transfer(reg | 0x80);
value = SPI.transfer(0x00);
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
return value;
}
void readAcceleration(int16_t* x, int16_t* y, int16_t* z) {
byte buffer[6];
SPI.beginTransaction(adxlSpiSettings);
digitalWrite(CS_PIN, LOW);
SPI.transfer(ADXL345_DATAX0 | 0x80 | 0x40);
for (int i = 0; i < 6; i++) {
buffer[i] = SPI.transfer(0x00);
}
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
*x = ((int16_t)buffer[1] << 8) | buffer[0];
*y = ((int16_t)buffer[3] << 8) | buffer[2];
*z = ((int16_t)buffer[5] << 8) | buffer[4];
}
and the output is
HTML:
X: 0.00 m/s^2, Y: 0.00 m/s^2, Z: 0.00 m/s^2
X: 0.00 m/s^2, Y: 0.00 m/s^2, Z: 0.00 m/s^2
X: 0.00 m/s^2, Y: 0.00 m/s^2, Z: 0.00 m/s^2
X: 0.00 m/s^2, Y: 0.00 m/s^2, Z: 0.00 m/s^2
X: 0.00 m/s^2, Y: 0.00 m/s^2, Z: 0.00 m/s^2
X: 0.00 m/s^2, Y: 0.00 m/s^2, Z: 0.00 m/s^2
X: 0.00 m/s^2, Y: 0.00 m/s^2, Z: 0.00 m/s^2
I aslo tried ADXL345_WE library to no avail