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?


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


#include <SPI.h>

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.setDataMode(SPI_MODE3); //configure accelerometer for SPI connecttion
  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("X: ");
  Serial.print(x, DEC);
  Serial.print("Y: ");
  Serial.print(y, DEC);
  Serial.print("Z: ");
  Serial.print(z, DEC); 

  //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("yg: ");
  Serial.print("zg: ");


//  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 
  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);   
  for(int i=0; i<numBytes; i++){
    values = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  • \$\begingroup\$ This should be referred to Arduino SE: arduino.stackexchange.com \$\endgroup\$ – akellyirl Mar 3 '14 at 16:51
  • 2
    \$\begingroup\$ @akellyirl the existence of a separate Arduino doesn't make all questions on Arduino off topic here directly. \$\endgroup\$ – user17592 Sep 17 '14 at 12:54
  • \$\begingroup\$ user38122, did you change the code which made that it didn't work anymore, or it just stopped working randomly? If the first, do you have the last working code (if you're using version control or just remember)? \$\endgroup\$ – user17592 Sep 17 '14 at 13:17
  • \$\begingroup\$ Looking at the Arduino sources, SPI::transfer returns a byte (aka uint8_t), though you assign it to a char *. The code you posted cannot compile (did you forgot to index variable `values'?), so either you put the wrong code here or you're not compiling the file you think it is. \$\endgroup\$ – Jarhmander Sep 20 '14 at 2:09

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?

  • \$\begingroup\$ It was working fine before. After that we able to access the registers, and change it to measurement mode. But the value is still X=0,Y=0,Z=0 constant \$\endgroup\$ – gzix Sep 23 '14 at 7:01
  • 1
    \$\begingroup\$ I'd recommend a voltage translator to be on the safe side. Other than that, I doubt you have "broken" it - from a hardware perspective, anyway... \$\endgroup\$ – Alan Campbell Sep 25 '14 at 7:14

Set the FIFO_CTL register to 0x80 then your adxl345 will populate the data in 0x32-0x37 registers

  • 4
    \$\begingroup\$ This sounds like it would be worth a shot although the answer would be better if you went into more details on what mode the value 0x80 sets etc. \$\endgroup\$ – PeterJ Jun 3 '14 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.