NRF24 sketches that work for Nano

I have tried a few article sketches for the NRF24 device, and tried using Arduino Nano. But so far nothing has worked or simple enough for me to try and understand how to adapt for what I want.

Basically want 4 pushbuttons (Transmitter) and 4 LEDS (Receiver) that momentarily light up when holding down buttons. Can anybody please suggest a link to an example that does work and could be modified the way I want it. Many thanks

Your question is too vague to merit serious consideration. You have some code that does something. You did not post the code, explain what it does, or explain what the problems with the code are.

You may find Robin2's links helpful:
https://forum.arduino.cc/index.php?topic=396844.msg2730404#msg2730404

However, a quick search turned up this project which is capable of forward-reverse and left-right:

Ray

mrburnette:
You may find Robin2's links helpful:

I hope this Simple nRF24L01+ Tutorial will be more useful for a beginner as it draws everything together that is needed to get started. I have also tried to make the examples simpler.

...R

Many thanks mrburnette, Robin2, just what I wanted. All my previous searches led to articles that threw up errors, which were then highlighted in reviews from others later in the articles.

Your links look ideal for what I was looking for, thanks again

When you are comfortable that you got your question answered, you may wish to casually read the ramblings of this site:
Low Power Lab

There are many tidbits and snippet of code that I have found very valuable over time.

Ray

Thanks again.

However all my examples all contain the error on :

done = radio.read(msg, 1);

Seen so many people bogged down with this but no clear remedy?

Anybody know? Thanks

ZOR2:
Thanks again.

However all my examples all contain the error on :

done = radio.read(msg, 1);

Seen so many people bogged down with this but no clear remedy?

Anybody know? Thanks

Take a look at the date on the article or github... often older code coughs up a fur-ball like an old cat when used on the newest IDE.

However, I did a quick Google and found this:
https://forum.arduino.cc/index.php?topic=302380.msg2103660#msg2103660

Maybe that will fix things...

FYI.... how did I find it and you did not? Well, Google is an interesting search engine and it helps to put queries to Google in a "context" setting ... in this case, I simply added the single word "error" as a pre-fix to the done = .... statement.

Ray

Thanks again. I am using the RF24-master version, but still a problem. I think I will have a last look round for code that works for 2 nano boards without errors to sort out. I simply just want the TX to have a pushbutton that when held down lights an LED on the receiver. Something I can then add further buttons and further LED's to. Just a simple one way transmission. I don't want to go backwards from Arduino to a Picaxe solution.

For the examples that have been suggested in this thread, you have to ensure you are using this RF24 library:

Otherwise you may get the classic "radio.read()" error.

The Nano, incidentally, has a poor 3.3 volt output ( about 50mA for some clones and derived from the USB chip) which may cause some problems with the RF24. You can get an adapter to allow the RF24 to use a 5 volt supply to get around this problem.

If you get a compilation error, post the full code here and error message, stating where you obtained the RF24 library and you should get a suggestion .

Get the RF24 library from here:

And try these 2 sketches (they work for me).

// nRF24_rx

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

RF24 radio(9,10); // CE on pin 9, CSN on 10

byte rxbuf[1];
byte led1 = 2, // connect LED anode to pin, 330 - 560 Ohm
     led2 = 3, // resistor to cathode other end of resistor
     led3 = 4, // to GND
     led4 = 5;

void setup()
{
  //Serial.begin(9600);
  for(byte i = 2; i <= 5;i++)
    pinMode(i,OUTPUT);
  radio.begin();
  radio.setRetries(15,15);
  radio.setPayloadSize(1);
  radio.openReadingPipe(1,0xF0F0F0F0D2);
  radio.setDataRate( RF24_250KBPS );
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();
}

void loop()
{
  boolean done;
  
  while(!radio.available());
  if(radio.available()){
    done = false;
    while (!done){
      radio.read(&rxbuf, 1);
      done = true;
    }
  for(byte i = 2; i <= 5;i++)
    digitalWrite(i,! bitRead(rxbuf[0],i - 2));

  }
}
// nRF24_tx

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

RF24 radio(9,10); // CE on pin 9, CSN on 10

byte txbuf[1];
byte button1 = 2, // connect button between pin 2 & GND
     button2 = 3, // "                          3    " 
     button3 = 4, // "                          4    "
     button4 = 5; // "                          5    "

void setup()
{
  //Serial.begin(9600);
  for(byte i = 2;i <= 5;i++)
    pinMode(i,INPUT_PULLUP);
  radio.begin();
  radio.setRetries(15,15);
  radio.setPayloadSize(1);
  radio.openWritingPipe(0xF0F0F0F0D2);
  radio.setPALevel( RF24_PA_MAX );
  radio.setDataRate( RF24_250KBPS );
}
void loop()
{
  for(byte i = 0;i < 4;i++)
    bitWrite(txbuf[0],i,bitRead(PIND,i + 2));
  
  radio.write( &txbuf, 1);
  delay(100);

}

ZOR2:
However all my examples all contain the error on :

done = radio.read(msg, 1);

My examples in the link I gave you in Reply #3 do not.

As explained in that link I recommend that you do NOT use the ManiacBug version of the RF24 library.

...R

Thanks outsider, I will try your sketches later, although Robin2 suggests not using maniacbug/RF24 library (maybe just in Robins sketches?)

I am moving forward after using 6v6gt library suggestion, although I did still get the done = radio.read(msg,1) error. I found if I removed the word "done" the error went away, you can see this in my receiver sketch.

I have the following problems with where I am.

  1. Looking at the receiver serial monitor, the value of 111 is continually received, and if I press the pushbutton it alternates between 111 and 222. I want it to send nothing if buttons not pressed.
  2. Only want transmissions made if a button gets pressed, the current loop is active all the time.

I am basically trying to do a 4 button remote to pan and tilt a camera, so most of the time the camera is left static.

Transmit

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

int msg[1];

RF24 radio(7,8);
const uint64_t pipe = 0xE8E8F0F0E1LL;

int SW1 = 9;         // asign push button pin 9
int SW2 = 10;       // asign push button pin 10

void setup(void){
 Serial.begin(9600);
 radio.begin();
 radio.openWritingPipe(pipe);}

void loop(void){
 if (digitalRead(SW1) == LOW);      //  button SW1 pressed
    msg[0] = 111;                      
    radio.write(msg, 1); 
  if (digitalRead(SW2) == LOW){     //  button SW2 pressed
    msg[0] = 222;                        
   radio.write(msg, 2);
}}

Receiver

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

int msg[1];
RF24 radio(7,8);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int LED1 = 4;     // led pin 4
int LED2 = 5;     // led pin 5

void setup(void){
 Serial.begin(9600);
 radio.begin();
 radio.openReadingPipe(1,pipe);
 radio.startListening();
 pinMode(LED1, OUTPUT);
 pinMode(LED2, OUTPUT);}

void loop(void){
 if (radio.available()){
   bool done = false;   
   while (!done){
    //done = 
    radio.read(msg, 1);     
     Serial.println(msg[0]);
     if (msg[0] == 111){delay(10);digitalWrite(LED1, HIGH);}
        if (msg[0] == 222){delay(10);digitalWrite(LED2, HIGH);}
     else {digitalWrite(LED2, LOW);}
     delay(100);}}
 else{Serial.println("No radio available");}}

Thanks also to Robin2 and mrburnette

This example (RF24) highlights one of the things about open source software which I am sad to say drives me up the wall.
Generally there is no hierarchical control and a lot of well meaning people producing often good code with sometimes good documentation maybe according to some design rules all tripping over each other.

In the specific case of RF24, there was already an established library with a clear interface and a massive amount of code already written. That was Maniac Bug. It was not, however without its flaws.

Then a new library emerged, (THRH20) which usurped the original name but changed the behaviour of some of the methods, rendering much of the old applications and examples invalid. There is no doubt that at a technical level, the new library is a big improvement on the old, but the keeping the same name while changing the interface was a very bad move in my opinion.

The situation appears even worse with DS1307 / DS3231 clock chips with a proliferation of different libraries, some of which exhibit bizarre behaviour or are littered with all sorts of useless features which cause endless problems for the unwary.

The solution I see is some sort of central library catalog (why not on this site?) where a sort of reputation type scoring system could help potential users "separate the wheat from the chaff"

Having said all that, I'm thankful for the huge amount of effort that has gone into the massive body of software that is available for the Arduino and that it is all free is amazing.

6v6gt:
the new library is a big improvement on the old, but the keeping the same name while changing the interface was a very bad move in my opinion.

Agree 110%, and I have made the same point several times.

But the TMRh20 version of the library works in cases where the ManiacBug version does not.

...R

Sorry. In that rant I forgot about the OP's problem.

This is wrong because you are sending an integer (2 Bytes) :

   radio.write(msg, 1);

it should be

   radio.write(msg, 2);    // integer = 2 bytes

Can someone tell me why this loop is sending out 111/222/111/222 etc when both buttons are high?

void loop(void){
 if (digitalRead(SW1) == LOW);      //  button SW1 pressed
    msg[0] = 111;                     
    radio.write(msg, 1);
  if (digitalRead(SW2) == LOW){     //  button SW2 pressed
    msg[0] = 222;                       
   radio.write(msg, 2);
}}

No, because it will not.

It will send 111 when both buttons are HIGH.

The problem is the missing bracketing and the superfluous semicolon in your first if statement.

 if (digitalRead(SW1) == LOW);      //  button SW1 pressed

There is still one error in

    radio.write(msg, 1);
void loop(void){
 if (digitalRead(SW1) == LOW){      //  button SW1 pressed
    msg[0] = 111;                     
    radio.write(msg, 1);
    }
  if (digitalRead(SW2) == LOW){     //  button SW2 pressed
    msg[0] = 222;                       
   radio.write(msg, 2);
   }
}

radio.write(msg, 2);

What does the 2 here do? Is that to send out 2 bytes, is that what you meant Whandall?