SPI Data Read Question

Hi All, any help with my predicament would be awesome! I am trying to capture data from a 3D Hall sensor (Mikroelectrika Click Board) using SPI with an Arduino UNO. All of the wiring has been set-up correctly, and I just need help with data collection of the 8-byte packages now. This is the code I have so far:

#include <SPI.h> // Include the SPI Library

const int sckPin = 13; //Initialize the four pins used with the SPI 
const int misoPin = 12;
const int mosiPin = 11;
const int ssPin = 10;

int recArray[8]; //Creating array to caputre 8 Bytes of data to be coming from the sensor

void setup() {

 SPI.begin(); //Begin SPI usage
  
  pinMode(sckPin, OUTPUT); //Declare four pin modes
  pinMode(misoPin, INPUT); 
  pinMode(mosiPin, OUTPUT);
  pinMode(ssPin, OUTPUT);

  digitalWrite(ssPin, HIGH); //Define starting values for digital pins
  digitalWrite(mosiPin, HIGH);
  digitalWrite(sckPin, LOW);
  digitalWrite(misoPin, HIGH);

  Serial.begin(9600);     //Start serial collection
}

void loop() {

SPI.beginTransaction(SPISettings(7000000, MSBFIRST, SPI_MODE1)); //Inititializing parameters of transfer 
                                                                 //(Confident on mode and speed. The sensor uses least significant BYTES first, but uses Most significant BITS first as well, so not sure MSBFIRST is correct) 
 digitalWrite(ssPin, LOW); //Connect to the Sensor board
 delay(2); //Sensor requires deactivation for at leasrt 2ms

  SPI.transfer(&recArray, 8); //Transfer the 8 bytes of data to the recArray

 digitalWrite(ssPin, HIGH); //Disconnect from the sensor board

 SPI.endTransaction(); //End the transfer
 delay(250); //Delay to let the serial monitor not go insane (Can remove)

 for(int i = 0; i < 8; i++) //Method for displaying the bytes on the serial monitor
{
  Serial.println(recArray[i]); 
}
}

I think I am doing something fundamentally wrong with the SPI Library. But to provide a bit more info about the data I am trying to collect, I have attached an image of some of the transfer specs of the sensor embedded in the Mikro breakout board. For the entire info about the transfer specs, look at this link and go to pages 35-38.

Thank you so much for any help in advance, I am definitely stuck in the mud at the moment.

-Cam

Hi,

No too sure what went wrong there either but what I would suggest to help you debug it would be to try the following:

  1. move "SPI.beginTransaction(SPISettings(7000000, MSBFIRST, SPI_MODE1)); " to SETUP. btw are you sure you are using the right mode?

Also no need to use "SPI.endTransaction(); " in your LOOP. you can comment that out.

2)try looking at the receiving data individually instead of putting all in an array; so instead "SPI.transfer(&recArray, 8 );", try this in your FOR loop (move the it before "digitalWrite(ssPin, HIGH);") to view the data:

byte x;

for(int i = 0; i < 9; i++){ //Method for displaying the bytes on the serial monitor
x = SPI.transfer(0xFF); //the datasheet picture you provide suggest that you should be sending 0xFF
Serial.print(i);
Serial.print(": ");
Serial.println(x);
}

digitalWrite(ssPin, HIGH);

What I would be then expecting in the serial monitor would be something like that:

0: xxx //some value between 0 and 255
1: 255 //corresponding to 0xFF
2: LSByte X value in decimal
3: MSByte X value in decimal
4: LSByte Y value in decimal
5: MSByte Y value in decimal
6: LSByte Z value in decimal
7: MSByte Z value in decimal
8: SUM value in decimal

Hope that helps...

//Yes, that helps a ton! I am reading values in from the sensor now, I think the main issue was centered around parsing the data as it was being received. I should be able to fiddle my way to start reading accurately now.

Thank you very much!//

I now have this code running (advised edits included):

#include <SPI.h> // Include the SPI Library

const int sckPin = 13; //Initialize the four pins used with the SPI 
const int misoPin = 12;
const int mosiPin = 11;
const int ssPin = 10;

void setup() {

 SPI.begin(); //Begin SPI usage
  
  pinMode(sckPin, OUTPUT); //Declare four pin modes
  pinMode(misoPin, INPUT); 
  pinMode(mosiPin, OUTPUT);
  pinMode(ssPin, OUTPUT);

  digitalWrite(ssPin, HIGH); //Define starting values for digital pins
  digitalWrite(mosiPin, HIGH);
  digitalWrite(sckPin, LOW);
  digitalWrite(misoPin, HIGH);

  Serial.begin(9600);     //Start serial collection

 SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE1)); //Inititializing parameters of transfer 
                                                                 //(Confident on mode and speed) 
}

void loop() {

 digitalWrite(ssPin, LOW); //Connect to the Sensor board
 delay(2); //Sensor requires deactivation for at leasrt 2ms

 byte x;

for(int i = 0; i < 9; i++){ //Method for displaying the bytes on the serial monitor
  x = SPI.transfer(0xFF); //the datasheet picture you provide suggest that you should be sending 0xFF
  Serial.print(i); 
  Serial.print(": ");
  Serial.println(x);
}

digitalWrite(ssPin, HIGH);
delay(2000);
}

The sensor is definitely outputting something, as the reading changes from all "255" to pretty much a series of randomly changing bytes for the transferred values 2-7 (The magnet position values), so the Arduino/ code isn't currently capturing the data that it should. I have verified that it's in the right mode (1), and MSBFIRST. Does anyone have an idea about what else could be going wrong?

Thanks in advance!