Hi guys, Welcome to today’s tutorial. Today, we will be building a simple project to demonstrate how easy it is to add a one-way wireless communication capability to our Arduino Projects using the NRF24L01. As we advance and build more Arduino projects, there will always be the need for us to set up communication between two devices for data to be sent and received from one end to the other. One of the ways to achieve this is through the use of the NRF24L01 transceiver module.

 

The NRF24L01 module is a low-cost (less than$3) ultra-low power, bi-directional transceiver module. It is designed to operate within the 2.4GHz ISM band which means it can be used for projects with industrial, scientific and medical applications.  The module can achieve data rates as high as 2Mbits! and uses a high-speed SPI interface in order to communicate with the Arduino and other kinds of microcontrollers and development boards.

NRF24L01 Module

One of the best features of this module asides the ease with which it can be used with Arduino and other microcontroller is its low power consumption. This module consumes, less than 14mA in full communication mode and consumes only a few microamps in power down mode. This makes it Ideal for projects with long battery life desires.

The goal of today’s project is simple, we simply want to demonstrate the ease with which data can be sent from one Arduino to the other using the NRF module. The first Arduino will be sending data every second and the second Arduino will receive the data and display it on the serial monitor.

Required Components and Where to Buy

The following components are required to build and follow this tutorial and they can be bought through the link in front of each component.

1. NRF24L01 ▶ https://educ8s.tv/part/NRF24L01

2. Cheap Arduino Uno ▶ https://educ8s.tv/part/ArduinoUno

3. Powerbank ▶ https://educ8s.tv/part/Powerbank

4. Wires▶ https://educ8s.tv/part/Wires

 

Schematics

The schematics for this project is quite simple, connect one NRF24L01 to each of the two Arduino boards that will be used for this project as shown in the schematics below.

Schematics

The NRF24L01 module has 8 pins but only 7 is used in connecting with the arduino. The pin connections is described below to aid the understanding of the schematics.

NRF24L01 ▶ Arduino

GND ▶ GND

VCC ▶ 3.3v

CE ▶ D7

CS ▶ D8

SCK ▶ D13

MOSI ▶ D11

MISO ▶ D12

A little downside to this module is that we can’t plug the module in the breadboard due to its form factor so we are going to use the male to female wires in order to connect the module to Arduino.

It’s important that the module’s VCC pin is not connected to the Arduino 5v pin as this will damage the NRF24L01 module. It is a 3.3V device.

Double check the schematics to be sure everything is as it should be before proceeding to the code.

Code

The code for this project will be in 2 part. one for the transmitter and one for the receiver. The transmitter basically performs the task of sending data at intervals to the receiver which then receives the message and prints it to the serial monitor. Both the transmitter and the receiver code are heavily reliant on the RF24 library which can be downloaded from the link below.

LIBRARIES

https://github.com/TMRh20/RF24

[adsense]

To do a brief explanation of the code starting with that for the Transmitter, the first thing we do, as usual, is to include the libraries that we will be using.

#include <SPI.h>  
#include "RF24.h"

Next, we declare the pins of the Arduino to which the CE and the CS pins of the NF24Lo1 are connected as they are arguments for the Rf24 library.

RF24 myRadio (7, 8);

Next, we create the struct package which will be used in sending the data.

#include <SPI.h>  
#include "RF24.h"byte addresses[][6] = {"0"};

struct package
{
  int id=1;
  float temperature = 18.3;
  char  text[100] = "Text to be transmitted";
};


typedef struct package Package;
Package data;

The void setup() function is next. Here we initialize the NRF module setting the communication channel, signal power, and the data rate.

void setup()
{
  Serial.begin(115200);
  delay(1000);
  myRadio.begin();  
  myRadio.setChannel(115); 
  myRadio.setPALevel(RF24_PA_MAX);
  myRadio.setDataRate( RF24_250KBPS ) ; 
  myRadio.openWritingPipe( addresses[0]);
  delay(1000);
}

Next, we move to the void loop function. the void loop function simply increases the temperature initialized in the struct package described initially then sends it to the receiver.

void loop()
{
  myRadio.write(&data, sizeof(data)); 

  Serial.print("\nPackage:");
  Serial.print(data.id);
  Serial.print("\n");
  Serial.println(data.temperature);
  Serial.println(data.text);
  data.id = data.id + 1;
  data.temperature = data.temperature+0.1;
  delay(1000);

}

The second code is that of the receiver. This receives data from the transmitter and displays it on the serial monitor.

The first thing we do here too is to include the libraries that will be used.

#include <SPI.h>  
#include "RF24.h"

Next, we create an object of the RF24 library and pass the pins of the Arduino to which the CE and CS pins of the NRF24l01 are connected to as arguments.

RF24 myRadio (7, 8);

Next, we create the struct package to receive the data with the temperature initialized to zero.

struct package
{
  int id=0;
  float temperature = 0.0;
  char  text[100] ="empty";
};

byte addresses[][6] = {"0"}; 



typedef struct package Package;
Package data;

With this done, we proceed to the void setup() function. Here we initialize the NRF module, setting the communication channel, signal power, and the data rate to match that of the transmitter.

void setup() 
{
  Serial.begin(115200);
  delay(1000);

  myRadio.begin(); 
  myRadio.setChannel(115); 
  myRadio.setPALevel(RF24_PA_MAX);
  myRadio.setDataRate( RF24_250KBPS ) ; 
  myRadio.openReadingPipe(1, addresses[0]);
  myRadio.startListening();
}

The void loop function for the receiver is fairly simple, All we will need to do is read the data that was received and print it on the serial monitor.

void loop()  
{

  if ( myRadio.available()) 
  {
    while (myRadio.available())
    {
      myRadio.read( &data, sizeof(data) );
    }
    Serial.print("\nPackage:");
    Serial.print(data.id);
    Serial.print("\n");
    Serial.println(data.temperature);
    Serial.println(data.text);
  }

}

The complete code for both the transmitter and the receiver can be downloaded by clicking on the link below.

——————–

CODE OF THE PROJECT
——————–

 

 

Upload the code to each board and keep the Arduino running the recevier code connected to the computer so you can view the data being displayed over the serial monitor. After a while, you should see the data from the transmitter being displayed on the serial monitor as shown in the Image below.

Received Data

That’s it for this tutorial guys, thanks for watching and(or) reading. Did you make any cool and interesting modification to this project or you have any questions, feel free to leave me a comment. Don’t forget to subscribe to our youtube channel by following the link below if you’ve not done so already, its one of the few ways you could show support for what we are doing.

 

SUBSCRIBE ON YOUTUBE

——————–

Never miss a video: Subscribe to educ8s.tv