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.
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:
- Install the digitalIO library
- Open RF24_config.h in a text editor. Uncomment the line or add the build flag/option
- In your sketch, add
- 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:
- Install the SPI_UART library
- Edit RF24_config.h and uncomment
- In your sketch, add
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 certain 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>
void setup() {
Serial.begin(115200);
while (!Serial) {}
SPI.pins(6, 7, 8, 0);
SPI.begin();
if (!radio.begin(&SPI)) {
Serial.println(F("radio hardware not responding!!"));
while (1) {}
}
}
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>
SPIClass* hspi = nullptr;
void setup() {
Serial.begin(115200);
while (!Serial) {}
hspi = new SPIClass(HSPI);
hspi->begin();
if (!radio.begin(hspi)) {
Serial.println(F("radio hardware not responding!!"));
while (1) {}
}
}
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>
#define MY_MISO 8
#define MY_MOSI 7
#define MY_SCLK 14
void setup() {
Serial.begin(115200);
while (!Serial) {}
SPI.setMOSI(MY_MOSI);
SPI.setMISO(MY_MISO);
SPI.setSCK(MY_SCLK);
if (!radio.begin()) {
Serial.println(F("radio hardware not responding!!"));
while (1) {}
}
}