NRF24L01 receiving inaccurate data

Hello, I'm working on a project to communicate one Arduino Uno and an Arduino Uno R4 WiFi with a NRF24L01 transceivers.

I believe both Arduinos are talking but the data sent back is inaccurate.
The transmitter sent:
Data1:123
Data2:x
Sent

The receiver gets:
Data1:0
Data2:
Received

The code I used is:
Transimiter

#include "SPI.h" 
#include "RF24.h" 
#include "nRF24L01.h" 
#define CE_PIN 9 
#define CSN_PIN 10 
#define INTERVAL_MS_TRANSMISSION 250 
RF24 radio(CE_PIN, CSN_PIN); 
const byte address[6] = "00001"; 
//NRF24L01 buffer limit is 32 bytes (max struct size) 
struct payload { 
	 byte data1; 
	 char data2; 
}; 
payload payload; 
void setup() 
{ 
	 Serial.begin(9600); 
	 radio.begin(); 
	 //Append ACK packet from the receiving radio back to the transmitting radio 
	 radio.setAutoAck(false); //(true|false) 
	 //Set the transmission datarate 
	 radio.setDataRate(RF24_250KBPS); //(RF24_250KBPS|RF24_1MBPS|RF24_2MBPS) 
	 //Greater level = more consumption = longer distance 
	 radio.setPALevel(RF24_PA_MAX); //(RF24_PA_MIN|RF24_PA_LOW|RF24_PA_HIGH|RF24_PA_MAX) 
	 //Default value is the maximum 32 bytes 
	 radio.setPayloadSize(sizeof(payload)); 
	 //Act as transmitter 
	 radio.openWritingPipe(address); 
	 radio.stopListening(); 
} 
void loop() 
{ 
	 payload.data1 = 123; 
	 payload.data2 = 'x'; 
	 radio.write(&payload, sizeof(payload)); 
	 Serial.print("Data1:"); 
	 Serial.println(payload.data1); 
	 Serial.print("Data2:"); 
	 Serial.println(payload.data2); 
	 Serial.println("Sent"); 
	 delay(INTERVAL_MS_TRANSMISSION);}

Receiver

#include "SPI.h" 
#include "RF24.h" 
#include "nRF24L01.h" 
#define CE_PIN 9 
#define CSN_PIN 10 
#define INTERVAL_MS_SIGNAL_LOST 1000 
#define INTERVAL_MS_SIGNAL_RETRY 250 
RF24 radio(CE_PIN, CSN_PIN); 
const byte address[6] = "00001"; 
//NRF24L01 buffer limit is 32 bytes (max struct size) 
struct payload { 
	 byte data1; 
	 char data2; 
}; 
payload payload; 
unsigned long lastSignalMillis = 0; 
void setup() 
{ 
	 Serial.begin(9600); 
	 radio.begin(); 
	 //Append ACK packet from the receiving radio back to the transmitting radio 
	 radio.setAutoAck(false); //(true|false) 
	 //Set the transmission datarate 
	 radio.setDataRate(RF24_250KBPS); //(RF24_250KBPS|RF24_1MBPS|RF24_2MBPS) 
	 //Greater level = more consumption = longer distance 
	 radio.setPALevel(RF24_PA_MIN); //(RF24_PA_MIN|RF24_PA_LOW|RF24_PA_HIGH|RF24_PA_MAX) 
	 //Default value is the maximum 32 bytes1 
	 radio.setPayloadSize(sizeof(payload)); 
	 //Act as receiver 
	 radio.openReadingPipe(0, address); 
	 radio.startListening(); 
} 
void loop() 
{ 
	 unsigned long currentMillis = millis(); 
	 if (radio.available() > 0) { 
	   radio.read(&payload, sizeof(payload)); 
	   Serial.println("Received"); 
	   Serial.print("Data1:"); 
	   Serial.println(payload.data1); 
	   Serial.print("Data2:"); 
	   Serial.println(payload.data2); 
	   lastSignalMillis = currentMillis; 
	 } 
	 if (currentMillis - lastSignalMillis > INTERVAL_MS_SIGNAL_LOST) { 
	   lostConnection(); 
	 } 
} 
void lostConnection() 
{ 
	 Serial.println("We have lost connection, preventing unwanted behavior"); 
	 delay(INTERVAL_MS_SIGNAL_RETRY); 
}

Any help resolving this issue is appreciated.
Thanks

You "believe both Arduino are talking"?

Based on what? You've ignored, turned off or not called anything that might tell you whether or not they in fact were!

Transmitter:

  • You don't check the return status of begin(). You have no idea if the nRF24L01 was even detected, let alone if it's working.
  • You turn off auto acks. Why bother letting the library verify that a transmission did go through, right?
  • radio.printDetails(); would be full of great debugging info. But you don't call it.
  • You set the PA to maximum. Low would be better for a test on a desktop.
  • You don't check the return status of write(). You have no idea if anything was sent.
  • You never look at radio.failureDetected to see if the library has detected a fault.

Receiver:

  • You don't check the return status of begin(). You have no idea if the nRF24L01 was even detected, let alone if it's working.
  • You turn off auto acks again.
  • radio.printDetails(); would be full of great debugging info. But you don't call it.
  • You never look at radio.failureDetected to see if the library has detected a fault.

The library gives you all kinds of great tools to assist in debugging communication problems.

You aren't using any of them.

You might want to rethink that strategy.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.