Struggling to get digital SPI accelerometer working (ADXL363)

Hi there,

I’m quite new to using Arduinos and have to use one for my research project, which currently involves implementing a digital acceleromter. I’ve never used digital sensors before and am a little confused as to how to start reading data. From what I can see from looking around, the accelerometer I’m using (ADXL363 - datasheet here) communicates using the SPI protocol - I am not familiar with this and am unsure as to where to go next to get it working. I am using an Arduino Due.

If anyone could lend me a hand that would be greatly appreciated!

Thanks in anticipation,

Luke

This should read the six axis registers byte values and combine these into three integer values. It uses the standard SPI library. You can use the Due extended stuff, but this keeps it compatible with other Arduino boards. Use D8 as the ADXL slave select pin.

You need not adjust any of the SPI settings. This IC uses all the default values (MSBFIRST, MODE0, 4MHz).

#include <SPI.h>

#define slaveSelect 8

byte xLow, xHi, yLow, yHi, zLow, zHi;
int xVal, yVal, zVal;

void setup() {
  Serial.begin(9600);
  pinMode(slaveSelect, OUTPUT);
  digitalWrite(slaveSelect, HIGH);

  SPI.begin();
}

void loop() {
  delay(1000);

  readRegisters();

  xVal = xHi << 8 | xLow;
  yVal = yHi << 8 | yLow;
  zVal = zHi << 8 | zLow;

  Serial.print(xVal);
  Serial.print(", ");
  Serial.print(yVal);
  Serial.print(", ");
  Serial.println(zVal);
}

void readRegisters() {

  // activate ADXL SPI
  digitalWrite(slaveSelect, LOW);

  // transfer a read command
  SPI.transfer(0x0B);

  // transfer the start register address. 0x0E is the start of the accelerometer registers
  SPI.transfer(0x0E);

  // get all the registers
  xLow = SPI.transfer(0x00);
  xHi = SPI.transfer(0x00);
  yLow = SPI.transfer(0x00);
  yHi = SPI.transfer(0x00);
  zLow = SPI.transfer(0x00);
  zHi = SPI.transfer(0x00);

  // disable the ADXL SPI
  digitalWrite(slaveSelect, HIGH);
}

No guarantees. I don’t have this model accelerometer to test this with.

Hi there,

Thanks very much for your help - I've tried running this code with MISO, MOSI, SCK and SS plugged in 50, 51, 53 and 8 respectively, but the serial monitor just outputs '65335, 65335, 65335' repeatedly. I don't suppose you know what's causing this? I tried changing the registry read code to the following:

  xLow = SPI.transfer(slaveSelect,0x00);
  SPI.transfer(slaveSelect,0xF);
  xHi = SPI.transfer(slaveSelect,0x00);
  SPI.transfer(slaveSelect,0x10);
  yLow = SPI.transfer(slaveSelect,0x00);
  SPI.transfer(slaveSelect,0x11);
  yHi = SPI.transfer(slaveSelect,0x00);
  SPI.transfer(slaveSelect,0x12);
  zLow = SPI.transfer(slaveSelect,0x00);
  SPI.transfer(slaveSelect,0x13);
  zHi = SPI.transfer(slaveSelect,0x00);

This changed the output to '255, 255, 255' which still isn't particularly helpful. Any help would be greatly appreciated!

Best wishes,

Luke

I don't have a Due yet, but hope to purchase one soon to upgrade my Mega.

The Due product page says the SPI pins are on the ICSP connector. It does not mention anything about D50 to D53.

edit: The code I posted will use the ADXL default stuff. 2G, 100Hz update.

edit2: The Due labels the old ICSP pins as SPI. Those are the pins you want to use.

The new ICSP pins for the Due are located in a different position, close to the power jack.