nRF24L01 Interfacing with Arduino | Wireless Communication

nRF24L01 Interfacing with Arduino | Wireless Communication

In the first example, we will send “Hello world” command to blink the LED. In the second example, we will do bidirectional communication.

  • 164,973 views
  • 38 comments
  • 79 respects

Components and supplies

Apps and online services

About this project

In this tutorial, you will learn about nRF24L01 Arduino interfacing with the help of two examples. In the first example, we will send “Hello world” and a command to blink the LED connected to the other Arduino. In the second example, we will do the bidirectional control and will send the command from First Arduino to blink the LED on the second and then we will send the command from second Arduino to blink the LED on the first.

For Custom Projects, hire me at https://www.freelancer.com/u/Muhammadaqibdutt

Before going in detail, first have a look at the specification of this module

nRF24L01 Module

The nFR24L01 is a transceiver module which means that it can both send and receive the data.

These modules are very cheap, smaller in size and has a lot of specifications. Some of the specifications of these modules are as follows

Specifications of nRF24L01 Module

  • Power consumption is around 12mA during transmission which is even lesser than the led.
  • It can operate with baud rates from 250Kbps up to 2 Mbps.
  • Its range can reach up to 100 meters if used in open space and with antenna.
  • It can both send and receive the data simultaneously.
  • Each module can communicate with up to 6 other modules.
  • It uses the 2.4 GHz band.
  • It can send 1 to 25 bytes of raw data at the transmission rate of 1 MB.
  • It has 125 different channels.

Pinout

The nRF24L01 module works with the Arduino through the SPI communication. The pinout of the module is as follows

The operating voltage of this module is from 1.9 to 3.6V but the other pins are 5V tolerant which means that the other pins can be directly connected to the Arduino.

The MOSI, MISO and the SCK are the SPI pins and these needs to be connected to the SPI pins of Arduino. Different Arduino’s have different SPI pins.

The CSN and CE are for setting the module in active mode and for switching between command and transmit mode. These can be connected to any digital pins of Arduino.

The IRQ pin is the interrupt pin and you don’t have to connect it.

Example 1 - nRF24L01 Arduino Interfacing

In the first example for nRF24L01 arduino interfacing, we are going to simply send the data from one Arduino to other Arduino. When we will press the button connected to the first Arduino, LED connected to the second Arduino will light up.

The circuit diagram for the first example is shown below and the connections are shown below that.

Code for Transmitter

Download the library from here.

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001"; //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side.
int button_pin = 2;
boolean button_state = 0;
void setup() {
pinMode(button_pin, INPUT);
radio.begin(); //Starting the Wireless communication
radio.openWritingPipe(address); //Setting the address where we will send the data
radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
radio.stopListening(); //This sets the module as transmitter
}
void loop()
{
button_state = digitalRead(button_pin);
if(button_state == HIGH)
{
const char text[] = "Your Button State is HIGH";
radio.write(&text, sizeof(text)); //Sending the message to receiver
}
else
{
const char text[] = "Your Button State is LOW";
radio.write(&text, sizeof(text)); //Sending the message to receiver 
}
radio.write(&button_state, sizeof(button_state)); //Sending the message to receiver 
delay(1000);
}

Code for Receiver

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001";
boolean button_state = 0;
int led_pin = 3;
void setup() {
pinMode(6, OUTPUT);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address); //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN); //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
radio.startListening(); //This sets the module as receiver
}
void loop()
{
if (radio.available()) //Looking for the data.
{
char text[32] = ""; //Saving the incoming data
radio.read(&text, sizeof(text)); //Reading the data
radio.read(&button_state, sizeof(button_state)); //Reading the data
if(button_state == HIGH)
{
digitalWrite(6, HIGH);
Serial.println(text);
}
else
{
digitalWrite(6, LOW);
Serial.println(text);}
}
delay(5);
}

Video

Example 2 - nRF24L01 Arduino Interfacing

In the second example for nRF24L01 Arduino interfacing, we are going to do the bidirectional communication. First, we will send the command from the first Arduino to light up the LED connected to the second Arduino and then we will send the command from the second Arduino to light up the LED connected to the first Arduino.

Code for First Arduino

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte addresses [][6] = {"00001", "00002"}; //Setting the two addresses. One for transmitting and one for receiving
int button_pin = 2;
int led_pin = 3;
boolean button_state = 0;
boolean button_state1 = 0;
void setup() {
pinMode(button_pin, INPUT);
pinMode(led_pin, OUTPUT);
radio.begin(); //Starting the radio communication
radio.openWritingPipe(addresses[1]); //Setting the address at which we will send the data
radio.openReadingPipe(1, addresses[0]); //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver. 
}
void loop()
{
delay(5);
radio.stopListening(); //This sets the module as transmitter
button_state = digitalRead(button_pin);
radio.write(&button_state, sizeof(button_state)); //Sending the data
delay(5);

radio.startListening(); //This sets the module as receiver
while(!radio.available()); //Looking for incoming data
radio.read(&button_state1, sizeof(button_state1)); //Reading the data
if (button_state1 == HIGH)
{
digitalWrite(led_pin, HIGH);
}
else
{
digitalWrite(led_pin, LOW);
}
}

Code for Second Arduino

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte addresses [][6] = {"00001", "00002"}; //Setting the two addresses. One for transmitting and one for receiving
int button_pin = 2;
boolean button_state = 0;
boolean button_state1 = 0;
int led_pin = 3;
void setup() {
pinMode(led_pin, OUTPUT);
Serial.begin(9600);
radio.begin(); //Starting the radio communication
radio.openWritingPipe(addresses[0]); //Setting the address at which we will send the data
radio.openReadingPipe(1, addresses[1]); //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
}
void loop()
{
delay(5);
radio.startListening(); //This sets the module as receiver
if (radio.available()) //Looking for incoming data
{
radio.read(&button_state, sizeof(button_state));
if(button_state == HIGH)
{
digitalWrite(led_pin, HIGH);
}
else
{
digitalWrite(led_pin, LOW);
}
delay(5);

radio.stopListening(); //This sets the module as transmitter
button_state1 = digitalRead(button_pin);
radio.write(&button_state1, sizeof(button_state1)); //Sending the data
}
}

Video


Code

Example 1 TransmitterArduino
    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>
    RF24 radio(9, 10); // CE, CSN         
    const byte address[6] = "00001";     //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side.
    int button_pin = 2;
    boolean button_state = 0;
    void setup() {
    pinMode(button_pin, INPUT);
    radio.begin();                  //Starting the Wireless communication
    radio.openWritingPipe(address); //Setting the address where we will send the data
    radio.setPALevel(RF24_PA_MIN);  //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
    radio.stopListening();          //This sets the module as transmitter
    }
    void loop()
    {
    button_state = digitalRead(button_pin);
    if(button_state == HIGH)
    {
    const char text[] = "Your Button State is HIGH";
    radio.write(&text, sizeof(text));                  //Sending the message to receiver
    }
    else
    {
    const char text[] = "Your Button State is LOW";
    radio.write(&text, sizeof(text));                  //Sending the message to receiver 
    }
    radio.write(&button_state, sizeof(button_state));  //Sending the message to receiver 
    delay(1000);
    }
Example 1 ReceiverArduino
    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>
    RF24 radio(9, 10); // CE, CSN
    const byte address[6] = "00001";
    boolean button_state = 0;
    int led_pin = 3;
    void setup() {
    pinMode(6, OUTPUT);
    Serial.begin(9600);
    radio.begin();
    radio.openReadingPipe(0, address);   //Setting the address at which we will receive the data
    radio.setPALevel(RF24_PA_MIN);       //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
    radio.startListening();              //This sets the module as receiver
    }
    void loop()
    {
    if (radio.available())              //Looking for the data.
    {
    char text[32] = "";                 //Saving the incoming data
    radio.read(&text, sizeof(text));    //Reading the data
    radio.read(&button_state, sizeof(button_state));    //Reading the data
    if(button_state == HIGH)
    {
    digitalWrite(6, HIGH);
    Serial.println(text);
    }
    else
    {
    digitalWrite(6, LOW);
    Serial.println(text);}
    }
    delay(5);
    }
Example 2 First ArduinoArduino
    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>
    RF24 radio(9, 10); // CE, CSN
    const byte addresses [][6] = {"00001", "00002"};  //Setting the two addresses. One for transmitting and one for receiving
    int button_pin = 2;
    int led_pin = 3;
    boolean button_state = 0;
    boolean button_state1 = 0;
    void setup() {
      pinMode(button_pin, INPUT);
      pinMode(led_pin, OUTPUT);
      radio.begin();                           //Starting the radio communication
      radio.openWritingPipe(addresses[1]);     //Setting the address at which we will send the data
      radio.openReadingPipe(1, addresses[0]);  //Setting the address at which we will receive the data
      radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver. 
    }
    void loop() 
    {  
      delay(5);
      radio.stopListening();                             //This sets the module as transmitter
      button_state = digitalRead(button_pin);
      radio.write(&button_state, sizeof(button_state));  //Sending the data
      delay(5);
      
      radio.startListening();                            //This sets the module as receiver
      while(!radio.available());                         //Looking for incoming data
      radio.read(&button_state1, sizeof(button_state1)); //Reading the data
      if (button_state1 == HIGH)
      {
        digitalWrite(led_pin, HIGH);
      }
      else
      {
        digitalWrite(led_pin, LOW);
      }
    }
Example 2 Second ArduinoArduino
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 10); // CE, CSN
const byte addresses [][6] = {"00001", "00002"};    //Setting the two addresses. One for transmitting and one for receiving
int button_pin = 2;

boolean button_state = 0;
boolean button_state1 = 0;
int led_pin = 3;

void setup() {
  pinMode(led_pin, OUTPUT);
  Serial.begin(9600);
  radio.begin();                            //Starting the radio communication
  radio.openWritingPipe(addresses[0]);      //Setting the address at which we will send the data
  radio.openReadingPipe(1, addresses[1]);   //Setting the address at which we will receive the data
  radio.setPALevel(RF24_PA_MIN);            //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
}

void loop() 
{
  delay(5);
  radio.startListening();                    //This sets the module as receiver
  if (radio.available())                     //Looking for incoming data
  {
    radio.read(&button_state, sizeof(button_state));
    if(button_state == HIGH)
  {
     digitalWrite(led_pin, HIGH);
  }
  else
  {
     digitalWrite(led_pin, LOW);
  }
  delay(5);
  
  radio.stopListening();                           //This sets the module as transmitter
  button_state1 = digitalRead(button_pin);
  radio.write(&button_state1, sizeof(button_state1));   //Sending the data
  }
}
NRF24l01 Library

OSI Layer 2 driver for nRF24L01 on Arduino & Raspberry Pi/Linux Devices — Read More

Latest commit to the master branch on 5-14-2021

Download as zip

Schematics

Second Example Circuit Diagram
Nrf24l01 arduino bidirectional bb q1poee9hcv
First Example Circuit Diagram
Nrf24l01 arduino bb j5h63vjhsf

Comments

cedric1980

cedric1980

2 years ago

Thanks a lot for this project, just realised it !
I just have a little problem, after 1 minute or less, I loose communication between the two boards and I have to reset it. I use a breakout bord (5 v to 3,3 V) and use the 5 Vdc output of my uno boards.

Thanks

Cédric

Aqib

Aqib

2 years ago

Try using a different address.

Tom George

Tom George

2 years ago

Check that your 5V supply is sufficient to keep the 3.3V regulated.
A trick is to put a 10uF 16V capacitor across the 3.3v at the NRL.
When it transmits it causes the current to jump, the capacitor will help supply that current increase.
.

eldeer

eldeer

2 years ago

it does not work

PEPONNETL

PEPONNETL

2 years ago

Hello; thanks to you for this tuto;
Little mistake on receiver code for the first exemple; you don't use the variable led_pin in methods PinMode() and digitalWrite().
thanks again!

Hewnikel

Hewnikel

2 years ago

Thanks a lot.. however I have a weird case happening..
I am using the NRF24L for communication in a car chassis project including a car and remote control, in addition to an Ultrasonic Sensor HC - SR04 in the car for distance measurement.
the problem is when the receiver (car) has "radio.begin()" instruction in its code, the sensor doesn't work and serial keeps showing 0 distance. when I remove (or comment) the radio.begin() instruction, the sensor starts showing distance data correctly! (even though i still didn't attach the NRF24L module yet, only code is done).
has anyone faced it before??!!

elnur444

elnur444

2 years ago

Thanks for the best information.

arehaslum

arehaslum

a year ago

I've looked all over the web, but can't seem to find a solution for this using
Arduino Micro. I've tried using "while (!Serial);" in the setup, but can not see anything on the monitors. Does anyone have any experience using the NRF24L01 and Arduino Micro? Seems to me that this is an unsolved problem?

Pat2210

Pat2210

a year ago

Hello, I have a question about example 1. Can I plug a button on one arduin to one port while plugging an LED into the same port on the other arduin?

hermanb3

hermanb3

a year ago

Hi Mohammad,

Thank you for this information.

I am busy with a simple project to detect a switch and transmit the status to another Arduino where a relay will operate to indicate the change in status. Your example code is simple enough for me to understand as a complete newbie to Arduino programming. I do have some programming experience in Assembler.

Everything works perfectly the first time around. The only thing I picked up though is that if I open up the Serial Monitor on the Arduino the Receiver doesn't operate that well anymore.The data displayed is also not legible every time. With the monitor closed everything works fine.

I do have two questions though. Firstly, is there a way to detect if the communication fails between the two nRF24L01 modules? If there is no activity on the switch, how will I know if it still works after a day or two? I would like to maybe have a slow flashing LED on the receiver to indicate that the communication is still going. Secondly, is it possible to transmit only when the switch closes to save on battery power? (I will be using a small battery for the transmitter).

Keep up the good work.

Nartarox

Nartarox

a year ago

Hello, I tested the program "an arduino transmitter and an arduino receiver" with NANO arduinos, it doesn't work. So I would like to know:
How do I know that the NRF24 modules work?
How do I know that the module actually sends data?
How do I know if the module is receiving?

  • 1 thank
Fowelld

Fowelld

10 months ago

Hello, I have also tested the program "an arduino transmitter and an arduino receiver" with NANO arduinos, it doesn't work I have also tested the programs with Arduino UNO's and could not get them to work. I have tried with different RNF24L01's and got the same result So I would like to know:
How do I know that the NRF24 modules work?
How do I know that the module actually sends data?
How do I know if the module is receiving?

alanmjsundo

alanmjsundo

9 months ago

it works, just put the RX input to Pin 6 or change the code to Pin2(Pin 6 is written)

Maddy Sharma

Maddy Sharma

8 months ago

it still not works need help

lightthedreams

lightthedreams

9 months ago

thank you for a good tutorial. it works but after some modification refer to the previous comment about the variable on the output pin. i have one question why i got my data transmitted seems like little bit lag/delay when i push the button and the led is on. any advise?

lightthedreams

lightthedreams

9 months ago

it's now ok for the 1 way communication is working properly. thank you

now, i am trying to make the 2 ways communication. 1-2 times are ok, but later there comes problem seems like there is crash between the program. could you advise me? thanks

lightthedreams

lightthedreams

9 months ago

Hello. Thank you again. After several trial and error to modify the sketch, finally i can fix it and the two ways communication working smoothly. If you guys faced the similar problem, you may modify the variable name between those transmitter and receiver. For example use Led_A for the first Arduino and use Led_B for the second Arduino. Also apply to the Button and Button_state. Good luck!

Maddy Sharma

Maddy Sharma

8 months ago

hi
can you explain what modification you made ?

Maddy Sharma

Maddy Sharma

8 months ago

for both communication.

lightthedreams

lightthedreams

7 months ago

check my project. sorry didn't see the notification

1161102535

1161102535

9 months ago

The led is not blinking

1161102535

1161102535

9 months ago

can anyone help with it? i have use the same code without any modification to it

victorwiller_gtba

victorwiller_gtba

9 months ago

Same with me, I used the same code (the seconde one) and the LEDs didn't turn on. If someone knows the reasson please tell us. Thnx in advance!

ktcphoto

ktcphoto

9 months ago

Hello,

I ran into a problem with the first sketch (communication one way); the boards would not communicate It turns out the Rx code states pinMode 6, but should be pinMode 2 as the Fritzing diagrams shows digital pin 2 wired to the LED.

Cheers!

keith_smile

keith_smile

9 months ago

I tried literally off and on for days to get communication. Bought cheap radios. Thought they didn't work, but this works. I did have to put a 25V, 10 microfarad capacitor between the + and - in case of voltage drops and also:
The project will not work unless you first start arduino 2 and then arduino 1. Don't know why. Also, if it sits too long it halts.
It did hang on me a few times with an LED staying lit but a reset ... reset it ~8^)
Thank you so much for this post!

Keith3D

39908596

39908596

5 months ago

I Tried this with arduino UNO but not working. Can some one help me to do this you can charge as per your work. My application with NRF 24L01 Transceiver module 3 input and 3 output on both Module (Arduino NANO) 3 momentary switch 3 Momentary Output on both card

cadman2021

cadman2021

5 months ago

This was the most concise instruction I have found to date.
Thank you so much, this helped move my project along.

  • 1 thank
39908596

39908596

5 months ago

Can you help me on this project. Actually iam new to arduino

rajkumar1637

rajkumar1637

5 months ago

Code compile is show error

shreesardesai

shreesardesai

4 months ago

Help. I tried doing the Hello World example & there is some issue I added the capacitors at input for nRf module checked every connection but somehow it takes around an hour of restarting & checking the connections for the modules to connect to each other & once they are connected they it's not a problem I can disconnect them for some time & again they work but everytime I keep them disconnected for over night I have to spend an hour sometimes 2 for them to work again this has haulted all my efforts as I have classes & work during the day & every night I wana work on this my work is left incomplete as most of time is gone only to start the connection between the 2 modules please help

ayoub_sahnoun3453

ayoub_sahnoun3453

4 months ago

why the :
#include <nRF24L01.h>
#include <RF24.h>
dosn't work

ayoub_sahnoun3453

ayoub_sahnoun3453

4 months ago

sorry all is right

sova_x

sova_x

4 months ago

Can Li eventually see the cod with all the changes working

ratack0

ratack0

3 months ago

Thank you so much! This project helped me get my nRF24L01 modules working!

Henry Watson

Henry Watson

3 months ago

It did not work for me with the original code. Because it did not match the sketch pins first time but In order to fix it you simply need to either change where the led pin is declared in the code or swap the led to pin 6 of the arduino

NeilIves

NeilIves

2 months ago

I've just got the basic one-way comms working between an Uno and a Nano. I had to set up Serial Monitor in each sketch to check that things were actually happening. The use of the LED stick in the sketch was confusing, (for me) I just set up a single LED on the receiver end.
Anyway, many thanks for helping me on my way to my project.

carcoder

carcoder

2 months ago

Thank you so much for this project it really helped me move along!

Hi, thank,s for your usefull post.
i try example1 but just text transfer between to board and boolean data no transfer. Even i try by integer data, but still not transfered, even i delete text and just transfer a boolean data but still that problem remain. what can i do with this problem?

Similar projects you might like

Add projectSign up / Login