How to Use an nRF24L01 Module with Arduino

In the world of robotics and IoT, wireless communication between devices is essential. One of the most affordable and efficient modules for transmitting data over a 2.4GHz radio frequency is the nRF24L01. In this tutorial, we鈥檒l explore how it works, the difference between its versions, and how to use it with Arduino.

馃攳 What is the nRF24L01?

The nRF24L01 is a wireless transceiver module operating on the 2.4GHz ISM band (free and license-exempt). It can communicate with other nRF24 modules up to a distance of:

  • 100 meters in open space for the standard PCB antenna version

  • 1 km or more with the external antenna PA+LNA version

馃摗 nRF24L01 With vs Without Antenna

Version Range Stability Power Consumption Cost
Without antenna (PCB) up to 100m Medium Low Affordable
With external antenna (PA+LNA) up to 1000m High Higher More expensive

Recommendation:
If your project involves obstacles or distances above 50-100m, it鈥檚 better to use the external antenna version.

馃搼 How it Works

The nRF24L01 uses the SPI protocol to communicate with Arduino. It can operate both as a transmitter and a receiver, allowing data exchange between two or more devices.

馃摱 Modes:

  • Transmitter (TX): sends data to one or more receivers

  • Receiver (RX): listens for incoming data from transmitters

聽馃摑 Required Components

  • 2 x Arduino Uno (or compatible board)

  • 2 x nRF24L01

  • Jumper wires

馃攲 Wiring Diagram

馃摝 Complete Arduino Code – Transmitter

Here鈥檚 the code to send the data:

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

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

const byte address[6] = "00001";

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_HIGH);
  radio.stopListening();
}

void loop() {
  const char text[] = "Hello World";
  radio.write(&text, sizeof(text));
  Serial.println("Message sent");
  delay(1000);
}

				
			

馃摝 Complete Arduino Code – Receiver

Here鈥檚 the code to receive the data:

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

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

const byte address[6] = "00001";

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_HIGH);
  radio.stopListening();
}

void loop() {
  const char text[] = "Hello World";
  radio.write(&text, sizeof(text));
  Serial.println("Message sent");
  delay(1000);
}

				
			

馃攷聽Breaking down the Code

Let鈥檚 review the code section by section:

馃摝 Include Libraries

Includes the necessary libraries for SPI and nRF24 functionality.

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

				
			

馃摝 Declare the radio object

Defines the CE and CSN pins used for communication.

				
					RF24 radio(9, 10);

				
			

馃摝 Set the Address

Defines the CE and CSN pins used for communication.

				
					const byte address[6] = "00001";

				
			

Sets a shared communication channel for both TX and RX modules. You can change this value to create independent connections.

馃摝 Transmitter Configuration

				
					radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_HIGH);
radio.stopListening();
				
			
  • begin() starts the module

  • openWritingPipe() sets the sending channel

  • setPALevel() sets transmission power (LOW, MEDIUM, HIGH, MAX)

  • stopListening() configures the module as a transmitter

馃摝 Receiver Configuration

				
					radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_HIGH);
radio.startListening();

				
			
  • openReadingPipe() opens the receiving channel

  • startListening() sets the module as a receiver

馃摝 Sending and Receiving Data

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

				
			
Receiver:
				
					if (radio.available()) {
  radio.read(&text, sizeof(text));
}

				
			

The transmitter sends data, while the receiver checks if data is available and reads it into a buffer.

es_ES