Arduino with NRF24L01

Objective:

Connect two NRF24L01 (2.4GHz RF transceiver module) with 2 different Arduinos, one is connected to a push button and other one connected to LED, download and setup necessary libraries in Arduino IDE. Write necessary code to control the LED connected to one Arduino wirelessly using push button connected to other Arduino using aforementioned RF transceiver module.

Working of NRF24L01

It is wireless transceiver module operating at the frequency of 2.4GHz, with the range of 100 meters and data transmission speed from 250kbps to 2Mbps.

Using FSK(Frequency shift key) modulation we can create upto 125channel at the spacing of 1MHz and can have upto 6 different addresses at a particular channel so one unit can communicate with six other units in a same time.

NRF24L01 pin description

cod screen shot

Resources needed

SNo Component Quantity
1 Arduino uno 2
2 NRF24L01 2
3 LED 1
4 Male to Female Jumper wire 14
5 Breadboard 1
6 Push button 1
7 Resistor 10KOhm 1

Libraries needed

SNo Library Name & link Description
1 NRF24L01 Library to talk to our NRF24L01 module via SPI communication.
2 SPI Serial Peripheral Interface

Setting up the libraries

Once we download the library from the above links, launch Arduino IDE, go to Sketch ->Include library ->Add.Zip Library and select the downloaded file and press open. We will have SPI library by default.

Fritzing:

Transmitter cod screen shot

Receiver cod screen shot

Snapshot of Transmitter code

cod screen shot

Code explanation for transmitter

We import the SPI library to establish communication with the transceiver module and nrf24L01.h and RF24.h to utilize the APIs to communicate with NRF24L01 transceiver module. We create radio object of RF24 module that select and enable the chip of NRF24L01 module using Arduino digital pins. and we use the address 000001 for our communication, both transmitter and receiver should have same address.

In our setup function we begin the radio and open the address specified above and radio.setPALevel(RF24_PA_MIN); will setup the power transmitter of our RF module and we disable listening to make it ct as transmitter.

Rest is a typical Arduino program that reads the digital pin of Arduino which is connected to push button and transmit wirelessly using radio.write(); function, and we give us a delay of 1 second.

Snapshot of Receiver code

cod screen shot

Code Explanation for receiver

Here for receiver, everything is same but we are listening for radio signals using radio.available(); function. Value we are receiving here is a boolean value. button pressed = 1 & not pressed = 0. If button is pressed we turn ON the LED else we turn off the LED.

Transmitter Code

#include
#include
#include
RF24 radio(7, 8); // CE, SCN
const byte address[6] = "00001";
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
Serial.begin(9600);
pinMode(4,INPUT);
}
void loop() {
int x =digitalRead(4);
radio.write(&x, sizeof(x));
delay(1000); }

Receiver code

#include
#include
#include
RF24 radio(7, 8); // CE, SCN
const byte address[6] = "00001";
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
pinMode(2,OUTPUT);
}
void loop() {
if (radio.available()) {
int x;
radio.read(&x, sizeof(x));
if(x){
digitalWrite(2,HIGH);
Serial.println(x);
}
else{
digitalWrite(2,LOW);
}
}
}
Here is the link to the video that shows the working of the above project.