How to Use NRF24L01 Wireless Module with Arduino Easily
To use the
nRF24L01 wireless module with Arduino, connect its pins to the Arduino SPI pins and use the RF24 library to send and receive data wirelessly. Initialize the module in your code, set addresses, and use write() and read() functions to communicate between two Arduinos.Syntax
The basic syntax involves including the RF24 library, creating an RF24 object with the CE and CSN pins, and using methods to initialize and communicate.
RF24 radio(CE_pin, CSN_pin);- Creates the radio object.radio.begin();- Starts the radio module.radio.openWritingPipe(address);- Sets the address to send data.radio.openReadingPipe(number, address);- Sets the address to receive data.radio.write(&data, sizeof(data));- Sends data.radio.available()- Checks if data is received.radio.read(&data, sizeof(data));- Reads received data.
arduino
#include <SPI.h> #include <RF24.h> // Create radio object on pins 9 (CE) and 10 (CSN) RF24 radio(9, 10); const byte address[6] = "00001"; void setup() { radio.begin(); radio.openWritingPipe(address); radio.setPALevel(RF24_PA_LOW); radio.stopListening(); // Set as transmitter } void loop() { const char text[] = "Hello"; radio.write(&text, sizeof(text)); delay(1000); }
Example
This example shows two Arduino sketches: one to send a message and one to receive it using the nRF24L01 module.
arduino
// Transmitter Code #include <SPI.h> #include <RF24.h> RF24 radio(9, 10); const byte address[6] = "00001"; void setup() { Serial.begin(9600); radio.begin(); radio.openWritingPipe(address); radio.setPALevel(RF24_PA_LOW); radio.stopListening(); } void loop() { const char text[] = "Hello World"; bool success = radio.write(&text, sizeof(text)); if (success) { Serial.println("Sent: Hello World"); } else { Serial.println("Send failed"); } delay(1000); } // Receiver Code #include <SPI.h> #include <RF24.h> RF24 radio(9, 10); const byte address[6] = "00001"; void setup() { Serial.begin(9600); radio.begin(); radio.openReadingPipe(0, address); radio.setPALevel(RF24_PA_LOW); radio.startListening(); } void loop() { if (radio.available()) { char text[32] = {0}; radio.read(&text, sizeof(text)); Serial.print("Received: "); Serial.println(text); } }
Output
Sent: Hello World
Received: Hello World
Common Pitfalls
Common mistakes include:
- Incorrect wiring of CE, CSN, MOSI, MISO, and SCK pins.
- Not matching addresses between transmitter and receiver.
- Forgetting to call
radio.startListening()on the receiver. - Using different power levels or data rates causing communication failure.
- Not adding capacitors near the module to stabilize power supply.
arduino
// Wrong: Not calling startListening on receiver radio.openReadingPipe(0, address); // Missing this line: // radio.startListening(); // Right: radio.openReadingPipe(0, address); radio.startListening();
Quick Reference
| Function | Purpose |
|---|---|
| radio.begin() | Initialize the nRF24L01 module |
| radio.openWritingPipe(address) | Set address for sending data |
| radio.openReadingPipe(number, address) | Set address for receiving data |
| radio.startListening() | Switch to receive mode |
| radio.stopListening() | Switch to transmit mode |
| radio.write(&data, size) | Send data |
| radio.available() | Check if data received |
| radio.read(&data, size) | Read received data |
Key Takeaways
Connect nRF24L01 pins correctly to Arduino SPI pins and power supply.
Use the RF24 library to handle wireless communication easily.
Match addresses exactly on transmitter and receiver for communication.
Call startListening() on receiver and stopListening() on transmitter.
Add a 10uF capacitor near the module to stabilize power and avoid resets.