Optimized high speed nRF24L01+ driver class documentation  1.4.2
TMRh20 2020 - Optimized fork of the nRF24L01+ driver
Arduino

RF24 is fully compatible with Arduino boards.

See Arduino Board reference and Arduino SPI reference for more information

RF24 makes use of the standard hardware SPI pins (MISO, MOSI, SCK) and requires two additional pins, to control the chip-select and chip-enable functions.

RF24 radio(ce_pin, cs_pin);
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
Definition: RF24.h:114

These pins must be chosen and designated by the user and can use any available pins.

Alternate SPI Support

RF24 supports alternate SPI methods, in case the standard hardware SPI pins are otherwise unavailable.

Software Driven SPI

Software driven SPI is provided by the DigitalIO library.

Setup:

  1. Install the digitalIO library
  2. Open RF24_config.h in a text editor. Uncomment the line
    #define SOFTSPI
    or add the build flag/option
    -DSOFTSPI
  3. In your sketch, add
    #include <DigitalIO.h>
Note
Note: Pins are listed as follows and can be modified by editing the RF24_config.h file.
#define SOFT_SPI_MISO_PIN 16
#define SOFT_SPI_MOSI_PIN 15
#define SOFT_SPI_SCK_PIN 14

Or add the build flag/option

-DSOFT_SPI_MISO_PIN=16 -DSOFT_SPI_MOSI_PIN=15 -DSOFT_SPI_SCK_PIN=14

Alternate Hardware (UART) Driven SPI

The Serial Port (UART) on Arduino can also function in SPI mode, and can double-buffer data, while the default SPI hardware cannot.

The SPI_UART library is available at TMRh20/Sketches

Enabling:

  1. Install the SPI_UART library
  2. Edit RF24_config.h and uncomment
    #define SPI_UART
  3. In your sketch, add
    #include <SPI_UART.h>

SPI_UART SPI Pin Connections:

NRF Arduino Uno Pin
MOSI TX(0)
MISO RX(1)
SCK XCK(4)
CE User Specified
CSN User Specified
Note
SPI_UART on Mega boards requires soldering to an unused pin on the chip. See #24 for more information on SPI_UART.

Using a specific SPI Bus

An alternate SPI bus can be specified using the overloaded RF24::begin(_SPI*) method. This is useful for some boards that offer more than 1 hardware-driven SPI bus or cetain Arduino cores that implement a software-driven (AKA bit-banged) SPI bus that does not use the DigitalIO library.

Warning
The SPI bus object's SPIClass::begin() method must be called before calling the overloaded RF24::begin(_SPI*) method.

Below are some example snippets that demonstrate how this can be done.

ESP8266 example

See also
The following example code is meant for the popular NodeMCU board. Please refer to the ESP8266 ArduinoCore's SPI documentation for other ESP8266-based boards.
#include <SPI.h>
#include <RF24.h>
// notice these pin numbers are not the same used in the library examples
RF24 radio(D4, D3); // the (ce_pin, csn_pin) connected to the radio
void setup() {
Serial.begin(115200);
while (!Serial) {} //some boards need this
// by default (with no arguments passed) SPI uses D5 (HSCLK), D6 (HMISO), D7 (HMOSI)
SPI.pins(6, 7, 8, 0);
// this means the following pins are used for the SPI bus:
// MOSI = SD1
// MISO = SD0
// SCLK = CLK
// CSN = GPIO0 (labeled D3 on the board)
// **notice we also passed `D3` to the RF24 contructor's csn_pin parameter**
SPI.begin();
if (!radio.begin(&SPI)) {
Serial.println(F("radio hardware not responding!!"));
while (1) {} // hold program in infinite loop to prevent subsequent errors
}
// ... continue with program as normal (see library examples/ folder)
}

ESP32 example

See also
Please review the Espressif's SPI_Multiple_Buses.ino example for the ESP32 located in their ArduinoCore repository (along with the SPI library for the ESP32).
#include <SPI.h>
#include <RF24.h>
// to use custom-defined pins, uncomment the following
// #define MY_MISO 26
// #define MY_MOSI 27
// #define MY_SCLK 25
// #define MY_SS 32 // pass MY_SS as the csn_pin parameter to the RF24 constructor
// notice these pin numbers are not the same used in the library examples
RF24 radio(2, 0); // the (ce_pin, csn_pin) connected to the radio
SPIClass* hspi = nullptr; // we'll instantiate this in the `setup()` function
// by default the HSPI bus predefines the following pins
// HSPI_MISO = 12
// HSPI_MOSI = 13
// HSPI_SCLK = 14
// HSPI_SS = 15
void setup() {
Serial.begin(115200);
while (!Serial) {} //some boards need this
hspi = new SPIClass(HSPI); // by default VSPI is used
hspi->begin();
// to use the custom defined pins, uncomment the following
// hspi->begin(MY_SCLK, MY_MISO, MY_MOSI, MY_SS)
if (!radio.begin(hspi)) {
Serial.println(F("radio hardware not responding!!"));
while (1) {} // hold program in infinite loop to prevent subsequent errors
}
// ... continue with program as normal (see library examples/ folder)
}

Teensy example

See also
The overloaded RF24::begin(_SPI*) is not needed according to the Teensyduino SPI documentation. Please review the table provided in the Teensyduino documentation for what pins are used by default for certain Teensy boards.
#include <SPI.h>
#include <RF24.h>
// these pins are the alternate SPI pins available for Teensy LC/3.0/3.1/3.2/3.5/3.6
#define MY_MISO 8
#define MY_MOSI 7
#define MY_SCLK 14
// notice these pin numbers are not the same used in the library examples
RF24 radio(2, 0); // the (ce_pin, csn_pin) connected to the radio
void setup() {
Serial.begin(115200);
while (!Serial) {} //some boards need this
SPI.setMOSI(MY_MOSI);
SPI.setMISO(MY_MISO);
SPI.setSCK(MY_SCLK);
if (!radio.begin()) {
Serial.println(F("radio hardware not responding!!"));
while (1) {} // hold program in infinite loop to prevent subsequent errors
}
// ... continue with program as normal (see library examples/ folder)
}