Blog
NRF24L01 transceiver module
what is the NRF24?
NRF24 is a extremely tiny 2.4GHZ transceiver chip,which can realize radio signals transmission and reception.It can run in the global 2.4G frequency band,and the NRF24 power consumption is super-low,especially suitable for battery-powered devices. Usually used in electric vehicle electronic keys,wireless mice and so on.
How does NRF24 work?
NRF24L01 integrates multiple data registers to control the working status,including the data buffer FIFO,when setting up each register, through SPI communication bus,the data will be send out automatically by writing data to TX FIFO.when receive the data,the IRQ pin will send interrupt signal,at the same time,the internal RX FIFO overflow flag is set to 1,after reading will automatically reset.
What is the advantage of NRF24L01?
NRF24 has the advantage of tiny,low-power consumption,multiple-frequency and high transmission rate.because all of control system are built-in inside chip.which make it easily development.
What are the operating modes of NRF24?
4 types Working mode
- send and receive mode
- Idle mode
- Configuration mode
- Shutdown mode
What is the composition of the NRF24?
Mainly composed:frequency generator,enhanced SchockBurst mode controller,power amplifier,crystal oscillator,modulator,demodulator.
What is the role of the NRF24?
The NRF24L01 is mainly responsible for wireless data transmission. With proper code control, it can send data while receiving. It also has an impressive transmission range,even without an external wires, it can reach up to 500 meters. That’s why it’s perfect as a wireless signal transceiver for remote control devices.
NRF24 connection to Arduino transmission example
NRF24 connection to Arduino sample code
I originally wanted to study the underlying working logic of NRF24L01 on Arduino using the programmer’s exclusive coding method “Ctrl+C” and “Ctrl+V”. However, after looking for many bloggers, they only provided code for one-to-one communication, with very simple explanations, and the code could only either transmit or receive. This is very unfavorable for my subsequent research. So, I spent several days searching for information online, and even “dug through” the Arduino official website. The following is my understanding of the NRF24L01 code in Arduino, hoping it can be helpful to you.
First, there are the header files that need to be included, which are
#include
#include
#include
But among these, we only need to download the RF24 library file.
Next, we need to create a structure for NRF24L01.
RF24 radio(CE_PIN, CSN_PIN);
The CE and CSN pins are defined by ourselves. However, to make the CSN correspond to the SPI’s CSN pin of Arduino itself and to make the wiring more neat, we choose CE – 9 and CSN – 10, which means defining:
#define CE_PIN 9
#define CSN_PIN 10
Then there’s the communication address. Most bloggers say that the transmitting end and the receiving end must have the same address, but they don’t give a reason. After many tests, I found that the NRF24L01 can store both the transmitting address and the receiving address, and the relationship between them is very simple. The receiving end’s address can be regarded as its own location, and the transmitting address is like the recipient’s address on a package. Only when the package’s address matches its own location can it receive the package. That’s why the transmitting address and the receiving address must be consistent.
With this, we can achieve both sending and receiving functions. First, we need to define the addresses for the transmitting and receiving ends. Attention the address of NRF24L01 is a 5-byte array.
const uint8_t TxAddress[5] ={0x00,0x00,0x00,0x00,0x00};
//Sender address
const uint8_t RxAddress[5] ={0x01,0x00,0x00,0x00,0x00};
//Receiver address
In addition, according to official data, a receiving end can receive data from multiple transmitting ends. It only needs to modify the receiving end’s code based on the address of each transmitting end. However, since such usage scenarios are not common, this article will not elaborate further. Those who are interested can try it on their own.
Next comes the initialization part.
while (!radio.begin()) {
Serial.println("NRF24L01 initialization failed!");
delay(100);
}
This is the code to check if the SPI communication lines are properly connected. If the connection is not established correctly, the program will get stuck here.
radio.setChannel(0);
This is a function for setting the channel, with optional values rang from 0 to 125. Because the 2.4G frequency band is not a single frequency, but a range from 2.4MHz to 2.526MHz, which is divided into multiple channels according to the communication speed. You don’t have to use this function,the default value for Arduino is 76.
radio.setDataRate(RF24_1MBPS);
This is a function for setting the data rate, which can be set to RF24_250Kbps, RF24_1Mbps, or RF24_2Mbps. The larger the value, the faster the transmission speed, but the shorter the distance. You don’t have to use this function, the default is RF24_1Mbps.
In addition, the communication frequency band can be obtained by multiplying with the channel. For example, with channel 76 and a transmission rate of 1Mbps, the minimum frequency = 2400MHz + 76MHz = 2476MHz. The maximum frequency = minimum frequency + 1MHz = 2476MHz + 1MHz = 2477MHz. That is to say, the wireless frequency used by nRF24l01 for communication is between 2476MHz and 2477MHz.
Attention that the frequency must not exceed 2526MHz, otherwise normal communication will not be possible. At the same time, it is necessary to ensure that the channel and wireless data rate of NRF24l01 are consistent.
radio.openReadingPipe(0,RxAddress);
//The receiving end setting can be omitted as the sending end.
//Set data channel 0-5, set the receiving end (own) address
Set the receiver address, which is our own communication position. The NRF24L01 can communicate with up to 6 devices, so there are also 6 corresponding data channels (0-5). If only channel 0 is used, it can transmit 32 char-type data, like
char receivedData[32];
radio.openWritingPipe(TxAddress);
//The sender setting can be omitted as the receiver. Set the recipient's address
The function for setting the transmitter is like delivery address for a package information. Only the NRF24L01 with the corresponding address will receive the data.
setPayloadSize();
This is a function to set the data transmission length. Not necessary to use it, after all, shorter data has little impact on transmission.
setCRCLength():modify CRC length(RF24_CRC_DISABLED、RF24_CRC_8、RF24_CRC_16)
This is a function to set the length of check code.Better not to use it. Except for large amount of data needs to be transmitted without errors,basically unnecessary. The default value is sufficient.
radio.setPALevel(RF24_PA_MIN);
//Adjust the transmission power(RF24_PA_MIN、RF24_PA_LOW、RF24_PA_HIGH、RF24_PA_MAX)
Set the transmission power,the higher the power, the more power consumes, but the longer the transmission distance. It’s optional to set, default is RF24_PA_LOW.
The above are the initialization configurations. Next is to set up receiving and sending, which is also very simple.
radio.stopListening();
Set to transmit mode.
radio.startListening();
set to receive mode
For example, the code to send a piece of data from the receiving mode and then continue receiving is as follows:
radio.stopListening();
//Set as sender
radio.write("hello",5);
//send data and data length
radio.startListening();
//Set as receiving end
In receive mode, data is stored in the FIFO data buffer and needs to be read by yourself. However, the official library provides a function radio.available() to check if new data. It returns true if there is new data in the FIFO buffer, and false otherwise. This way, we don’t have to keep reading duplicate data from the buffer. The specific code is as follows:
if (radio.available()) {
//If there is data available
radio.read(&receivedData, sizeof(receivedData));
//read data
Serial.println("new data!");
Serial.print("data1:");
Serial.println(receivedData[0]);
Serial.print("data2:");
Serial.println(receivedData[1]);
Serial.print("data3:");
Serial.println(receivedData[2]);
//...
}
Complete code
#include
#include
#include
#define CE_PIN 9
#define CSN_PIN 10
RF24 radio(CE_PIN, CSN_PIN);
//New Device
const uint8_t TxAddress[5] ={0x00,0x00,0x00,0x00,0x00};
//Sender address
const uint8_t RxAddress[5] ={0x01,0x00,0x00,0x00,0x00};
//Receiver address
char receivedData[32];
//Only one sender and one receiver can transmit up to 32 char arrays
void setup() {
Serial.begin(9600);
while (!radio.begin()) {
Serial.println("NRF24L01 initialization failed!");
delay(100);
}
//radio.setChannel(0);
//Set Communication channel 0-126
//radio.setDataRate(RF24_1MBPS);
//Set the air transmission rate to 250Kbps,1Mbps,2Mbps.
radio.openReadingPipe(0,RxAddress);
//The receiving end setting can be omitted as the sending end.
//Set data channel 0-5, set the receiving end (own) address
radio.openWritingPipe(TxAddress);
//The sender setting can be omitted as the receiver. Set the recipient's address
//radio.setPALevel(RF24_PA_MIN);
//Adjust the transmission power(RF24_PA_MIN、RF24_PA_LOW、RF24_PA_HIGH、RF24_PA_MAX)
//radio.setCRCLength(RF24_CRC_8);
//Modify CRC length(RF24_CRC_DISABLED、RF24_CRC_8、RF24_CRC_16)
radio.stopListening();
//Set as sender
radio.write("hello",5);
//send data and data length
radio.startListening();
//Set as receiving end
Serial.println("NRF24L01 Started, waiting for data..");
Serial.println("-----------------------------------");
}
void loop() {
if (radio.available()) {
//If there is data available
radio.read(&receivedData, sizeof(receivedData));
//read data
Serial.println("new data!");
Serial.print("data1:");
Serial.println(receivedData[0]);
Serial.print("data2:");
Serial.println(receivedData[1]);
Serial.print("data3:");
Serial.println(receivedData[2]);
//...
//Reply to the sender, you don't need to
//delay(10);
//radio.stopListening();
//radio.write("OK",2);
//radio.startListening();
}
delay(10);
}
NRF24 Functional Parameter Table
| NRF24 | |
| Input voltage | 1.9V – 3.6V |
| Working temp | -45°C – 85°C |
| Max transmit power | 4dbm |
| Max data rate | 2000kbps |
| Max transmission distance | 120m |
| dimension | 15.01mm*29.01mm |
NRF24L01 DATASHEET
NRF24 PINS
GND:Power ground terminal
VCC:Power input voltage.must be between 1.9-3.6v,exceeding this range will cause damage to the module
CE:control line,which can control the state of the NRF24 module
CSN:SPI chip select pin
SCK:clock line(SPI clock)
MOSI:Master Out Slave In pin, through which the microcontroller sends data to the NRF24L01 module
MISO:Master In Slave Out pin, through which the NRF24L01 module sends data to the micro-controller.
IRQ:Interrupt signal output pin
FAQS
How to increase NRF24L01 range?
If you want to extend the data transmission range of the NRF24L01, we can choose an RF24 module with an external wires. With a suitable gain wires and proper orientation, theoretically can reach up to 1500 meters.
How to pair NRF24L01?
How many channels does NRF24L01 have?
What devices use NRF24?
Any MCU with more than 5 pins can work with the NRF24L01. If it has integrated SPI hardware, it can also reduce the consumption of system resources.
Can NRF24L01 transmit audio?
You can use the NRF24L01 to send audio stored in electronic devices, but you have to split the audio file into very short data segments for transmission. The receiver then has to piece them back together into a complete audio file, which is quite challenging overall.
Can NRF24L01 connect to mobile?
NThe NRF24L01 can only connect to other controllers via SPI, and for wireless transmission, it can only link to other NRF24L01 devices.