Communication over NRF24L01+

Module NRF24L01+ by Nordic Semiconductor is an ultra low power 2Mbps RF transceiver IC for the 2.4GHz band. It communicates with its special protocol and designed to save battery lifetime when running on battery.

NRF24L01plus-and-adapter

The module can be connected to another device (e.g. Arduino board) over Serial Peripheral Interface (SPI)

The module NRF24L01+ is powered with 3.3V. There is an adapter with voltage regulator on-board – to connect the module over it to 5V power line. Read NRF24L01 product specification (pdf) for more details.

NRF24L01plus-and-adapter  NRF24L01plus-with-adapter

Connect NRF24L01+ to Arduino

NRF24L01plus-pinout

NRF24L01+ Arduino Description
GND GND GND
VCC 3.3V Power
CSN 7 Chip Select Not
CE 8 Control RX/TX
MOSI 11 Master Output
MISO 12 Master Input
SCK 13 Serial Clock

Connect with an adapter is similar but power pins are connected to +5V and GND.

NRF24L01plus-with-adapter-connected-to-Arduino

Communication between two Arduino boards with NRF24L01+

In the Arduino site two libraries mentioned: Mirf and RF24. Example below uses Mirf library. It can be downloaded from gitub. Download and copy its folder to a library folder of Arduino IDE:

Windows: C:\Program Files\Arduino\libraries
Linux: /usr/share/arduino/libraries

After installing libraries – restart the Arduino IDE, select menu item File >> Examples >>Mirf >> ping _server (or another).

Arduino-select-example-Mirf

Here is shortened version of this example – sender and receiver of int-counter. Upload sender sketch to one Arduino with module NRF24L01+ connected to it, upload receiver sketch to another Arduino with another module.

hint What if both Arduino boards are connected to one computer

When both Arduino boards are connected to one computer – change ports to upload sender and receiver sketches to each board

Arduino-IDE-Mirf-sender-and-receiver-examples-on-one-computer

 

 

Sketch for Sender
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
#include <SoftwareSerial.h>

int counter;//sending data

void setup(){
  Mirf.spi = &MirfHardwareSpi;    //NRF-module is controlled by SPI
  Mirf.init();                    //init NRF-module
  Mirf.setRADDR((byte *)"serv1"); //set name of this NRF-module as "serv1" (max 5 bytes) 
  Mirf.payload = sizeof(int);     //data transferred between modules are int-s  
  Mirf.channel = 78;              //channel is 78 (by default the channel is 1)
  Mirf.config();                  //apply settings specified above to NRF-module
  
  //optional
  Serial.begin(9600);             //monitor state over Serial Monitor
  Serial.println("Started...");   //indicate that the program is ready to receive data
}

void loop(){
  if(Mirf.isSending()){           //wait if NRF-module is sending data
  }
  Mirf.setTADDR((byte *)"clie1"); //send data to a NRF-module with name "clie1" (max 5 bytes)
  Mirf.send((byte*)&counter);     //send int values by address of the field 
  
  //optional
  Serial.print("Sent:");          //indicate what values was sent
  Serial.println(counter);
  delay(500);                     //wait some time (optional)
  if(++counter > 3000)            //if the value of counter is large 
    counter = 0;                  //reset the counter
}

 

Sketch for Receiver
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

int data;//received data

void setup(){
  Mirf.spi = &MirfHardwareSpi;    //NRF-module is controlled by SPI
  Mirf.init();                    //init NRF-module
  Mirf.setRADDR((byte *)"clie1"); //set name of this NRF-module as "clie1" (max 5 bytes) 
  Mirf.payload = sizeof(int);     //data transferred between modules are int-s  
  Mirf.channel = 78;              //channel is 78 (by default the channel is 1)
  Mirf.config();                  //apply settings specified above to NRF-module
  
  //optional
  Serial.begin(9600);             //monitor state over Serial Monitor
  Serial.println("Listening..."); //indicate that the program is ready to receive data
}

void loop(){
  if(Mirf.dataReady()){           //check if data received by NRF-module
    
    while(!Mirf.rxFifoEmpty())  //repeat data reading while the queue (buffer) is not emptied
    {                           //in cycle
      Mirf.getData((byte *) &data);//get data by the address of "data" field

      //optional
      if(data != 0){               //if data is not 0 - print it
          Serial.print("Received:");
          Serial.println(data);
      }
    }
  }
}

 

When Arduino boards are connected to two computers – Serial Monitors of the Arduino IDE can be used. When both boards are connected to one computer – external program can be connected to serial ports – e.g. “CuteCom” (it can be installed from “Ubuntu Software Center“).

Arduino-communicating-with-NRF24L01plus-serial-outputs

 hint What is a channel in NRF24L01+ module

NRF24L01+ module operates on frequencies within 2.400GHz to 2.525GHz, with resolution 1MHz per one channel:

Mirf.channel = 0;    //Default channel: 2400 MHz
Mirf.channel = 78;   //78th channel: 2400 MHz + 78 MHz = 2478 MHz
Mirf.channel = 120;  //120th channel: 2400 MHz + 120 MHz = 2520 MHz

 Each channel actually occupies bandwidth 1MHz (at data transfer speed 1Mbps) and 2MHz (at 2Mbps).

Check which frequencies and powers of receivers are allowed in your country to setup correct channel.

Example: usually WiFi channels may occupy following frequencies and bandwidths.

Mirf.channel = 78;    //78th channel: 2400 MHz + 78 MHz = 2478 MHz – between 13th and 14th WiFi channels. There are smart-phone applications showing occupied WiFi channels around – to select less busy frequency.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmail