nRF24L01+ sketch works on ESP8266 but not on Arduino?

hi all,

So i have a simple nRF24L01 sketch that should transmit 1Hz signal, i’ve first set it up with D1Mini and it works like its supposed to work, but then i wanted to switch to arduino that is to atmega328p for easier integration in my project, only to discover that it does not work for some reason…

So the steps that i did to debug:

  1. My sketch works on D1Mini - so the sketch is working!
  2. Different (NRF demo) sketch works with my Arduino setup - so both Arduino and nRF24L01+ modules are working and connected ok.
  3. i have tested 3 different boards: Nano, UNO and Mega, as well as bare atmega328p, none worked.
    This has lead me to believe that the problem is a combination of my sketch and arduino, for whatever reason.

This is my D1Mini connection:

GND ==› GND
3.3V ==› 3.3V
CE ==› D2
CS ==› D8
CSK ==› D5
MOSI ==› D7
MISO ==› D6

This is my UNO/Nano connection:

GND ==› GND
3.3V ==› 3.3V (or using NRF adapter that has AMS1117 to reduce 5V to 3.3V)
CE ==› 9
CS ==› 10
CSK ==› 13
MOSI ==› 11
MISO ==› 12

and lastly, this is my sketch

TRANSMITTER:

#include <SPI.h>                            //Communication Interface with modem
#include "nRF24L01.h"
#include "RF24.h"                          //library to control radio modem
 
RF24 radio(9,10);                          //Creating an object
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
unsigned long Command ;
void setup() {
    Serial.begin(115200);
    pinMode(5,INPUT_PULLUP);
    radio.begin();                        //activates modem
    radio.setDataRate(RF24_2MBPS);
    radio.setRetries(0,2);              //number of times modem retry to send data
    radio.openReadingPipe(1,pipes[1]);
    radio.startListening();
    radio.printDetails();
    radio.openWritingPipe(pipes[0]);     //sets address to receiver
    radio.openReadingPipe(1,pipes[1]);
    radio.stopListening();               //switch the modem to data transmission mode
   }
 
  void loop(void) {
    radio.stopListening();
    if (digitalRead(5)==1){
    Command=1;
   } else {
        Command=2;
      }
    radio.write( &Command, sizeof(unsigned long) );  //blocks the program until it receives the acknowledgment
    radio.startListening();
  }

RECEIVER:

#include <SPI.h>                           //Communication Interface with modem
#include "nRF24L01.h"
#include "RF24.h"                          //library to control radio modem

#define led 4
 
RF24 radio(9, 10); // CE, CSN

const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
typedef enum { role_ping_out = 1, role_pong_back } role_e;
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
 
role_e role = role_pong_back;

void setup(void)
   {
    Serial.begin(115200);
    pinMode(led,OUTPUT);
    radio.begin();                         //activates modem
    radio.setDataRate(RF24_2MBPS);
    radio.setRetries(0,2);
    radio.openReadingPipe(1,pipes[1]);
    radio.startListening();
    radio.printDetails();
    radio.openWritingPipe(pipes[1]);
    radio.openReadingPipe(1,pipes[0]);
    radio.startListening();
   }
 
 void loop(void)
   {
    if ( radio.available() )               //check receive data
       { 
          unsigned long data = 0;
          radio.read( &data, sizeof(unsigned long) );
          Serial.println(data);
             if (data==1)
                  {
                    digitalWrite(led,LOW);
                  
                  }
            else
                  {
                    digitalWrite(led,HIGH);
                    
                  }
                 
       }
    }

I have tried, without success:

  1. Changing baud rate
  2. removing 2Mbps baud rate for transmission
  3. removing number of transmission retries

Many thanks for any tips,
Alek

Check which boards are supported by the libraries.

Thanks DrDiettrich.
Yes, these libraries are supported by all boards i am using as well as bunch of similar sketches i have found on the net, its just a confusing why this specific sketch is not working on ANY arduino boards…
This one suits my needs as it has acknowledge of receipt message returned to transmitter.

thanks,
Alek

i got it working!!!

Turned out this was the issue:

THIS:
#include <SPI.h>
#include “nRF24L01.h”
#include “RF24.h”

Needs to be like this for Arduino:
#include <nRF24L01.h>
#include <RF24.h>
#include <SPI.h>

So brackets instead of quotes. Anyone could shed some light on why is this an issue?

In any case, this is now solved!

Thanks,
Alek

Quoted files are searched in the user/project folders first. Here inadequate files may be located on your system. Bracketed files are searched only in the system library folders.

1 Like

ok, false solution as more strange things keep happening…

so, now NANO V3 works, sort of… but, if i use this in the sketch:
RF24 radio(8,10); // CE, CSN

Then it only works if i move CE to 9, even though it defines CE as 8 in the sketch but even weirder, if i define CE as 9 in the sketch, it only works if i connect CE to 8 on nano… amazing

Also, the same sketch, whatever i do does not work on UNO or bare atmega328.

both UNO and nRF24L01 module are functional, so it has to be the sketch.

thanks for any tips,
Alek

ok, more strange things, i have swapped atmega328p-pu with atmega328p-u and it works…
crazy

Arduino pin numbers are not controller pin numbers but board pin numbers. For each bare controller or board you need the matching descriptor file.

yes, i am aware. i have used both MCU as bare and one works while the other doesn’t. then test both on UNO board with random sketches, both work, then testing nRF24L01 sletch on both while on UNO board, again the same one wprks amd the other doesn’t.
not sure what the issue could be.

irony is, the one doesn’t work was purchased from moiser as original and the other one that works came from knockoff arduino UNO.
p-pu bought from mouser came with a bootloader.

thanks,
alek

If you have a working setup, and you sid you had, then it ought to be simple enough to work out which modules are OK and which are faulty.

IIRC D10 should be defined as OUTPUT on 328p based systems to guarantee SPI master behavior,
that could explain some of your errors.
Using it as one of the control pins takes care of that.

not sure if you understood what the problem is, i am not trying to figure out which one works and which one doesnt.
i have two atmega328 MCUs, and i understand they should be the same at least function wise, but one of those 2 MCUs works with my sketch and one doesnt, my goal is to find out why.

Both MCU chips WORK, i have tested them bare and on UNO board with various sketches, both work with all 5 random sketches i used, its just the one (p-pu) does not working with this specific sketch.

thanks,
Alek

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.