Electronic – Arduino UNO and ADXL345 getting 0 0 0 OR -1 -1 -1 outputs with SPI communication

accelerometerarduinospi

I have EVAL-ADXL345Z evaluation board from Analog Device hooked up to the Arduino UNO R3, the code complies and runs fine. At first I could still get the output values, but after sometime the output values i get is either 0 0 0 OR -1 -1 -1. Is it any problem with ADXL345 or Arduino UNO? Did I damage the sensor?

Connection:

ADXL345 --> Arduino
VIO     --> 3.3V
GND     --> GND
CS      --> Pin 10
VS      --> 3.3V
SCL     --> Pin 13
SDA     --> Pin 11
SDO     --> Pin12
INT1    --> Pin 8

Code:

#include <SPI.h>
#define PINNUMBER

int CS=10;
char values[10];

int x,y,z; 
double xg, yg, zg; 
char ff=0;
long line = 0;

#define   DEVID      0x00   //Device ID Register
#define   OFSX      0x1E   //X-axis offset
#define   OFSY      0x1F   //Y-axis offset
#define   OFSZ      0x20   //Z-axis offset
#define   THRESH_ACT   0x24   //Activity Threshold
#define   THRESH_INACT   0x25   //Inactivity Threshold
#define   TIME_INACT   0x26   //Inactivity Time
#define   ACT_INACT_CTL   0x27   //Axis enable control for activity and inactivity detection
#define   THRESH_FF   0x28   //free-fall threshold
#define   TIME_FF      0x29   //Free-Fall Time
#define ACT_TAP_STATUS   0x2B   //Source of tap/double tap
#define   BW_RATE      0x2C   //Data rate and power mode control
#define POWER_CTL   0x2D   //Power Control Register
#define   INT_ENABLE   0x2E   //Interrupt Enable Control
#define   INT_MAP      0x2F   //Interrupt Mapping Control
#define   INT_SOURCE   0x30   //Source of interrupts
#define   DATA_FORMAT   0x31   //Data format control
#define DATAX0      0x32   //X-Axis Data 0
#define DATAX1      0x33   //X-Axis Data 1
#define DATAY0      0x34   //Y-Axis Data 0
#define DATAY1      0x35   //Y-Axis Data 1
#define DATAZ0      0x36   //Z-Axis Data 0
#define DATAZ1      0x37   //Z-Axis Data 1



void setup(){ 
  SPI.begin();
  SPI.setDataMode(SPI_MODE3); //configure accelerometer for SPI connecttion
  Serial.begin(9600);
  pinMode(CS, OUTPUT); //set chip select to be output
  digitalWrite(CS, HIGH); //set chip select to be high
  writeRegister(DATA_FORMAT, 0x03); //put accelerometer into 16G range
  writeRegister(POWER_CTL, 0x08);  //Measurement mode 
}

void loop(){
  readRegister(DATAX0, 6, values);

  x = ((int)values[1]<<8)|(int)values[0];
  y = ((int)values[3]<<8)|(int)values[2];
  z = ((int)values[5]<<8)|(int)values[4];

  line  = line + 1;
  Serial.print(line);
  Serial.print('-');
  Serial.print("X: ");
  Serial.print(x, DEC);
  Serial.print(',');
  Serial.print("Y: ");
  Serial.print(y, DEC);
  Serial.print(',');
  Serial.print("Z: ");
  Serial.print(z, DEC); 
  Serial.print(',');  
  delay(100); 

  //convert accelerometer value to G
  xg = x * 0.0078;
  yg = y * 0.0078;
  zg = z * 0.0078;

  //Print the results to the terminal 
  //so that i can monitor the reading using the serial monitor.

  Serial.print("xg: ");
  Serial.print((float)xg,2);
  Serial.print("g,");
  Serial.print("yg: ");
  Serial.print((float)yg,2);
  Serial.print("g,");
  Serial.print("zg: ");
  Serial.print((float)zg,2);
  Serial.println("g");
  delay(100); 

}

//  char registerAddress - The register to write a value to
//  char value - The value to be written to the specified register.
void writeRegister(char registerAddress, char value){
  //Set Chip Select pin low to signal the beginning of an SPI packet.
  digitalWrite(CS, LOW); // to signal beginning of SPI packet 
  SPI.transfer(registerAddress);
  SPI.transfer(value);
  digitalWrite(CS, HIGH); // to signal end of SPI packet
}

void readRegister(char registerAddress, int numBytes, char * values){
  // to perform read operation the most significant bit of the register address must be set
  char address = 0x80 | registerAddress; 
  if(numBytes > 1)address = address | 0x40;   
  digitalWrite(CS, LOW);   
  SPI.transfer(address);
  for(int i=0; i<numBytes; i++){
    values = SPI.transfer(0x00);
  }
  digitalWrite(CS, HIGH);
}

Best Answer

Does it start out OK, but degrade over time? Or did it work once, but now fails every time?
A ggogle search found this pdf, with handling considerations on the first page:
1. Not reverse polarity protected
2. Dropping on a hard surface can exceed Acceleration limits.

The main other options for breaking the sensor would be: static damage, or the Arduino IO exceeding 3.3V. The Data sheet here lists a voltage limit of 3.9V (page 6).
According to the UNO web page, the "official" output levels are 5V...

Does the R3 version have logic level translation? Did you use some form of voltage divider? Have you got a modified board, like discussed here?