Trying to learn basics of NRF24L01+

I’m trying to learn how to program for the NRF24L01+ wireless radios. After reviewing many sites, I think I’m close, but still can’t seem to get a basic program to work. For my simple test, I have one Arduino attached to one NRF24L01+ sending a random number. Then another Arduino attached to another NRF24L01+ to receive this number. Here’s my code…

Sending…

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

RF24 radio(9,10);

const uint64_t pipe = 0xF0F0F0F0E1LL;

void setup(void) {
 
  Serial.begin(57600);
  radio.begin();
  //radio.setRetries(15,15);
  radio.openWritingPipe(pipe);
  
}


void loop () {
  
  unsigned int random_Number = random(0,255);
  Serial.print("Sending: ");
  Serial.println(random_Number);
  
  radio.stopListening();  
    
  boolean ok = radio.write( &random_Number, sizeof(random_Number) );
    
  if (ok) Serial.println("ok...");
  else Serial.println("failed.");
  
  delay(5000);
  
}

And receiving…

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

RF24 radio(9,10);

const uint64_t pipe = 0xF0F0F0F0D2LL;

void setup(void) {
 
  Serial.begin(57600);  
  radio.begin();
  //radio.setRetries(15,15);
  radio.openReadingPipe(1,pipe);
  radio.startListening();
  Serial.println("Ready!");
  
}


void loop () {
  
  if ( radio.available() ) {
    Serial.println("radio is available");
    unsigned int data;
    boolean done=false;
    while (!done) {
      done = radio.read( &data, sizeof(data) );
      Serial.print("Received: ");
      Serial.println(data);
    }
  }
  
}

On the sending sketch, the serial monitor says…

“Sending: 145
failed.”

And on the receiving sketch it only says “Ready”. Never “radio is available”.

I have tested it with the “GettingStarted” sketch from the RF24 examples, and it works perfectly - so I know I have them wired correctly.

Any help someone can give me would be greatly appreciated.

Thanks in advance!

Hi rbontrager

Any change if you use the same value for pipe in both programs?

Regards

Ray

Thanks for the idea, but I've tried that. Putting the same value in for each pipe yields the same results.

  1. Use the same pipe for both: const uint64_t pipe = 0xF0F0F0F0D1LL;
  2. Open a writing pipe on the receiver:
radio.openWritingPipe(pipe);

Info: The transmitter must transmit to the receiving address of the receiver for the payload to be delivered. In order for the receiver to acknowledge (ACK) transmission, it needs a writing pipe opened to send back the ACK packet.

Thanks TMRh20 for the help. Unfortunately, after making the changes, it still doesn't work at all.

Any other ideas?

Well, it works ok for me, so we know the the code is fine, here is what I am using:

Transmitter:

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

RF24 radio(9,10);

const uint64_t pipe = 0xF0F0F0F0E1LL;

void setup(void) {
 
  Serial.begin(57600);
  radio.begin();
  //radio.setRetries(15,15);
  radio.openWritingPipe(pipe);
  
}


void loop () {
  
  unsigned int random_Number = random(0,255);
  Serial.print("Sending: ");
  Serial.println(random_Number);
  
  radio.stopListening();  
    
  boolean ok = radio.write( &random_Number, sizeof(random_Number) );
    
  if (ok) Serial.println("ok...");
  else Serial.println("failed.");
  
  delay(1000);
  
}

Receiver:

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

RF24 radio(9,10);

const uint64_t pipe = 0xF0F0F0F0E1LL;

void setup(void) {
 
  Serial.begin(57600);  
  radio.begin();
  //radio.setRetries(15,15);
  radio.openReadingPipe(1,pipe);
  radio.openWritingPipe(pipe);
  radio.startListening();
  Serial.println("Ready!");
  
}


void loop () {
  
  if ( radio.available() ) {
    Serial.println("radio is available");
    unsigned int data;
    boolean done=false;
      radio.read( &data, sizeof(data) );
      Serial.print("Received: ");
      Serial.println(data);
  }
  
}

So, if you are using that code and it doesn’t work, it usually indicates another problem.

To get debugging info, you need to do a few things:

  1. Add the printf.h file from one of the examples to your sketch folder
  2. Add the line #include “printf.h” in your sketch
  3. Do printf_begin(); right after Serial.begin(57600);
  4. Finally, at the end of your setup() function, do a radio.printDetails();

It should look exactly like the attached picture on the receiver with the debug details printed.

Since you say it works fine with the getting started sketch, we know the wiring is ok, and the code works fine for me, so the code is fine, then something odd is usually to blame, but difficult to figure out from my end. Also, power cycle the Arduino, since some settings cannot be undone otherwise, and may interfere if you have been running other example sketches. Not sure what library fork you are using, but of course I am using my fork found here: https://github.com/TMRh20/RF24/archive/master.zip

rf24debug.JPG