nRF24L01 – How It Works, Arduino Interface, Code, Schematic

In this Arduino tutorial we will learn how to make a wireless communication between two Arduino boards using the NRF24L01 transceiver module. You can watch the following video or read the written tutorial below.

Overview

For explaining the wireless communication we will make two examples, the first one will be sending a simple “Hello World” message from one Arduino to another, and in the second example we will have a bi-directional communication between the Arduino boards, where using the Joystick at the first Arduino we will control the servo motor at the second Arduino, and vice versa, using the push button at the second Arduino we will control the LED at the first Arduino.

https://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-and-Arduino-Wireless-Communication-Complete-Guide-Turorial-300x169.jpg 300w, https://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-and-Arduino-Wireless-Communication-Complete-Guide-Turorial-1024x576.jpg 1024w, https://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-and-Arduino-Wireless-Communication-Complete-Guide-Turorial-768x432.jpg 768w" sizes="(max-width: 681px) 100vw, 681px" class="clear" data-reader-unique-id="23" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

NRF24L01 Transceiver Module – How It Works

Let’s take a closer look at the NRF24L01 transceiver module. It uses the 2.4 GHz band and it can operate with baud rates from 250 kbps up to 2 Mbps. If used in open space and with lower baud rate its range can reach up to 100 meters.

NRF24L01 Transceiver Module https://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-Transceiver-Module--300x183.jpg 300w, https://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-Transceiver-Module--768x467.jpg 768w" sizes="(max-width: 550px) 100vw, 550px" class="clear" data-reader-unique-id="27" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

The module can use 125 different channels which gives a possibility to have a network of 125 independently working modems in one place. Each channel can have up to 6 addresses, or each unit can communicate with up to 6 other units at the same time.

Working Principles of Channels and Addresseshttps://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-Working-Principles-of-Channels-and-Addresses-300x161.png 300w" sizes="(max-width: 600px) 100vw, 600px" class="clear" data-reader-unique-id="31" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

The power consumption of this module is just around 12mA during transmission, which is even lower than a single LED. The operating voltage of the module is from 1.9 to 3.6V, but the good thing is that the other pins tolerate 5V logic, so we can easily connect it to an Arduino without using any logic level converters.

NRF24L01 Transceiver Module Pinouts Connectionshttps://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-Transceiver-Module-Pinouts-Connections-300x225.jpg 300w, https://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-Transceiver-Module-Pinouts-Connections-80x60.jpg 80w" sizes="(max-width: 351px) 100vw, 351px" class="clear" data-reader-unique-id="34" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

Three of these pins are for the SPI communication and they need to be connected to the SPI pins of the Arduino, but note that each Arduino board has different SPI pins. The pins CSN and CE can be connected to any digital pin of the Arduino board and they are used for setting the module in standby or active mode, as well as for switching between transmit or command mode. The last pin is an interrupt pin which doesn’t have to be used.

There are several variations of the NRF24L01 modules. The most popular is the one with on-board antenna. This makes the module to be more compact, but on the other hand, lowers the transmission range to a distance of about 100 meters.

Various modules based on the NRF24L01 chiphttps://howtomechatronics.com/wp-content/uploads/2017/02/Various-modules-based-on-the-NRF24L01-chip-300x174.jpg 300w, https://howtomechatronics.com/wp-content/uploads/2017/02/Various-modules-based-on-the-NRF24L01-chip-1024x595.jpg 1024w, https://howtomechatronics.com/wp-content/uploads/2017/02/Various-modules-based-on-the-NRF24L01-chip-768x446.jpg 768w" sizes="(max-width: 680px) 100vw, 680px" class="clear" data-reader-unique-id="42" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

The second variation, instead of on-board antenna, it has a SMA connector and which we can attach a duck antenna for better transmission range.

The third variation shown here, in addition to the duck antenna, it has a RFX2401C chip which includes PA (Power Amplifier) and LNA (Low-Noise Amplifier).  This amplifies the NRF24L01 signal and enables even better transmission range of up to 1000 meters in open space.

NRF24L01 Module Pinout

Here’s a detailed look at the NRF24L01 pinout, as well as the NRF24L01+ PA/LNA module.

NRF24L01 Pinout & NRF24L01+ PA LNA .pnghttps://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-Pinout-NRF24L01-PA-LNA--300x200.png 300w, https://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-Pinout-NRF24L01-PA-LNA--1024x683.png 1024w, https://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-Pinout-NRF24L01-PA-LNA--768x512.png 768w, https://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-Pinout-NRF24L01-PA-LNA--360x240.png 360w, https://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-Pinout-NRF24L01-PA-LNA--600x400.png 600w" sizes="(max-width: 650px) 100vw, 650px" class="clear" data-reader-unique-id="51" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

Both modules, the NRF24L01 and the NRF24L01+ PA/LNA have the same pinout, so we can connect them in our circuit the same way.

Circuit Diagram

Here’s how we need to connect the NRF24L01 modules to the Arduino boards.

NRF24L01 and Arduino Tutorial Circuit Schematichttps://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-and-Arduino-Tutorial-Circuit-Schematic-300x168.png 300w, https://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-and-Arduino-Tutorial-Circuit-Schematic-768x430.png 768w" sizes="(max-width: 960px) 100vw, 960px" class="clear" data-reader-unique-id="59" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

As I already mentioned, each Arduino board has different SPI pins, so keep that in mind when connecting the modules to your Arduino board.

Arduino SPI Pinshttps://howtomechatronics.com/wp-content/uploads/2017/02/Arduino-SPI-Pins-300x182.png 300w" sizes="(max-width: 320px) 100vw, 320px" class="clear" data-reader-unique-id="62" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

You can get the components needed for this Arduino Tutorial from the links below:

Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.

Arduino and NRF24L01 Code

Once we connect the NRF24L01 modules to the Arduino boards we are ready to make the codes for both the transmitter and the receiver.

First we need to download and install the RF24 library which makes the programming less difficult.

Here are the two codes for the wireless communication and below is the description of them.

Transmitter Code

* Arduino Wireless Communication Tutorial

* Example 1 - Transmitter Code

* by Dejan Nedelkovski, www.HowToMechatronics.com

* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/

RF24 radio(7, 8); // CE, CSN

const byte address[6] = "00001";

radio.openWritingPipe(address);

radio.setPALevel(RF24_PA_MIN);

constchar text[] = "Hello World";

radio.write(&text, sizeof(text));

Receiver Code

* Arduino Wireless Communication Tutorial

* Example 1 - Receiver Code

* by Dejan Nedelkovski, www.HowToMechatronics.com

* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/

RF24 radio(78); // CE, CSN

const byte address[6] = "00001";

radio.openReadingPipe(0, address);

radio.setPALevel(RF24_PA_MIN);

radio.startListening();

if(radio.available()){

radio.read(&text, sizeof(text));

Serial.println(text);

Description:

So we need to include the basic SPI and the newly installed RF24 libraries and create an RF24 object. The two arguments here are the CSN and CE pins.

RF24 radio(78); // CE, CSN

Next we need to create a byte array which will represent the address, or the so called pipe through which the two modules will communicate.

const byte address[6] = "00001";

We can change the value of this address to any 5 letter string and this enables to choose to which receiver we will talk, so in our case we will have the same address at both the receiver and the transmitter.

In the setup section we need to initialize the radio object and using the radio.openWritingPipe() function we set the address of the receiver to which we will send data, the 5 letter string we previously set.

radio.openWritingPipe(address);

On the other side, at the receiver, using the radio.setReadingPipe() function we set the same address and in that way we enable the communication between the two modules.

radio.openReadingPipe(0, address);

Then using the radio.setPALevel() function we set the Power Amplifier level, in our case I will set it to minimum as my modules are very close to each other.

radio.setPALevel(RF24_PA_MIN);

Note that if using a higher level it is recommended to use a bypass capacitors across GND and 3.3V of the modules so that they have more stable voltage while operating.

Next we have the radio.stopListening() function which sets module as transmitter, and on the other side, we have the radio.startListening() function which sets the module as receiver.

In the loop section, at the transmitter, we create an array of characters to which we assign the message “Hello World”. Using the radio.write() function we will send that message to the receiver. The first argument here is the variable that we want to be sent.

const char text[] = "Hello World";

radio.write(&text, sizeof(text));

By using the “&” before the variable name we actually set an indicating of the variable that stores the data that we want to be sent and using the second argument we set the number of bytes that we want to take from that variable. In this case the sizeof() function gets all bytes of the strings “text”. At the end of the program we will add 1 second delay.

On the other side, at the receiver, in the loop section using the radio.available() function we check whether there is data to be received. If that’s true, first we create an array of 32 elements, called “text”, in which we will save the incoming data.

if(radio.available()){

radio.read(&text, sizeof(text));

Serial.println(text);

Using the radion.read() function we read and store the data into the “text” variable. At the end we just print text on the serial monitor. So once we upload both programs, we can run the serial monitor at the receiver and we will notice the message “Hello World” gets printed each second.

Troubleshooting

It’s worth noting that power supply noise is one of the most common issues people experience when trying to make successful communication with the NRF24L01 modules. Generally, RF circuits or radio frequency signals are sensitive to power supply noise. Therefore, it’s always a good idea to include a decoupling capacitor across the power supply line. The capacitor can be anything from 10uF to 100uF.

NRF24L01 Troubleshooting- decoupling capacitor and external power supplyhttps://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-Troubleshooting-decoupling-capacitor-and-external-power-supply-300x147.png 300w, https://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-Troubleshooting-decoupling-capacitor-and-external-power-supply-1024x503.png 1024w, https://howtomechatronics.com/wp-content/uploads/2017/02/NRF24L01-Troubleshooting-decoupling-capacitor-and-external-power-supply-768x377.png 768w" sizes="(max-width: 700px) 100vw, 700px" class="clear" data-reader-unique-id="642" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

Another common issue is that the 3.3V pin of the Arduino boards, cannot always supply enough power to the NRF24L01 module. So, powering the module with an external power source is also a good idea.

Arduino Wireless Bi-directional Communication with two NRF24L01 modules


Let’s see the second example, a bi-directional wireless communication between two Arduino boards. Here’s the circuit schematics:

Arduino Wireless Communication NRF24L01 Circuit Schematic Tutorialhttps://howtomechatronics.com/wp-content/uploads/2017/02/Arduino-Wireless-Communication-NRF24L01-Circuit-Schematic-Tutorial-300x179.png 300w, https://howtomechatronics.com/wp-content/uploads/2017/02/Arduino-Wireless-Communication-NRF24L01-Circuit-Schematic-Tutorial-768x458.png 768w" sizes="(max-width: 960px) 100vw, 960px" class="clear" data-reader-unique-id="648" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

You can get the components needed for this example from the links below:

Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.

https://howtomechatronics.com/wp-content/uploads/2018/07/How-To-Build-an-Arduino-Wireless-Network-with-Multiple-NRF24L01-Modules-Tutorial-768x432.jpg 768w, https://howtomechatronics.com/wp-content/uploads/2018/07/How-To-Build-an-Arduino-Wireless-Network-with-Multiple-NRF24L01-Modules-Tutorial-1024x576.jpg 1024w, https://howtomechatronics.com/wp-content/uploads/2018/07/How-To-Build-an-Arduino-Wireless-Network-with-Multiple-NRF24L01-Modules-Tutorial.jpg 1280w" sizes="(max-width: 213px) 100vw, 213px" class="clear" data-reader-unique-id="686" style="max-width: 100%; margin: 0px; display: block; height: auto; clear: both;">

Source Code


Here are the two codes and below is the description of them.

Transmitter Code

* Arduino Wireless Communication Tutorial

* Example 2 - Transmitter Code

* by Dejan Nedelkovski, www.HowToMechatronics.com

* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/

RF24 radio(7, 8); // CE, CSN

const byte addresses[][6] = {"00001""00002"};

radio.openWritingPipe(addresses[1]); // 00002

radio.openReadingPipe(1, addresses[0]); // 00001

radio.setPALevel(RF24_PA_MIN);

int potValue = analogRead(A0);

int angleValue = map(potValue, 0, 1023, 0, 180);

radio.write(&angleValue, sizeof(angleValue));

radio.startListening();

while(!radio.available());

radio.read(&buttonState, sizeof(buttonState));

if(buttonState == HIGH){

digitalWrite(led, HIGH);

digitalWrite(led, LOW);

Receiver Code

* Arduino Wireless Communication Tutorial

* Example 2 - Receiver Code

* by Dejan Nedelkovski, www.HowToMechatronics.com

* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/

RF24 radio(7, 8); // CE, CSN

const byte addresses[][6] = {"00001""00002"};

pinMode(button, INPUT);

radio.openWritingPipe(addresses[0]); // 00001

radio.openReadingPipe(1, addresses[1]); // 00002

radio.setPALevel(RF24_PA_MIN);

radio.startListening();

if( radio.available()){

while(radio.available()){

radio.read(&angleV, sizeof(angleV));

myServo.write(angleV);

radio.stopListening();

buttonState = digitalRead(button);

radio.write(&buttonState, sizeof(buttonState));

What’s different here from the previous example is that we need to create two pipes or addresses for the bi-directional communication.

const byte addresses[][6] = {"00001""00002"};

In the setup section we need to define both pipes, and note that the writing address at the first Arduino needs to be the reading address at the second Arduino, and vice versa, the reading address at the first Arduino needs to be the writing address at the second Arduino.

radio.openWritingPipe(addresses[1]); // 00001

radio.openReadingPipe(1, addresses[0]); // 00002

radio.openWritingPipe(addresses[0]); // 00002

radio.openReadingPipe(1, addresses[1]); // 00001

In the loop section using the radio.stopListening() function we set the first Arduino as transmitter, read and map the value of Joystick from 0 to 180, and using the radio.write() function send the data to the receiver.

int potValue = analogRead(A0);

int angleValue = map(potValue, 010230180);

radio.write(&angleValue, sizeof(angleValue));

On the other side, using the radio.startListening() function we set the second Arduino as receiver and we check whether there is available data. While there is data available we will read it, save it to the “angleV” variable and then use that value to rotate the servo motor.

if( radio.available()){

while(radio.available()){

radio.read(&angleV, sizeof(angleV));

myServo.write(angleV);

Next, at the transmitter, we set the first Arduino as receiver and with an empty “while” loop we wait for the second Arduino the send data, and that’s the data for the state of the push button whether is pressed or not. If the button is pressed the LED will light up. So these process constantly repeats and both Arduino boards are constantly sending and receiving data.

More NRF24L01 and Arduino examples

The NRF24L01 module is a must-have for any Arduino enthusiast. I have already used the NRF24L01 module in many of my Arduino projects. Here I will list all of my projects in which I have used these modules.

Each of these projects/ tutorials has a detailed explanation how to use the NRF24L01 module, including circuit diagrams, improved code implementation for better communication and so on.

DIY Arduino RC Transmitter - Arduino Robot Car Wireless Controlhttps://howtomechatronics.com/wp-content/uploads/2019/01/DIY-Arduino-RC-Transmitter-Arduino-Robot-Car-Wireless-Control-300x169.jpg 300w, https://howtomechatronics.com/wp-content/uploads/2019/01/DIY-Arduino-RC-Transmitter-Arduino-Robot-Car-Wireless-Control-768x432.jpg 768w" sizes="(max-width: 500px) 100vw, 500px" data-reader-unique-id="1489" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">

DIY Arduino RC Transmitter Example

That’s all for this tutorial, I hope you enjoyed it and learned something new. As always, feel free to ask any question in the comments section below.