How to setup and use the nRF24L01 in your projects

Adding wireless to your projects can add flexibility in situations where using wires is not practical. It allows you to position your sensors where wires physically prevent it. Today we at look at the popular nRF24L01 module that has many useful features and can easily be integrated into your electronics projects.

Later on down the page, I show you how easy it is to set up a one way wireless link using two of these modules with two microcontroller boards to control them. Then I’ll use the rain gauge sensor from the previous digital rain gauge monitor project and make it wireless. So keep reading to find out how or watch the tutorial in the video below !

 

 

How does the nRF24L01 Work – Features and Specifications

The nRF24L01 chip uses two way communications over the 2.4GHz ISM band. It operates on 3.3V and uses SPI for communications to a microcontroller. The SPI pins are 5V tolerant so connecting directly to a microcontroller’s pins that runs off 5V will work just fine without any voltage conversion needed.

It has 126 channels that are selectable in 1MHz increments across the 2.4GHz band. The output power level is also adjustable which is great for power saving for short distance transmission. The bandwidth is also adjustable from 250kbps to 2 Mbps which is great if you want to transmit large amounts of data at a time.

The modules cost anywhere between $1 to $3 US a piece from various online retailers.

 

Setting up the nRF24L01 – One way link

In this example, we use the minimum code possible to demonstrate how easy it is to create a one way communication link. One way communications is fine for transmitting periodic data such as a sensors output. Here we will transmit a string of characters every 1 second to a receiver that will display the output to the serial monitor. This is a great little test to show how easy it is to get these modules up and running using the RF24 library.

 

Components Required to Setup the nRF24L01 Commications Link

  • 2x Arduino Uno (or compatible boards)
  • 2x nrf24L01
  • Hookup wire

 

First wire the boards in accordance to the pins outs in the diagram below

 

nrf24L01 arduino wiring diagram

 

Coding the Arduino Boards for the NRF24L01 Receiver and Transmitter

 

The code uses the RF24 library.

In the transmitter code we set the address to 00001, open the writing pipe, set the output RF power to minimum, set datarate to 250kbps and set the channel to channel 100. We place the text we are going to send in a character string then transmit the data and repeat transmitting every 1 second.

NRF24L01 Transmitter Code :

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

RF24 transmit (2,3);                    //create RF24 object called transmit

byte address [6] = “00001”;             //set address to 00001

void setup() {
  transmit.begin();
  transmit.openWritingPipe(address);    //open writing pipe to address 00001
  transmit.setPALevel(RF24_PA_MIN);     //set RF power output to minimum
  transmit.setDataRate(RF24_250KBPS);   //set datarate to 250kbps
  transmit.setChannel(100);             //set frequency to channel 100
  transmit.stopListening();
   }

void loop() {

  char data[32] = “It Works !”;         //send character string upto 32 bytes long
  transmit.write(&data,sizeof(data));   //transmit the data
  delay(1000);      
}

Looking at the receiver, we setup  the same parameters as the transmitter using the same address, channel and data rate. Then we wait for data to be received. Once it is received we output the data to the serial monitor.

 

NRF24L01 Receiver Code :

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

RF24 receive (2,3);                         //create object called receive
byte address [5] = “00001”;                 //creat an array with 5 elements, where each element is 1 byte;

void setup() {
  Serial.begin(9600);
  Serial.print(“Starting Receiver \n”);
  receive.begin();
  receive.openReadingPipe(0,address);      //open reading pipe 0 at address 00001
  receive.setPALevel(RF24_PA_MIN);         //Set RF output to minimum
  receive.setDataRate(RF24_250KBPS);       //set datarate to 250kbps
  receive.setChannel(100);                 //set frequency to channel 100
  receive.startListening();                

  }

void loop() {

  if (receive.available())                //check when received data available
  {
    char data[32];
    receive.read(&data, sizeof(data));
    Serial.print(data);                   //print data to serial monitor
  }
}

 

Next we upload the transmitter code to the transmit board and the receive code to the receiver board.

Once the code has been uploaded, power up both circuits and make sure the receiver is connected to the PC, then start the serial monitor, make sure the board rate is set to 9600,  and if all is working correctly you should see the message “It Works !” being displayed repeatedly on the serial monitor every second.

 

NRF24L01 Wireless Rain Gauge Sensor

Next we’ll add the rain gauge sensor and change a few bits of code in the transmitter. Every time the rain sensor is activated we will add 0.8mm to the total rainfall variable and send the updated value to the receiver.

As the rain sensor is effectively a reed switch that gets activated by a magnet swinging by, we need to add a 10k pull up resistor to the sensor input to make sure the input does not float and will either be 5V when the sensor is activated or 0V when there is no activity.

The transmitter code is slightly modified by setting up pin 4 as a digital input then every time the rain sensor is activated, 0.8 is added to the rainfall variable. We continue to transmit the rainfall variable data every 1 second.

Also as the rainfall variable is a float, we need to convert this to a character string using the dtostrf function.

Wireless Rain Gauge Transmitter Code :

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

RF24 transmit (2,3);                            //create RF24 object called transmit
byte address [6] = “00001”;                     //set address to 00001
float rainfall = 0;                             //rainfall float variable
int rain_sensor = 4;                            //set rainsenor to pin 4
char data[32];                                  //define character string called data that is 32 bytes long

void setup() {
  transmit.begin();
  transmit.openWritingPipe(address);            //open writing pipe to address 00001
  transmit.setPALevel(RF24_PA_MIN);             //set RF power output to minimum
  transmit.setDataRate(RF24_250KBPS);           //set datarate to 250kbps
  transmit.setChannel(100);                     //set frequency to channel 100
  transmit.stopListening();
digitalWrite(rain_sensor, LOW);               //set pin 4 low
pinMode(rain_sensor, INPUT);                  //set pin 4 as an input
   }

float get_rainfall()                          //get_rainfall function
{
 a:  if (digitalRead(rain_sensor) == HIGH)
          {
  return 0.8;
          }         
 goto a;           
}

void loop() {
  get_rainfall();
  rainfall = rainfall + get_rainfall();
  dtostrf(rainfall,10,2,data);                 //convert float to char string
  transmit.write(&data,sizeof(data));         //transmit the data
  delay(1000);      
}

 

Wireless Rain Gauge Receiver Code :

The only difference in the new receiver code is the addition of Serial.print(” mm\n”); to display the rainfall units in the serial monitor.

 

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

RF24 receive (2,3);                         //create object called receive
byte address [5] = “00001”;                 //creat an array with 5 elements, where each element is 1 byte;

void setup() {
  Serial.begin(9600);
  Serial.print(“Starting Receiver \n”);
  receive.begin();
  receive.openReadingPipe(0,address);      //open reading pipe 0 at address 00001
  receive.setPALevel(RF24_PA_MIN);         //Set RF output to minimum
  receive.setDataRate(RF24_250KBPS);       //set datarate to 250kbps
  receive.setChannel(100);                 //set frequency to channel 100
  receive.startListening();                

  }

void loop() {

  if (receive.available())                //check when received data available
  {
    char data[32];
    receive.read(&data, sizeof(data));
    Serial.print(data);                   //print data to serial monitor
    Serial.print(” mm\n”);
  }

}

How to Setup and use the nRF24L01 in your Projects | Summary

 

Using a one way wireless link can help you expand your project capabilities and is very useful where there are long distances involved or where it is not practical to use wires. Luckily for us makers and electroncis hobbyists, the nRF24L01 is cheap and has an easy to use RF24 library.

I hope this tutorial has been useful to you.

If you liked it please share it with a Friend !

Subscribe Now !

Receive updates and new information straight to your inbox.