0

I am trying to get 2 Arduino Uno boards to talk to one another using 2 nRF24L01 transceiver modules. I confirmed the wiring was correct and the modules indeed worked by uploading the GettingStarted sketch (from the nRF24 library) with the required modifications and everything was working (I used the Arduino 3V3 pin with a 100 uF decoupling cap).

If the transceivers were both in proximity I had almost 100% success rate, and if I went in another room (behind closed door, behind a wall, etc.) I still had almost no dropped packets (every transmission sent and response received succesfully).

I then made a sketch that reads a joystick input, computes an integer and attempts to broadcast that integer to the other Arduino. The transmission only worked when I held the modules very close to one another and with the antennas facing each other, and then with a big delay and about 50% drop rate. Also on one serial monitor it would output a string of successfull messages, and only after a pause would the other Arduino output a string of succesfull pong-backs (not even close to synchronous as they were with the Getting Started sketch). I even tried changing the PA level, did not help. Can anyone give me any help or pointers? Here is the Getting Started sketch I modified:

/*
* 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(8,9);
/**********************************************************/

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

// Used to control whether this node is sending or receiving
//bool role = 0;

const int x_pin = 2;
const int y_pin = 3;

int forwards, backwards, left, right;

void setup() {
  Serial.begin(115200);
  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_MAX);

  // 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"));

    int x = analogRead(x_pin);
    int y = analogRead(y_pin);

    backwards = 0;
    forwards = 0;
    left = 0;
    right = 0;

    if (x > 1000) right = 1;
    if (x < 100) left = 1;
    if (y > 1000) backwards = 1;
    if (y > 100) forwards = 1;

    int message = 0;

    if (forwards && !left && !right) message = 3;
    else if (forwards && left && !right) message = 2;
    else if (!forwards && left && !right) message = 1;
    else if (forwards && !left && right) message = 4;
    else if (!forwards && !left && right) message = 5;
    else if (backwards) message = 6;
    else if (!forwards && !backwards && !left && !right) message = 0;

    //unsigned long start_time = micros();                             // Take the time, and send it.  This will block until complete
     if (!radio.write( &message, sizeof(int) )){
       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"));
    }

    delay(50);
} // Loop

Also, whenever I would get a succesful transmission (about 50% rate, and with huge delay) I would never receive a single digit on the other Uno & nRF24L01. It was a variable-length string of digits (eg '232420001'). The other Uno had the GettingStarted sketch with just the radio number modified and configured via serial into receive mode.

  • go back to the GettingStarted sketch .... add only this line int x = analogRead(x_pin); in the same place ... if that works, change the send line if (!radio.write( &x, sizeof(int) )){ .... and so on – jsotola Jan 11 '18 at 20:21
  • 1
    just re-read your post. the payload size may not match in this line radio.read( &got_time, sizeof(unsigned long) ); ... you did not say that you changed it to int – jsotola Jan 11 '18 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.