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.
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.
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.
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
Does the part that gets transmitted have to be in the “struct package” loop
PLEAS SEND BACK QUICK
No, but putting things in structs makes them easy to send as one package. I think you could get rid of the struct, and the 2 redefinition it has, and then just replace it with another datatype.
Code will not compile.
Gives :
invalid conversion from ‘byte* {aka unsigned char*}’ to ‘uint64_t {aka long long unsigned int}’ [-fpermissive]
Any clues?
i have the the same issue while compiling… if u have some solution for it plz share it
change data type of address , make it unsigned long long int and don’t make a array..
bro change the include rf24.h from the error showing part = make this byte addresses[][6] = {“0”};
steve ,Thank you for the tutorial,but unfortunately I can not get it working despite trying it with nano’s and uno’s. in receive window it says twice empty packages. In the transmit window it says text to be transmitted and a scrolling number.Can you help?
Check very carful the connections and make it like explained in the video. Pause the video at any pin connecting explanation. There is no reason to fail in making the modules works if you fallows the step using the arduino uno. “Text to be transmitting” must be echoed in the receiving window together with the packed number and the pseudo temperature number. Make sure you first download the code for the sender into an arduino and then open a secon instance of arduino to download the code of the receiver in the second arduino but be sure it is conecte to a differen usb port.
I was having trouble making the rnf24l01 module works. There are some tutorial on the web that recommend the use of some capacitor to make the modules works with Arduino . However I was confident that there was somewhere a good tutorial which taught how to make these modules work. And finally I found here. May be I am wrong and simply I did not understood the others tutorial, but anyway the explanation in here in educ8c is very clear and understandable.
You must pay attention to where you connect the ce and cs pins from the rnf24 to the Arduino pins and be sure you use them as parameters for the RF24 radio(7,8) function. In the example here they use pin 7 and 8.
My wiring is correct but it displays these “2¹òüFÿ–‚ªý—–uþ“†uþ¹°4ÿäú–€ªýÆ,” what seems to be the problem.. please help
got same problem
Change your serial ports baud rate recording to the code
hocam peki bunu nasıl yapabiliriz
looks like the links to the code download no longer work.
Alan
I just tried the download link and it works fine. Please try again.
what sud i do when i cant get result from serial monitor ?
why in my coding there is an error at void setup where adresses was not declare on the scope?
Wonderful it works good for me, thank you for sharing
my arduino dont want to send any thing…. anyone pls help
I would like to point out that if you are having any trouble sending and receiving the data, to recheck your connections. As well, if your data is garbled, make sure your serial monitor baud is 115200. You can change it later on in the code, but the serial monitor and the code must match each other’s baud, or else you’ll get a bunch of random ASCII characters.
thankyou, this was much needed
the serial monitor only works when the transceiver is connected to the computer. when the receiver is connected, nothing shows on the serial monitor. it’s only when the transceiver is connected when the serial monitor shows data. the transceiver led blinks on the transceiver but the receiving led doesn’t blink on the receiving arduino. does anyone have an idea what’s wrong? please and thank you
MIne is showing RF24 libary is not there. But i have downloadeed and paste it in the libary folder.
Hi. I’m getting garbled data. I tried from a Nano to an uno where the nano was powered by a battery bank. Then I tried adding a cap. Finally I tried powering the nano from the battery pack via a dc v regulator set to 3.3 and still got garbage. I posted here:
https://forum.arduino.cc/index.php?topic=627524.0
Again check your baud rate. They must compare. 11520
merhabalar bende de gereksiz semboller çıkıyor ve siz Serial baud hızı kaydını koda dönüştürün demişsiniz nasıl yapılıyor
Would anyone be able to help me modify this code so that if I have an input go high on the TX the RX will turn an LED on?
Hello, everything works perfectly well. I only have an one issue. When I add more text to the struct text it is not send fully to the other side. I Also add one more value in the struct. my sender is like that
struct package
{
int id=1;
float temperature = 18.3;
char text[100] = “Text to be transmitted more text”;
char code = ‘1’;
};
and my receiver code is
struct package
{
int id=0;
float temperature = 0.0;
char text[100] =”n”;
char code = ‘5’;
};
however when data is printed in the serial monitor I dont get all the text but instead I get
Package:9127.30
Text to be transmitted mor
5
Here is my loop in receiver
void loop()
{
if ( myRadio.available())
{
while (myRadio.available())
{
myRadio.read( &data, sizeof(data) );
}
Serial.print(“Package:”);
Serial.print(data.id);
Serial.println(data.temperature);
Serial.println(data.text);
Serial.println(data.code);
if(data.code == ‘1’){
digitalWrite(4, HIGH);
}else{
digitalWrite(4, LOW);
}
}
}
I have this as my serial monitor output:
08:41:11.194 ->
08:41:12.258 ->
08:41:12.258 -> Package:0
08:41:12.258 -> 0.00
08:41:12.258 ->
08:41:13.274 ->
I am using nrf24+pa+lna module, do I need to connect irq pin to arduino?