Arduino RF communications using the nRF24L01+

The nRF24L01+ is a great little device to allow RF data exchange between two or more arduino devices. They are very inexpensive and you can purchase 4 units from ebay for under £5. There isn’t a library for the module within the core ardino libraries and the best library I found is written by a guy called manicbug and you can find information on how to download and install the library on his website . The rest of this post implies you’ve installed the library to your ardunio’s library directory.

I learnt a lot thanks to manicbug’s blog and various other posts which I will note at the end of this post if you want further background reading.

Regrettably, I could not get any of the examples working correctly despite checking and rechecking the connections, configuration. As the code was a little hard to understand ( I hate classes,pointers and anything OOP) I found I could not penetrate the code successfully. This is a limitation on my part and isn’t a slight against the library’s author.

So, I went on a search on the arduino forum and towards the end of the discussion there are mentions of powering up and down the radio device to ensure successful transmission. I then implemented the example provided on shanes.net with the mods provided by mlc and the packet exchange was then flawless. My modified code is shown below – This is the transmitter.

#include <SPI.h>;
#include <nRF24L01.h>;
#include <RF24.h>;
#include <RF24_config.h>;
/*
This sketch sends a string to a corresponding Arduino
with nrf24 attached. It appends a specific value
(2 in this case) to the end to signify the end of the
message.
*/
int success,fail;
char msg[1];
int i=77; // Lowercase 'a'
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
 
void setup(void)
{
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
}
 
void loop(void){
radio.powerUp();
delay(1);
// int messageSize = theMessage.length();
// for (int i = 0; i &lt; messageSize; i++) {
// int charToSend[1];
// charToSend[0] = theMessage.charAt(i);
// radio.write(charToSend,1);
// }
//send the 'terminate string' value...
msg[0] = 'H';
bool h=radio.write(msg,1);
if (h==false)
{
Serial.println("Failed to tx HIGH ");
fail++;
Serial.print("failed to send ");
Serial.print(fail);
Serial.println(" packets");
 
}
else
Serial.println("Sent HIGH OK");
radio.powerDown();
delay(500);
 
/*delay sending for a short period of time. radio.powerDown()/radio.powerupp
//with a delay in between have worked well for this purpose(just using delay seems to
//interrupt the transmission start). However, this method could still be improved
as I still get the first character 'cut-off' sometimes. I have a 'checksum' function
on the receiver to verify the message was successfully sent.
*/
 
radio.powerUp();
delay(1);
msg[0] = 'L';
bool l=radio.write(msg,1);
if (l==false)
{
Serial.println("Failed to tx LOW ");
fail++;
Serial.print("failed to send ");
Serial.print(fail);
Serial.println(" packets");
}
else
Serial.println("Sent LOW OK");
delay(500);
 
radio.powerDown();

 msg[0]=i;
 radio.powerUp();
 
delay(1);
bool shite=radio.write(msg,1);
i++; // incremenet counter to next alphanumeric character

if (i>122) i=77; // 122='z' reset to 'a'



if (shite==false)
{
Serial.println("Failed to tx random char ");
fail++;
Serial.print("failed to send ");
Serial.print(fail);
Serial.println(" packets");
}
else
Serial.println("Sent random char OK");
}

Receiver Code.

#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
 
/*
This sketch receives strings from sending unit via nrf24 
and prints them out via serial.  The sketch waits until
it receives a specific value (2 in this case), then it 
prints the complete message and clears the message buffer.
*/
 
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
// int lastmsg = 1;
// String theMessage = "";=
void setup(void){
  Serial.begin(9600);
  pinMode(8,OUTPUT);
  digitalWrite(8,LOW);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();
}
void loop(void){
  if (radio.available()){
    bool done = false;  
      done = radio.read(msg, sizeof(msg)); //1); 
   //   char theChar = msg[0]
      
      if (msg[0]==76) // got character 'L'
      {
        digitalWrite(8,LOW);
        Serial.print("rx = ");
        Serial.print(msg[0]);
        Serial.println("");
      }
      else if (msg[0]==72)  // got character 'H'
      {
        digitalWrite(8,HIGH);
        Serial.print("rx = ");
        Serial.print(msg[0]);
        Serial.println("");
      }
      
      else // a character was received, but it's one we weren't expecting !!!
      {
      Serial.print("Received invalid data ");
      Serial.print(msg[0]);
      char ewwor=msg[0];
      Serial.print(" which is the character ");
      Serial.println(ewwor);
      }
   }

}

There ya go! Happy new year 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *