SPI returning only 4 bit instead of whole byte

Hi there,

I am trying to communicate with my nRF24L01+ module via SPI from my Arduino Nano.

The code looks like this.

void loop() {

  SPI.beginTransaction(SPISettings(4000, MSBFIRST, SPI_MODE0));
  digitalWrite(CSNpin, LOW);
  Serial.print(SPI.transfer(0x00),BIN);
  Serial.println(SPI.transfer(0b01110010), BIN);
  SPI.endTransaction();
  digitalWrite(CSNpin, HIGH);
  
  delay(1000);
}

My problem now is that SPI.transfer only returns 4 bit instead of a whole byte. Could this be because of the nRF chip? Or is my code wrong. I deliberately set the SPI clock speed very low because I thought it might be the issue, but it wasn't.

The documentation says the nRF chip gives its status register over SPI when CSNpin shifts from high to low. Is the arduino too slow to catch all of the status bits?

The address of the status register is 0x00, the byte I want to write is 0b01110010.

My main issue is still the 4 bit return from the chip. Is there anything I'm doing wrong?

Try the following sketch and report the result:

#include <SPI.h>
byte myCommandData[] = {0x00, 0x72};
byte myData[2];

void setup ()
{
  Serial.begin(115200);
  SPI.begin();  //default speed: 4 MBits/sec
  digitalWrite(SS, LOW);   //Slave is selceted
}

void loop()
{
  for (int i = 0; i < 2; i++) //In Post-1, you sending two commands
  {
    myData[i] = SPI.transfer(myCommandData[i]);
    Serial.println(myData[i], HEX);
  }
  byte lsByte = myData[0];  
  byte msByte = myData[1];  
  int y = (int)msByte << 8 | (int)lsByte;
  Serial.print("Received from Slave: ");
  Serial.println(y, HEX);
  Serial.println("=========================");
  delay(2000);
}

Hi, thank you for your quick reply.

The outupt it get is

E
8
Received from Slave: 80E
=========================
8
8
Received from Slave: 808
=========================

Why aren't we using SPI.beginTransfer though? Is it just because the default values are stored anyways?

Is using SS better than using my own CSNpin variable?

What do you think was the problem?

Thanks again.

1. Are you getting from the Slave what you are expecting?
2. I am not used to emply SPI.beginTransfer() and SPI.end() codes.
3. SS, MOSI, MISO, SCK (the four SPI Signals) are mapped to DPin-10 11, 12, 13 when SPI.h Library is included in the sketch. That's why, I prefer to use the symbolic name SS which will be be connected with the CS/-pin of Slave.

4. Your sketch looked alright and yet it did not appear to me as logical.

1. With your sketch I am getting what I am expecting. 0x00 is the address of the status register and the byte 0x72 is what I want to write to it. Then it should continue to only return the written byte.

2. The SPI.beginTransfer code wasn't emty but with my settings in it. SPI.end() is empty even in the documentation.

3. That makes sense, yeah.

4. That's because it isn't logical. At the moment I just wanted to establish that the nano and the nRF chip yould communicate over SPI as expected. It didn't so I threw everything at it what I found.

Thank you for your help. I think I can figure it out now.

But you are getting : 0x08 or 0x0E! Whre is the 0x72?

In fact, your sketch of Post-1 was working except that the leading four zeros were not being shown on the Serial Monitor; as a result, you saw only four bits.

I tried again with a different command word while in the process of writing the reply, which I forgot to mention. With the different command word I am getting reply 0x72. My fault.

1 Like

With your sketch of Post-1, could you manage to get 0x72 as echo from Slave?

Yes, it might have been just the command word which was wrong. Also, not using SPI.end() and SPI,beginTransmission(mySettings) is a huge improvement for my code to use.

1 Like