Regarding SPI interface with sensor

Hi, I have been trying to interface a sensor based on SPI communication. The pinout connections are right and the sensor is even responding, but is giving garbage values when trying to read. Am attaching the datasheet and the instructions provided by the vendor. This information is not publicly available and confidential, that’s why some parts have been removed. Apologies for that.

I’ve mostly followed the correct steps as mentioned by the datasheet (IMO) and some references I picked up on the internet.

I’m using the sensor in normal (low speed mode) as is default and the firmware for the high speed mode is not yet available. In the command set, I am trying to read the Module ID and it responds with 255255255255255255255… and this is the code I could come up within a day. Datasheet is attached for your reference. The device connects and responds to DRDY pin as 1 which means the sensor is active, but everything after that is haywire to me. Thanks

#include <SPI.h>

#define SS_PIN 10
#define MISO_PIN 12
#define MOSI_PIN 11
#define SCK_PIN 13

int c=0;

void setup (void) 
{
   Serial.begin(9600); //set baud rate to 9600 for usart
    pinMode(MOSI_PIN, OUTPUT);
    pinMode(MISO_PIN, INPUT);
    pinMode(SCK_PIN, OUTPUT);
    pinMode(SS_PIN, OUTPUT);
    digitalWrite(SS_PIN, HIGH); // disable Slave Select
    
   SPI.begin ();
   SPI.setDataMode(SPI_MODE0);
   SPI.setClockDivider(SPI_CLOCK_DIV128);

   //setup initialization for device
   digitalWrite(2-3, HIGH);// enable the Module 
   delayMicroseconds(50000);
   c = digitalRead(4);// Check DRDY status to see if system is on
   if(c == 1)
    {
      Serial.println("Success");
      work();
      delayMicroseconds(50000); 
    }
    
   else 
   {
      Serial.println("Error establishing Connection with Module");
      Serial.println(digitalRead(4));
      setup();
   }
   
}

void work (void)
{
  SPI.beginTransaction (SPISettings (1000000, MSBFIRST, SPI_MODE0));
  digitalWrite(SS_PIN, LOW); // enable Slave Select
  
  SPI.transfer(0x80); // Write 10000000 (1 for write and address is seven 0s)
  SPI.transfer(0x00); //dummy bytes for setting response width (64 bits)
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  SPI.transfer(0x00);
    
  byte aa = SPI.transfer(0x80);
  byte bb = SPI.transfer(0x00);
  byte cc = SPI.transfer(0x00);
  byte dd = SPI.transfer(0x00);
  byte ee = SPI.transfer(0x00);
  byte ff = SPI.transfer(0x00);
  byte gg = SPI.transfer(0x00);
  byte hh = SPI.transfer(0x00);
  byte ii = SPI.transfer(0x00);
  byte jj = SPI.transfer(0x00);

  Serial.print(aa);
  Serial.print(bb);
  Serial.print(cc);
  Serial.print(dd);
  Serial.print(ee);
  Serial.print(ff);
  Serial.print(gg);
  Serial.print(hh);
  Serial.print(ii);
  Serial.print(jj);
  
  digitalWrite(SS_PIN, HIGH);
  SPI.endTransaction;
}
void loop(void){}

datasheet_2.pdf (565 KB)

You are throwing away the data you require. SPI is full duplex, each SPI.transfer() call exchanges a byte, so save the results. You are using twice as many byte exchanges and throwing away the results from the first set...

Thanks for the reply Mark. The datasheet says that data will be available to read from the third byte onwards. Does this require change in the code or should I just remove the first set of SPI.transfer?

Update: I tried removing the first set, but all I get are still junk values like 255 or 0.

The sensor gives 191255255255255.... now. Don't know what its supposed to mean. Tried contacting the vendor but the hardware support they provide is not very good. Hope you guys can help. The supplier just says datasheet is key.

void work (void)
{
  SPI.beginTransaction (SPISettings (1000000, MSBFIRST, SPI_MODE0));
  digitalWrite(SS_PIN, LOW); // enable Slave Select
    
  byte aa = SPI.transfer(0x80);
  byte bb = SPI.transfer(0x00);
  byte cc = SPI.transfer(0x00);
  byte dd = SPI.transfer(0x00);
  byte ee = SPI.transfer(0x00);
  byte ff = SPI.transfer(0x00);
  byte gg = SPI.transfer(0x00);
  byte hh = SPI.transfer(0x00);
  byte ii = SPI.transfer(0x00);
  byte jj = SPI.transfer(0x00);
  delay(100);

  Serial.print(aa);
  Serial.print(bb);
  Serial.print(cc);
  Serial.print(dd);
  Serial.print(ee);
  Serial.print(ff);
  Serial.print(gg);
  Serial.print(hh);
  Serial.print(ii);
  Serial.print(jj);
  
  digitalWrite(SS_PIN, HIGH);
  SPI.endTransaction;
}