ADXL345 Through SPI

Hello,

I am new to the Forum, Arduino and Electronics in general, although for whatever reason my only "cum laude" while studying my degree was in electronics... :slight_smile:

Having said that, I have to say that I find the Arduino concept really amazing and I took the jump to start trying to make my own projects. Specifically, the one I am working on right now is trying to develop a modal analysis kit, with at least four accelerometers, easy to transfer and plug to any computer, and be able to obtain structural dynamic response from simple hammer tests or even operational vibration.

Being at the bottom of the learning curve, I opted for the Due and ADXL345 accelerometers, connected via SPI. I know now that it was not the best of the decissions, but once taken I have to make it work, even if it is only one accelerometer. As a backup plan I have ordered 4 analog ADXL335 that should make the trick.

Unfortunately, I am having issues with the SPI connection to the DUE. I can access the accelerometer data via I2C, but when trying to get to it through SPI i get nothing, only -1,-1,-1 and not even the device ID. I started trying to adapt the existing sketch from sparkfun for the due ADXL345 Hookup Guide - SparkFun Learn, but as it did not work I tried my low level implementation, which does not work either:

//Add the SPI library so we can communicate with the ADXL345 sensor
#include <SPI.h>

//Assign the Chip Select signal to pin 52.
int CS=52;

//This is a list of some of the registers available on the ADXL345.
//To learn more about these and the rest of the registers on the ADXL345, read the datasheet!
char POWER_CTL = 0x2D;	//Power Control Register
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32;	//X-Axis Data 0
char DATAX1 = 0x33;	//X-Axis Data 1
char DATAY0 = 0x34;	//Y-Axis Data 0
char DATAY1 = 0x35;	//Y-Axis Data 1
char DATAZ0 = 0x36;	//Z-Axis Data 0
char DATAZ1 = 0x37;	//Z-Axis Data 1

//This buffer will hold values read from the ADXL345 registers.
char values[10];
//These variables will be used to hold the x,y and z axis accelerometer values.
int16_t x,y,z;
byte buff;
void setup(){ 
  //Initiate an SPI communication instance.
  SPI.begin();
  SPI.setClockDivider(42);  
  //Configure the SPI connection for the ADXL345.
  SPI.setDataMode(SPI_MODE3);
  //Create a serial connection to display the data on the terminal.
  Serial.begin(9600);  
  //Set up the Chip Select pin to be an output from the Arduino.
  pinMode(CS, OUTPUT);
  //Before communication starts, the Chip Select pin needs to be set high.
  digitalWrite(CS, HIGH);
  delay(100);
  digitalWrite(CS, LOW);
  SPI.transfer(DATA_FORMAT);
  SPI.transfer(0x01);
  digitalWrite(CS, HIGH);
  delay(50);   
  digitalWrite(CS, LOW);
  SPI.transfer(POWER_CTL);
  SPI.transfer(0x08);
  digitalWrite(CS, HIGH);
  delay(50);   
}

void loop(){
  digitalWrite(CS, LOW);
  SPI.transfer(DATAX0);
  values[0] = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  delay(50);  
//  
  digitalWrite(CS, LOW);
  SPI.transfer(DATAX1);
  values[1] = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  delay(50);    
//  
  digitalWrite(CS, LOW);
  SPI.transfer(DATAY0);
  values[2] = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  delay(50);  
//  
  digitalWrite(CS, LOW);
  SPI.transfer(DATAY1);
  values[3] = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  delay(50);  
//
  digitalWrite(CS, LOW);
  SPI.transfer(DATAZ0);
  values[4] = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  delay(50);  
//
  digitalWrite(CS, LOW);
  SPI.transfer(DATAZ1);
  values[5] = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  delay(50);   
//  
  //The ADXL345 gives 10-bit acceleration values, but they are stored as bytes (8-bits). To get the full value, two bytes must be combined for each axis.
  //The X value is stored in values[0] and values[1].
  x = (((int)values[1]) <<8) | values[0];
  //The Y value is stored in values[2] and values[3].
  y = (((int)values[3]) <<8) | values[2];
  //The Z value is stored in values[4] and values[5].
  z = (((int)values[5]) <<8) | values[4]; 
  //Print the results to the terminal.
  Serial.print(x, DEC);
  Serial.print(',');
  Serial.print(y, DEC);
  Serial.print(',');
  Serial.println(z, DEC);   
  delay(1000); 
}

The accelerometer is connected to the SPI pins ( according to The 21st Century Digital Home: Arduino Due Hardware SPI), such that MOSI -> SDA; MISO ->SDO; SCK ->SCL.

Could anybody help me to understand what might be going on?

Thanks a lot and best regards.

Also refer to the extensed SPI calls for Due at http://arduino.cc/en/Reference/DueExtendedSPI - for begin you have an argument for which interface. You also need to use the third argument to SPI.write (SPI_CONTINUE) if writing multiple data values.

You should check the signal lines with an oscilloscope or logic analyzer if you have one to check to see if the lines are working.

Thanks for the response.

I tried the due extended approach too, but the result was the same. So I decided to take a step back and try to do it with simple commands...the result is unfortunately exact though.

I do not have an oscilloscope or anything like that. Is there any other way I can monitor what signals are going through those lines?. Maybe another arduino (I have bought a nano to play with too, but do not have it yet).

Regards.

Maybe this gives somebody an extra clue on what might be going on:

I have used succesfully a SD card module I bought. So I guess that discards me using the wrong pins (or in the wrong order).

However, when I attach the ADLX345 SDA to the MOSI, the SCL to the SCK and the SDO to the MISO lines, I can not talk to the SD card anymore. Any clue why this might be happening?

Thanks a lot!

hi,
I used your code(with just a few changes) for my ADXL345, and it works!
i guess your reading addresses are incorrect, because you have set the first bit while reading from
a register. for example the Address for DATAX0 which is 0x32, will change into 0xB2 and so on...
got it?

sorry i meant that you have to set the first bit! :slight_smile:

Today, I also wanted to make the ADXL345 work on the SPI.

I am using a Arduino Nano V3.0

Arduino Pin ADXL345
pin 13 SCK -> SCL
pin 12 MISO -> SDO
pin 11 MOSI -> SDA
pin 8 CS -> CS

Here is the code I use, it works.
The code comes from ADXL345 Hookup Guide - SparkFun Learn

//Add the SPI library so we can communicate with the ADXL345 sensor
#include <SPI.h>

//https://www.sparkfun.com/tutorials/240
//http://forum.arduino.cc/index.php/topic,159313.0.html

//Assign the Chip Select signal to pin 8.
int CS=8;

//This is a list of some of the registers available on the ADXL345.
//To learn more about these and the rest of the registers on the ADXL345, read the datasheet!
char POWER_CTL = 0x2D;	//Power Control Register
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32;	//X-Axis Data 0
char DATAX1 = 0x33;	//X-Axis Data 1
char DATAY0 = 0x34;	//Y-Axis Data 0
char DATAY1 = 0x35;	//Y-Axis Data 1
char DATAZ0 = 0x36;	//Z-Axis Data 0
char DATAZ1 = 0x37;	//Z-Axis Data 1

//This buffer will hold values read from the ADXL345 registers.
unsigned char values[10];
//These variables will be used to hold the x,y and z axis accelerometer values.
int x,y,z;

void setup(){ 
  //Initiate an SPI communication instance.
  SPI.begin();
  //Configure the SPI connection for the ADXL345.
  SPI.setDataMode(SPI_MODE3);
  //Create a serial connection to display the data on the terminal.
  Serial.begin(9600);
  
  //Set up the Chip Select pin to be an output from the Arduino.
  pinMode(CS, OUTPUT);
  //Before communication starts, the Chip Select pin needs to be set high.
  digitalWrite(CS, HIGH);
  
  //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
  writeRegister(DATA_FORMAT, 0x01);
  //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
  writeRegister(POWER_CTL, 0x08);  //Measurement mode  
}

void loop(){
  //Reading 6 bytes of data starting at register DATAX0 will retrieve the x,y and z acceleration values from the ADXL345.
  //The results of the read operation will get stored to the values[] buffer.
  readRegister(DATAX0, 6, values);

  //The ADXL345 gives 10-bit acceleration values, but they are stored as bytes (8-bits). To get the full value, two bytes must be combined for each axis.
  //The X value is stored in values[0] and values[1].
  x = ((int)values[1]<<8)|(int)values[0];
  //The Y value is stored in values[2] and values[3].
  y = ((int)values[3]<<8)|(int)values[2];
  //The Z value is stored in values[4] and values[5].
  z = ((int)values[5]<<8)|(int)values[4];
  
  //Print the results to the terminal.
  Serial.print(x, DEC);
  Serial.print(',');
  Serial.print(y, DEC);
  Serial.print(',');
  Serial.println(z, DEC);      
  delay(10); 
}

//This function will write a value to a register on the ADXL345.
//Parameters:
//  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);
  //Transfer the register address over SPI.
  SPI.transfer(registerAddress);
  //Transfer the desired register value over SPI.
  SPI.transfer(value);
  //Set the Chip Select pin high to signal the end of an SPI packet.
  digitalWrite(CS, HIGH);
}

//This function will read a certain number of registers starting from a specified address and store their values in a buffer.
//Parameters:
//  char registerAddress - The register addresse to start the read sequence from.
//  int numBytes - The number of registers that should be read.
//  char * values - A pointer to a buffer where the results of the operation should be stored.
void readRegister(char registerAddress, int numBytes, unsigned char * values){
  //Since we're performing a read operation, the most significant bit of the register address should be set.
  char address = 0x80 | registerAddress;
  //If we're doing a multi-byte read, bit 6 needs to be set as well.
  if(numBytes > 1)address = address | 0x40;
  
  //Set the Chip select pin low to start an SPI packet.
  digitalWrite(CS, LOW);
  //Transfer the starting register address that needs to be read.
  SPI.transfer(address);
  //Continue to read registers until we've read the number specified, storing the results to the input buffer.
  for(int i=0; i<numBytes; i++){
    values[i] = SPI.transfer(0x00);
  }
  //Set the Chips Select pin high to end the SPI packet.
  digitalWrite(CS, HIGH);
}

For your original code to work, you need to change the adress. Exactly like niloo said.

char DATAX0 = 0xB2;	//X-Axis Data 0
char DATAX1 = 0xB3;	//X-Axis Data 1
char DATAY0 = 0xB4;	//Y-Axis Data 0
char DATAY1 = 0xB5;	//Y-Axis Data 1
char DATAZ0 = 0xB6;	//Z-Axis Data 0
char DATAZ1 = 0xB7;	//Z-Axis Data 1

With those modification, the original code you posted works.

Thanks

I think I just broke my ADXL345 board... The code does not make it work anymore. I get values but they don't change.
I just tried using I2C and the sensor does not even initialize. My CS is at 5V for I2C.

I have a 3,3V and 5V arduino. For SPI, I have been using the ADXL345 on 5V. Vcc was on 3.3V but not the I/O...
My I2C code was working fine and everything was on 3.3V.

hum... I will take a break and try with a logic analyzer an other day.

I've been working on this for some months and I succeed connecting 2 ADXL345 over i2c, it's a shame to have only 2 adresses for that.
Anyway, i want to connect about 8 sensors and that's why I need SPI. I thought it was only needed to change the CS between the sensors but as I see is more than that.

I didn't understand the adresses change you said. The original adresses posted by rodiaIt works fine for me with one sensor.
For what is this adresse change ?

char DATAX0 = 0xB2;	//X-Axis Data 0
char DATAX1 = 0xB3;	//X-Axis Data 1
char DATAY0 = 0xB4;	//Y-Axis Data 0
char DATAY1 = 0xB5;	//Y-Axis Data 1
char DATAZ0 = 0xB6;	//Z-Axis Data 0
char DATAZ1 = 0xB7;	//Z-Axis Data 1

This thread could use the info on ICSP pin map...

Saved me, while using a Leonardo. Using the labeled SCL and SDA pins do not work, or setting numbered pins for input :frowning:

SCK -> SCL
MISO -> SDO
MOSI -> SDA
pin 8 -> CS

Pin layout...