I am trying to connect an ADXL355 digital sensor to an Arduino. The wiring and the code I think are OK because the system returns the correct values sometimes and partly correct answers mostly. The code checks the device register, and the correct data is printed to serial most of the time. The code also checks for temperature but only returns the correct value occasionally. The values are polled every few seconds. The values are correct all of the time if they are correct the first time after restart but never if they are incorrect on restart. I thought it might help to unplug the sensor when uploading the program or powering up the Arduino through USB to the computer but there is no clear pattern.
It is not a defective sensor because I have 2 that behave the same way. What could be the problem? Is this a problem with the Arduino?
I only wire VDD, VDDIO, ground,chip select, sclk, MOSI and MISO.
code:
#include <SPI.h>
const int ID = 0x00;
const int FIFO = 0x11;
const int TEMP = 0x06;
const int TEMP1 = 0x07;
const byte FIFO_WATERMARK = 0x29;
const int chipSelectPin = 7;
void setup() {
Serial.begin(9600);
SPI.begin();
pinMode(dataReadyPin, INPUT);
pinMode(chipSelectPin, OUTPUT);
// writing 0 to to enable sensor
writeRegister(0x2D, 0x00);
delay(100);
Serial.println(readRegister(ID));
Serial.println(readRegister(FIFO_WATERMARK));
}
void loop() {
int temp = (readRegister(TEMP)); // << 8) | (readRegister(TEMP1))) ;
temp = (temp<<8)|(readRegister(TEMP1));
Serial.println(((1852 - temp)/9.05) + 19.21);
Serial.println(readRegister(ID));
delay(1000);
}
byte readRegister (byte thisRegister){
byte inByte = 0 ;
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); // 500khz clock
digitalWrite(chipSelectPin, LOW);
SPI.transfer((thisRegister << 1) | 1);
inByte = SPI.transfer(0x00);
digitalWrite(chipSelectPin, HIGH);
return inByte;
}
void writeRegister (byte thisRegister, byte value){
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); // 500khz clock
digitalWrite(chipSelectPin, LOW);
SPI.transfer(thisRegister << 1);
SPI.transfer(value);
digitalWrite(chipSelectPin, HIGH);
}
Some one suggested it might be signal bounce on the SPI and the clock rate was set to 500kHz but it didn't work.
Update:
Not long ago, the correct device ID would be read consistently (173 for 0xAD); now that returns 0. The temperature would either return 0 from the register or changing values between -1 and -5 that may have been wrongly calibrated temperature. But sometimes the correct temperature would return. Now it also only returns 0 from the registers.