AnsweredAssumed Answered

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

Question asked by niclkn on Mar 3, 2014
Latest reply on Mar 4, 2014 by niclkn

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){
  char address = 0x80 | registerAddress; // to perform read operation the most significant bit of the register address must be set
  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);
}

Outcomes