ADXL355 Data logging to sd card using SPI with and arduino uno

  accelerometer, arduino, c++, logging

I just started this project and I’m trying to log the data from an accelerometer (ADXL355) to a SD card. Sadly it’s not working.

I can send data to the SD card without having the accelerometer connected and i can read the data on the serial monitor when the SD card is not connected but i can’t use both. so when both are connected, the SD card will not write anything and the serial monitor will read the data as 0.

I found a post on the arduino forum about data logging with a ADXL355. Sadly I faced the same problem as he had. I tried his code. i stripped it down so it would only show the data from the ADXL355 and wouldn’t have any code for the data logging part. From there i tried every possible way to add the data logging function but i just can’t figure out what’s wrong.

So i hope someone can help me with this problem. The accelerometer is pretty new so I’ve looked everywhere on the internet but i really can’t find anything about it. so i hope some experienced user can help me.

(running the ADXL355 in i2c isn’t working either same guy i got the code from tried that as well.) I’m pretty sure the connections are done right. So i hope someone can help me. Thank you in advance.

Pin outs:

vcc(sd) - 5V
vcc(accelermeter) - 3.3V
sd_CS - d8
accelermeter_CS -> d10
mosi(sd、accelermeter) - d11(mosi)
miso(sd、accelermeter) - d12(miso)
sclk(sd、accelermeter) - d13(sclk)
gnd(sd、accelermeter) - gnd

Datasheet ADXL355

User Guide ADXL355

The code:

#define DEVID_AD                 0x00
#define DEVID_MST                0x01
#define PARTID                   0x02
#define REVID                    0x03
#define STATUS                   0x04
#define FIFO_ENTRIES             0x05
#define TEMP2                    0x06
#define TEMP1                    0x07
#define XDATA3                   0x08
#define XDATA2                   0x09
#define XDATA1                   0x0A
#define YDATA3                   0x0B
#define YDATA2                   0x0C
#define YDATA1                   0x0D
#define ZDATA3                   0x0E
#define ZDATA2                   0x0F
#define ZDATA1                   0x10
#define FIFO_DATA                0x11
#define OFFSET_X_H               0x1E
#define OFFSET_X_L               0x1F
#define OFFSET_Y_H               0x20
#define OFFSET_Y_L               0x21
#define OFFSET_Z_H               0x22
#define OFFSET_Z_L               0x23
#define ACT_EN                   0x24
#define ACT_THRESH_H             0x25
#define ACT_THRESH_L             0x26
#define ACT_COUNT                0x27
#define FILTER                   0x28
#define FIFO_SAMPLES             0x29
#define INT_MAP                  0x2A
#define SYNC                     0x2B
#define RANGE                    0x2C
#define POWER_CTL                0x2D
#define SELF_TEST                0x2E
#define RESET                    0x2F

#include <SD.h>
#include <SPI.h>

const int SD_CS = 8;
const int acc_CS = 10; //
long values[20],t[9];
double x, y, z, c;
void setup() { 
  Serial.begin(9600);
 
  if (!SD.begin(SD_CS)) {
    Serial.println("Card failed, or not present");
  }
  pinMode(SD_CS, OUTPUT);
  digitalWrite(SD_CS, HIGH);
   
  SPI.setClockDivider(SPI_CLOCK_DIV16);
  SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0)); // 1000khz clock

  pinMode(acc_CS, OUTPUT);
  digitalWrite(acc_CS, HIGH);

  writeRegister(POWER_CTL, 0x00);  // writing 0 to to enable sensor
  writeRegister(RANGE, 0xC1);
  writeRegister(SELF_TEST, 0x00);  // writing 3 to to enable

  delay(500);
}
void loop() {
 
  acc_data();
 
  Serial.print(x,5);
  Serial.print(',');
  Serial.print(y,5);
  Serial.print(',');
  Serial.print(z,5);
  Serial.print('n');

  digitalWrite(SD_CS, LOW);
  File test0 = SD.open("test0.txt", FILE_WRITE);
  if (test0) {   
    Serial.println("writing");
    test0.print(x);
    test0.print(",");
    test0.print(y);
    test0.print(",");
    test0.print(z);
    test0.print('n');
    test0.close(); // close the file
  }
  // if the file didn't open, print an error:
  else {
    Serial.println("error opening test.txt");
  }
  digitalWrite(SD_CS, HIGH);
    delay(10);
}

byte readRegister (byte thisRegister) {
  byte inByte = 0 ;
  digitalWrite(acc_CS, LOW);
  SPI.transfer((thisRegister << 1) | 1);
  inByte = SPI.transfer(0x00);
  digitalWrite(acc_CS, HIGH);
  return inByte;
}
void writeRegister (byte thisRegister, byte value) {
  digitalWrite(acc_CS, LOW);
  SPI.transfer(thisRegister << 1);
  SPI.transfer(value);
  digitalWrite(acc_CS, HIGH);
}
void acc_data(){
  values[0] = readRegister(XDATA3);
  values[1] = readRegister(XDATA2);
  values[2] = readRegister(YDATA3);
  values[3] = readRegister(YDATA2);
  values[4] = readRegister(ZDATA3);
  values[5] = readRegister(ZDATA2);

  values[6] = readRegister(XDATA1);
  values[7] = readRegister(YDATA1);
  values[8] = readRegister(ZDATA1);

  t[0] = readRegister(TEMP2);
  t[1] = readRegister(TEMP1);

  x = (values[0] << 12) + (values[1]<<4) + (values[6]>>4);
  y = (values[2] << 12) + (values[3]<<4) + (values[7]>>4);
  z = (values[4] << 12) + (values[5]<<4) + (values[8]>>4);

  if (x >= 0x80000) {
    x = x-(2*0x80000);
  }
  if (y >= 0x80000) {
    y = y-(2*0x80000);
  }
  if (z >= 0x80000) {
    z = z-(2*0x80000);
  }
 
  c=(t[0]<<8)+t[1];
  c=((1852 - c)/9.05)+25.7;
 
  x=x*0.0000039;
  y=y*0.0000039;
  z=z*0.0000039;
  }

Source: Windows Questions C++

LEAVE A COMMENT