Nrf24l01 radio.available always true?

Hi Guys,

I'm using TMRh20's RF24 library with the Nrf24l01+. I have 2 Nrf24l01 that both transmit and receive to each other. One is connected to an Arduino Uno, the other to a Particle Photon.

Transmitting and receiving is working fine on the Photon.

Transmitting is working on the Arduino, but i'm having problems receiving.

When I call radio.available() the same message is repeated over and over in the serial monitor. Even if I disconnect the Photon radio.available() still sees that same message coming in over and over. The only way I've been able to stop this from happening is by calling radio.flush_rx() to manually flush the rx buffer on the chip.

Here is the relevant code from the Arduino:

  uint8_t pipe;
  int msg;
  while(radio.available(&pipe)) {
    Serial.println(F("Radio Available."));
    radio.read(&msg, sizeof(int));
    Serial.println(msg);
//    radio.flush_rx();
    }                                 
  }

Capture.PNG

radio.available() is a silly name for a function that tells you whether a valid message packet has been received.

There is absolutely no reason to print "Radio Available" every time that happens. Just read the message and move on.

The problem is once the message is read, it remains in the receive buffer. So, radio.available() is always true. I am manually flushing the receive buffer to get around this currently.

Post all your code.

I never heared of a NRF24 behaving like described.

Please post you whole code.

OFFTOPIC

jremington:
radio.available() is a silly name for a function that tells you whether a valid message packet has been received.

Same class of sillyness that Serial.available() contributes to. (IMHO not silly at all)

/OFFTOPIC

Serial or radio.data_available would be much clearer.

Most likely the OP's problem is that he/she is reading at most 2 bytes, but the message is longer.

radio.read(&msg, sizeof(int));

Anything wrong with the code?

.

shaahid:
Anything wrong with the code?

I have not looked at this Thread before now.

If the code works there is nothing wrong with it.
If it does not work then tell us what it actually does and what you want it to do.

...R

Same problem. I have custom pcb with atmega 2560. Programmed with arduino as ISP. When my isp programming cable is connected from arduino mega board to pcb and testing nrf24 noticed that radio.available giving 0 value constantly. When i removed my programming cable and powered my pcb up with external 5v supply it works fine?

Wils0n:
Same problem.

If you want advice please post the program code that YOU are using.
And please use the code button </> so your code looks like this and is easy to copy to a text editor

You say you are using a custom PCB so maybe that is the cause of the problem - but I am not competent to review a PCB schematic.

...R

You probably disturb the SPI hardware pins with the attached isp programmer.

FINAL EDIT: Forget it, I had connected CE and CS to pins 9 and 10 from a previous tutorial and the random values were the result of the floating pin states... Sorry.


Hi, I have the same or a similar problem here. I'm using an Arduino Uno and an Arduino Nano with a slightly modified version of the official Getting-Started sketch of the RF24 library:

/*
* Getting Started example sketch for nRF24L01+ radios
* This is a very basic example of how to send data from one node to another
* Updated: Dec 2014 by TMRh20
*/

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

/****************** User Config ***************************/
/***      Set this radio as radio number 0 or 1         ***/
bool radioNumber = 0;

/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(7,8);
/**********************************************************/

byte addresses[][6] = {"1Node","2Node"};

// Used to control whether this node is sending or receiving (1=sender, 0=receiver)
bool role = 0;

void setup() {
  Serial.begin(9600);
  Serial.println(F("RF24/examples/GettingStarted"));
  Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
  
  radio.begin();

  // Set the PA Level low to prevent power supply related issues since this is a
 // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
  radio.setPALevel(RF24_PA_LOW);
  
  // Open a writing and reading pipe on each radio, with opposite addresses
  if(radioNumber){
    radio.openWritingPipe(addresses[1]);
    radio.openReadingPipe(1,addresses[0]);
  }else{
    radio.openWritingPipe(addresses[0]);
    radio.openReadingPipe(1,addresses[1]);
  }
  
  // Start the radio listening for data
  radio.startListening();
}

void loop() {
  
  
/****************** Ping Out Role ***************************/  
if (role == 1)  {
    
    radio.stopListening();                                    // First, stop listening so we can talk.
    
    
    Serial.println(F("Now sending"));

    unsigned long start_time = micros();                             // Take the time, and send it.  This will block until complete
     if (!radio.write( &start_time, sizeof(unsigned long) )){
       Serial.println(F("failed"));
     }
        
    radio.startListening();                                    // Now, continue listening
    
    unsigned long started_waiting_at = micros();               // Set up a timeout period, get the current microseconds
    boolean timeout = false;                                   // Set up a variable to indicate if a response was received or not
    
    while ( ! radio.available() ){                             // While nothing is received
      if (micros() - started_waiting_at > 200000 ){            // If waited longer than 200ms, indicate timeout and exit while loop
          timeout = true;
          break;
      }      
    }
        
    if ( timeout ){                                             // Describe the results
        Serial.println(F("Failed, response timed out."));
    }else{
        unsigned long got_time;                                 // Grab the response, compare, and send to debugging spew
        radio.read( &got_time, sizeof(unsigned long) );
        unsigned long end_time = micros();
        
        // Spew it
        Serial.print(F("Sent "));
        Serial.print(start_time);
        Serial.print(F(", Got response "));
        Serial.print(got_time);
        Serial.print(F(", Round-trip delay "));
        Serial.print(end_time-start_time);
        Serial.println(F(" microseconds"));
    }

    // Try again 1s later
    delay(1000);
  }



/****************** Pong Back Role ***************************/

  if ( role == 0 )
  {
    unsigned long got_time;
    
    if( radio.available()){
                                                                    // Variable for the received timestamp
      while (radio.available()) {                                   // While there is data ready
        radio.read( &got_time, sizeof(unsigned long) );             // Get the payload
      }
     
      radio.stopListening();                                        // First, stop listening so we can talk   
      radio.write( &got_time, sizeof(unsigned long) );              // Send the final one back.      
      radio.startListening();                                       // Now, resume listening so we catch the next packets.     
      Serial.print(F("Sent response "));
      Serial.println(got_time);  
   }
 }




/****************** Change Roles via Serial Commands ***************************/

  if ( Serial.available() )
  {
    char c = toupper(Serial.read());
    if ( c == 'T' && role == 0 ){      
      Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
      role = 1;                  // Become the primary transmitter (ping out)
    
   }else
    if ( c == 'R' && role == 1 ){
      Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));      
       role = 0;                // Become the primary receiver (pong back)
       radio.startListening();
       
    }
  }


} // Loop

The role and radioNumber variables are adjusted for each board to either being both 1 or both 0.

The output I get for the receiving (Nano) board looks like this snippet:

Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 2139062143
Sent response 4294967295
Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 2139062143
Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 4294934399
Sent response 0
Sent response 0
Sent response 0
Sent response 2139062143
Sent response 2147450751
Sent response 0
Sent response 0
Sent response 520093696
Sent response 2139062047
Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 0
Sent response 2139062143
Sent response 0
Sent response 0
Sent response 0

Has anyone an idea what I'm doing wrong?

Thanks in advance!


EDIT:
After I completely disconnected the sending Arduino UNO and still receiving similar data as posted above on the Arduino NANO, I wondered if it was because of an already occupied channel. So I introduced a new global variable channel initially set to 0 and the following code at its respective position:

if ( role == 0 )
  {
    unsigned long got_time;
    
    if( radio.available()){
                                                                    // Variable for the received timestamp
      while (radio.available()) {                                   // While there is data ready
        radio.read( &got_time, sizeof(unsigned long) );             // Get the payload
      }

      // Try out a new channel if the current one is in use
      radio.setChannel(channel++);
      Serial.print("Switched to channel ");
      Serial.println(channel);
      if (channel == 126) channel = 0;
     
//      radio.stopListening();                                        // First, stop listening so we can talk   
//      radio.write( &got_time, sizeof(unsigned long) );              // Send the final one back.      
//      radio.startListening();                                       // Now, resume listening so we catch the next packets.     
//      Serial.print(F("Sent response "));
//      Serial.println(got_time);  
   }
 }

Now, it iterates through all available channels, seemingly receiving data on each of them as shown in this snippet:

Switched to channel 123
Switched to channel 124
Switched to channel 125
Switched to channel 126
Switched to channel 1
Switched to channel 2
Switched to channel 3
Switched to channel 4

EDIT2:
I used the following simplified code I wrote by myself and uploaded it to both the UNO and NANO while switching the other board off respectively. The result is a constant output of "Radio has data", sparingly followed by a "random" number. Any ideas why this could be happening?

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

RF24 radio(7, 8);
byte address[6] = "foobar";
const byte channel = 10;

void setup() {
  radio.begin();
  radio.setPALevel(RF24_PA_MIN);
  radio.setChannel(channel);
  radio.openReadingPipe(1, address);
  radio.startListening();
  
  Serial.begin(9600);
  Serial.println("Receiver started");
}

void loop() {
  if (radio.available()) {
    Serial.println("Radio has data");
    byte msg;
    while (radio.available()) {
      radio.read(&msg, sizeof(msg));
      if (msg != 0) {
        Serial.println(msg);
      }
    }
  }
}

Adding some new info to this old thread since it's highly ranked on Google.

— One transmitter, and one receiver, both Arduino Nano clones, with nRF24l01+ modules.
— Added capacitors to both radios soldered directly onto the + and -
— The transmitter would only transmit successfully with a dedicated power supply, and I added a capacitor to that circuit
:star: The receiver would only work successfully when powered by the Nano directly, otherwise radio.available() always returns true :star:

—— Troubleshooting the receiver

Powering the receiver with a dedicated supply would cause the radio.available() to return true. Adding capacitors did not solve this issue. I didn't test every option. This power module is for breadboards. Adding capacitors to the board didn't help. Cutting out the board and using jumpers directly to the module didn't help.

(The only thing I didn't have a chance to test is soldering capacitors directly to the power supply (todo later)).

I'll also try a different power regulator once I have some time.

It was tedious isolating all of these issues, but it validated what most people are saying. Power supply and capacitors make the difference. (If all your hardware otherwise works).

In my case, the program here Reply #29 in this [Simple nRF24L01+ Tutorial](http://forum.arduino.cc/index.php?topic=421081) was giving positive results even as the radio.available() == trueissue remained. Very useful tool just knowing that you have things wired up correctly.

—— Potential Workarounds

In the case of radio.available() repeatedly returning true, I was still receiving the data from the transmitter, so I could filter the noise using software. Of course, I'd rather solve the hardware problem.

Jason

PS: this work is a part of the Familiar Bot project. Check us out https://twitter.com/familiarHQ

jasonhargrove:
Adding some new info to this old thread since it's highly ranked on Google.

I can't figure from your Post whether you have a problem, and if so, what is it?

...R

Hi,

I was also struggling with getting the this module to work.
This is the code I'm using from the tutorial on this website https://howtomechatronics.com/tutorials/arduino/arduino-wireless-communication-nrf24l01-tutorial/.
Transmitter code:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}
void loop() {
  const char text[] = "Hello World";
  radio.write(&text, sizeof(text));
  delay(1000);
}

Reciever code:

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

RF24 radio(7, 8); // CE, CSN

const byte address[6] = "00001";

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    char text[32] = "";
    radio.read(&text, sizeof(text));
    Serial.println(text);
  }
}

The serial monitor just outputs 'nothing' and scrolls downwards.
With only the receiver plugged in, radio.available outputs true and radio.ischipconnected outputs 0.
I was wondering where my problem could stem from?
Thanks,
Aristide

aarnulf:
I was also struggling with getting the this module to work.

You have joined a very old Thread.

Have a look at this Simple nRF24L01+ Tutorial.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

...R