NRF24L01: Always receiving an "0" at the end of transmission

I am working with two Arduino UNOs. Both of them have a NRF24L01 unit.
The TX side is sending an long “211” to the rx side.
I can receive the data and print it to the Serial Window, but it always appends an 0 as newline.
Maybe you could help me why this happens.

This is my code for TX:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>

/*
This sketch sends a string to a corresponding Arduino
with nrf24 attached.  It appends a specific value 
(2 in this case) to the end to signify the end of the
message.
*/

RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int i = 0;

void setup(void){
  Serial.begin(4800);
  radio.begin();
  radio.openWritingPipe(pipe);
  radio.openReadingPipe(1,pipe);
  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_250KBPS);
  }
void loop(void){
  
   long rxTxData = 211;
   //Serial.println(sizeof(rxTxData));
   bool ack = radio.write(&rxTxData, sizeof(long));
   Serial.print("Sent: ");
   Serial.println(rxTxData);
   i++;
   if(ack) {
     Serial.println("ACK received");
   } else {
     Serial.println("ACK not received");
   }
   Serial.println(i);
   delay(2000);
}

This is my code for RX:

#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>

/*
This sketch receives strings from sending unit via nrf24 
and prints them out via serial.  The sketch waits until
it receives a specific value (2 in this case), then it 
prints the complete message and clears the message buffer.
*/
long rxTxData;
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
bool received = false;
int i = 0;

void setup(void){
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.openWritingPipe(pipe);
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
  Serial.print("Selected Power Level: ");
  Serial.println(radio.getPALevel());
}
void loop(void){
  //check if data available
  if (radio.available()){
      //read data from nrf24l01 buffer
      radio.read(&rxTxData, sizeof(long));
      Serial.println(rxTxData);
   }
   //ySerial.println(sizeof(long));
}

I have no problem at receiving my data, but it always appends an “0” at the end.

Serial Output:

Selected Power Level: 0
211
0 --> Why am I receving this 0?
211
0
211
0
211
0
211
0
211
0
211
0
211
0

Thanks in Advance.
mtdshare

I don't know, is the short answer.

Your Tx sends a value every 2 seconds. What is the interval between the Rx printing 211 and printing 0?

Maybe have a look at this Simple nRF24L01+ Tutorial

...R

//ySerial.println(sizeof(long));

Did you mean:
Serial.println(sizeof(rxTxData));?
That will always return 4, 4 bytes in a long integer.

Guessing: could it have anything to do with the fact that the full 32 byte packet is being transmitted padded with zeroes? Take a look at setPayloadSize.

Robin2:
I don't know, is the short answer.

Your Tx sends a value every 2 seconds. What is the interval between the Rx printing 211 and printing 0?

Maybe have a look at this Simple nRF24L01+ Tutorial

...R

The interval is zero, because it immediately jumps again into the radio.available condition and prints the "0".
If I add a delay of for example 1ms, than the "0" disappears.

edgemoron:
Did you mean:

Serial.println(sizeof(rxTxData));

?
That will always return 4, 4 bytes in a long integer.

This was only a test, to see if there is something wrong with the payload length.

My problem is that the data is two times available. The first time it prints the correct data "211", but afterwards it prints the "0".
For me it seems like the buffer of the NRF24L01 is not cleared.

wildbill:
Guessing: could it have anything to do with the fact that the full 32 byte packet is being transmitted padded with zeroes? Take a look at setPayloadSize.

This is a good idea.
I will try to add the parameter to my code and test it.
Is it correct to set the payload size to a length of 4 if I transmit a long?

mtdshare:
If I add a delay of for example 1ms, than the "0" disappears.

Post the program with that change.

...R

Robin2:
Post the program with that change.

…R

If I add a delay of for example 1 millisecond, the “0” disappears in the Serial Monitor.
But I don’t understand why there is a delay needed…

void loop(void){
  //check if data available
  if (radio.available()){
      //read data from nrf24l01 buffer
      radio.read(&rxTxData, sizeof(long));
      Serial.println(rxTxData);
   }
   delay(1);
}

mtdshare:
If I add a delay of for example 1 millisecond, the "0" disappears in the Serial Monitor.
But I don't understand why there is a delay needed...

Neither do I. Interesting. At least you have a work-around :slight_smile:

I don't have the time to set up a test at the moment to see if I get the same result.

What happens if you change

bool ack = radio.write(&rxTxData, sizeof(long));

to

bool ack = radio.write(&rxTxData, sizeof(rxTxData));

and

radio.read(&rxTxData, sizeof(long));

to

radio.read(&rxTxData, sizeof(rxTxData));

...R

Robin2:
Neither do I. Interesting. At least you have a work-around :slight_smile:

I don't have the time to set up a test at the moment to see if I get the same result.

What happens if you change

bool ack = radio.write(&rxTxData, sizeof(long));

to

bool ack = radio.write(&rxTxData, sizeof(rxTxData));

and

radio.read(&rxTxData, sizeof(long));

to

radio.read(&rxTxData, sizeof(rxTxData));

...R

I changed the code like you mentioned it, but nothing helped.

I fixed my problem yesterday... You won't believe what the problem was...
The power cable from the voltage regulator to the NRF24L01 was broken.
This produced the "0". I checked all cables, and the "0" disappeared when I moved this cable.
I swapped the cable with a new one and all works fine now.

Thank you for all your assistance

Thanks for the update. It will save a lot of head-scratching :slight_smile:

…R