ADXL357 library

Can anyone help with the ADXL357z library to read the accelerometer data?

it may be worth trying the adxl355 library as a starting point

1 Like

Many thanks for your response. Below are the evaluation board, schematic and the Wroom D1 mini


I am using. I am sceptical about the pinout because I don't want to damage the board. I need assistance with the SPI pinout.

adxl357-accelerometer-3-axis-sensor-evaluation-board-evaluation

could you upload a schematic showing how you connected the ADXL357 to the Wroom D1 mini?
did you try the library for the ADXL355?

Wroom D1 mini// ADXL357

3V3 // VDD
3V3 // VDDIO
GND // GND
GPI05 // CS/SCL
GPI023 // MOSI/SDA
GPI019 //MISO/ASEL

I have not tried the ADXL355 code yet. I wanted to be sure of the connection.

looks like you are connecting it up as an SPI device
you need to connect the SCK (clock) Wroom D1 pin GPIO18 to the SCLK signal of the adxl357
the library I linked too in post #2 appears to be SPI based

Thanks thus far. Unfortunately, it is not working . Maybe I have done something wrong. I have attached the code and the monitor output.

#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;

// Device values
const int RANGE_2G = 0x01;
const int RANGE_4G = 0x02;
const int RANGE_8G = 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 = 7;

void setup() {
  Serial.begin(921600);
  SPI.begin();

  // Initalize the  data ready and chip select pins:
  pinMode(CHIP_SELECT_PIN, OUTPUT);

  //Configure ADXL355:
  writeRegister(RANGE, RANGE_2G); // 2G
  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);
}

your output looks like wrong baud rate somewhere
what Arduino are you using?
I would reduce this

  Serial.begin(921600);

to 115200 and make sure your serial monitor is set to 115200

Thanks, I have changed the baudrate but I can't get the accelerometer data.
sim2

I would expect to see some printout from your program in the serial monitor even if the device was not operational
put a Serial.println("program stating"); in setup() so you can see the basic serial monitor is working
also some prints at various points in to program to print intermediate data and you can see the program flow

I changed the chip select pin to 5, and the range to 2G is still not able to read the correct accelerometer values.

Any suggestion would be highly appreciated

your post #8 shows you are attempting to write the ADXL357 code yourself
SPI can be difficult to set up correctly - I would have used I2C
did you try the ADXL355 library?

Thanks , Horace[quote="horace, post:2, topic:1058401, full:true"]
it may be worth trying the adxl355 library as a starting point
[/quote]

It is working now, and I changed the I2C address to 29.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.