Using TMRh20's RF24 Library - Having Troubles

Hey guys! I’m very new to using Arduino and I for some reason thought it would be a good idea to try wireless communication…

Anyways, I am using the libraries and example code from here: https://github.com/TMRh20/RF24

This seems to be VERY updated and is generally accepted as working. Now on to my issues. When I first attempted it, I installed the library and used the GettingStarted.ino class to test the wireless communication. I installed the class on both my Arduinos, only changing the radio number(was I supposed to do this?). I connected everything with this pinout: http://tmrh20.github.io/RF24/index.html - Scroll to bottom

Anyways, I wasn’t able to get ANYTHING working at all. I connected everything correctly, double and triple checked, and opened the serial messages thingy. I was asked to type ‘t’ to turn the Arduino into a transmit, but when I did this I got no response from the Arduino. The little RX lite did blink when I hit enter though. So I knew something was broken, but didn’t know what. I was messing around with connections on the Arduino when I accidently put my finger under the chip, which literally made the serial communcation work. Well one, wtf just happened? And two, awesome. It looks like some pins are misplaced.

After checking the pins with the pin connections listed on the documentary, I was unable to fix the issue. So I got into the library code I was using and checked the pin listing there. I found that the MISO, MOSI, and SCK where all in the wrong place. I also found that when I changed it to comply the with code, it used up pin 8 which was where the CSN connection was. So I had to go into the code and change RF24 radio(7,8); to RF24 radio(11,12); This is all very confused and I’m sick so my brain isn’t working right so I am having some trouble.

So with the updated library and code, I upload it to the chips again and make sure the connections are correct(Again, uploading one as radio 1 and one as radio 0)

The Serial output automatically started spewing when I connected the Arduino. It was spewing

Sent response 34340866
Sent response 34340866
Sent response 34340866
Sent response 34340866
Sent response 34340866
Sent response 34340866
Sent response 34340866

This meant it had a radio available and was set to receive. I concluded this behavior was undesired because it has a prompt when you start the serial comm, meaning that they probably want you to be able to actually read the prompt instead of being spammed. But typing ‘t’ still worked. So I did. Then it started to spew, every second, this:

Sent 148283216, Got response 255, Round-trip delay 148282961 microseconds
Now sending
failed
Sent 149286452, Got response 255, Round-trip delay 149286197 microseconds
Now sending
failed
Sent 150289784, Got response 255, Round-trip delay 150289529 microseconds
Now sending
failed

Since ‘failed’ meant it wasn’t able to write to the radio, I figured it didn’t work. I also noticed that the round-trip delay increased in an oddly specific way(nearly 1000000 each iteration).

Anyways, yeah. Here was the code I was using to upload:

/*
* 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 = 1;

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

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

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

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_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 time = micros();                             // Take the time, and send it.  This will block until complete
     if (!radio.write( &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 time = micros();
        
        // Spew it
        Serial.print(F("Sent "));
        Serial.print(time);
        Serial.print(F(", Got response "));
        Serial.print(got_time);
        Serial.print(F(", Round-trip delay "));
        Serial.print(time-got_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

So I wasn’t able to get it working, yet. It’s late now and I need a break. But tomorrow I will be figuring this out. I did realize while writing this I am using a 9 month old example, when the library was updated just a few months ago, so things may be broken. I noticed the another example ‘CallResponse’ was more recently updated, so I will be trying that tomorrow.

But has anyone else ran into these issues? Specifically the serial being frozen unless you basically short circuit the board? :stuck_out_tongue: If so, how did you fix it? Like I said I am new to this but I am learning.

Thanks,
HeyAwesomePeople

You might like to try these two programs

EDIT 25 Feb 2016 - there is a newer version of these programs here

EDIT 22 Apr 2017 - this Simple nRF24L01+ Tutorial is newer and more comprehensive

TRANSMITTER

// TrackControl - the master or the transmitter

 // http://tmrh20.github.io/RF24/
 // NB I have added 'TmR' at the start of the names of the library file to distiguish
 //   this version from maniacBug's version. Eg TmRRF24.h

 //~ - CONNECTIONS: nRF24L01 Modules See:
 //~ http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
 //~ 1 - GND
 //~ 2 - VCC 3.3V !!! NOT 5V
 //~ 3 - CE to Arduino pin 9
 //~ 4 - CSN to Arduino pin 10
 //~ 5 - SCK to Arduino pin 13
 //~ 6 - MOSI to Arduino pin 11
 //~ 7 - MISO to Arduino pin 12
 //~ 8 - UNUSED

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


#define CE_PIN   9
#define CSN_PIN 10

// NOTE: the "LL" at the end of the constant is "LongLong" type
// These are the IDs of each of the slaves
const uint64_t slaveID = 0xE8E8F0F0E1LL;

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

int dataToSend[2];

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000;
int txVal = 0;
int ackMessg[4];
byte ackMessgLen = 2;


void setup() {

 Serial.begin(9600);
 Serial.println("Track Control Starting");
 radio.begin();
 radio.setDataRate( RF24_250KBPS );
 radio.enableAckPayload();
 radio.setRetries(3,5); // delay, count
}

//====================

void loop() {

 currentMillis = millis();
 if (currentMillis - prevMillis >= txIntervalMillis) {

 radio.openWritingPipe(slaveID); // calls each slave in turn
 dataToSend[0] = txVal; // this gets incremented so you can see that new data is being sent
 txVal += 1;
 dataToSend[1] = txVal;
 txVal += 1;
 bool rslt;
 rslt = radio.write( dataToSend, sizeof(dataToSend) );
 Serial.print("\nRSLT ");
 Serial.println(rslt);
 Serial.print("Data Sent ");
 Serial.println(dataToSend[0]);
 if ( radio.isAckPayloadAvailable() ) {
 radio.read(ackMessg,ackMessgLen);
 Serial.print("Acknowledge received: ");
 Serial.println(ackMessg[0]);
 }

 prevMillis = millis();
 }
}

RECEIVER

// HandController - the slave or the receiver

    // http://tmrh20.github.io/RF24/
    // NB I have added 'TmR' at the start of the names of the library file to distiguish
    //   this version from maniacBug's version. Eg TmRRF24.h

    //~ - CONNECTIONS: nRF24L01 Modules See:
    //~ http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
    //~ 1 - GND
    //~ 2 - VCC 3.3V !!! NOT 5V
    //~ 3 - CE to Arduino pin 9
    //~ 4 - CSN to Arduino pin 10
    //~ 5 - SCK to Arduino pin 13
    //~ 6 - MOSI to Arduino pin 11
    //~ 7 - MISO to Arduino pin 12
    //~ 8 - UNUSED

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

#define CE_PIN   9
#define CSN_PIN 10

// NOTE: the "LL" at the end of the constant is "LongLong" type

const uint64_t   deviceID = 0xE8E8F0F0E1LL; // Define the ID for this slave

int valChange = 1; 

RF24 radio(CE_PIN, CSN_PIN);

int dataReceived[2];  
int ackData[2] = {12,0};
byte ackLen = 2;

void setup() {
    
    Serial.begin(9600);
    delay(1000);
    Serial.println("Hand Controller Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.openReadingPipe(1,deviceID);
    radio.enableAckPayload();
    radio.writeAckPayload(1, ackData, ackLen);
    radio.startListening();
}

void loop() {
    
    if ( radio.available() ) {
        radio.read( dataReceived, sizeof(dataReceived) );
        Serial.print("Data0 ");
        Serial.print(dataReceived[0]);
        Serial.print(" Data1 ");      
        Serial.println(dataReceived[1]);
        radio.writeAckPayload(1, ackData, ackLen);
        ackData[0] += valChange; // this just increments so you can see that new data is being sent
    }
}

They are cut-down versions of code I use to control model trains.

…R

Robin2: You might like to try these two programs

...R

Thanks, I will try them out soon. But what about the pin locations? Take a look at this: https://github.com/TMRh20/RF24/blob/master/RF24_config.h#L70

By what that says: MISO is pin 10, MOSI is pin 9, and SCK is pin 8. Other websites say different but they aren't as updated. So I am assuming this is the pinout I go with, right? (Relative to the ATMega328p)

Have a look at the Arduino Pin mapping for the Uno

On an Uno pins 11, 12 and 13 are used by SPI (Mosi, Miso and Sck) and they are pins 17,18 and 19 on the DIP Atmega 328.

That link you have posted refers to something called SOFT_SPI. I have no idea what that is. The code I posted works on my Mega and Uno. Note that the SPI pins are different on a Mega.

...R

Robin2: Have a look at the Arduino Pin mapping for the Uno

On an Uno pins 11, 12 and 13 are used by SPI (Mosi, Miso and Sck) and they are pins 17,18 and 19 on the DIP Atmega 328.

That link you have posted refers to something called SOFT_SPI. I have no idea what that is. The code I posted works on my Mega and Uno. Note that the SPI pins are different on a Mega.

...R

Ah, I see. Well that and the fact I wired it up backwards... :P

Anyways, using your code as a reference, I was able to create these two files. Note that one file is loaded on an ATMega328p and another on a Raspberry Pi B+. They are able to successfully communicate, but the data the Arduino receives isn't the same as I transmit from the Raspberry Pi.

Arduino: http://hastebin.com/ovaxasicic.coffee Raspberry Pi: http://hastebin.com/gatuzadoha.vala

The Pi is the transmitter, and it is transmitting an array of integers defined like this:

  int data[5];
  data[0] = 1;
  data[1] = 123;
  data[2] = 123;
  data[3] = 123;
  data[4] = 5;
  bool ok = radio.write( &data, sizeof(unsigned long) );

I seem to get a response of

  data[0] = 1
  data[1] = 0
  data[2] = 0
  data[3] = 0
  data[4] = 0
  // or as in the case of the serial output:
  On/Off: 1, RGB: [0, 0, 0], FadeSpeed: 0

I noticed you had changed the sizeof(unsigned long) to sizeof(dataToSend), so I did the same(For my data variable). But now my serial console is being constantly spammed and the raspberry pi output is reporting that the radio.write failed.

I did a quick check and my Arduino reports the size of the recieved data being 10, while the Pi says the data it's transmitting is 20. Any ideas of what's going on here?

And thanks for your help so far!

Please post your code in this Forum.

...R

RPi is 32-bit system while Arduino is 8-bit.

Long explanation: https://en.wikipedia.org/wiki/Data_structure_alignment

Short explanation: int on Arduino is 2-bytes (16-bits), int on RPi is 4-bytes ( http://www.tutorialspoint.com/cplusplus/cpp_data_types.htm )

One Solution: Change int to int16_t (uses a two-byte integer instead of 4) and send the correct length for the size of your data ( sizeof(data); )