Menu Close

nRF24L01 with Arduino UNO

Robo India presents tutorial to explains bidirectional communication between two circuits

Detailed Tutorial

1. Introduction:

The nRF24L01 is a single chip 2.4GHz transceiver with an embedded baseband protocol engine, designed for ultra low power wireless applications.This device is designed for operation in the world wide ISM frequency band at 2.400 – 2.4835GHz. An MCU (microcontroller) and very few external passive components are needed to design a radio system with the nRF24L01.

The nRF24L01 is configured and operated through a Serial Peripheral Interface (SPI.) Through this interface the register map is available.

The radio front end uses GFSK modulation. It has user configurable parameters like frequency channel, output power and air data rate. The air data rate supported by the nRF24L01 is configurable to 2Mbps. The high air data rate combined with two powers saving modes makes the nRF24L01 very suitable for ultra low power designs.

2 Pin Configuration

nRF24L01 is very universal and fits for all the Arduino.

1. CSN: SPI Chip Select.

2. CE: Chip Enable activates Rx & Tx modes. CE = 0 makes the chip to go into stand-by. (These are digital pins and can be connected to any digital pin or Arduino).

3. MISO: SPI Slave Data Output. (Digital Input)

4. MOSI: SPI Slave Data Input. (Digital Output)

5. SCK: SPI Clock Input. (Digital Input)

6. IRQ: Maskable Interrupt Pin. Active Low (Digital Output)

7. GND: Connects to system ground.

8. VCC: Power Supply. Input to voltage Regulator (3.3V) If we use 5V instead, we will probably burn the chip, since 5V is outside of its 1.9 to 3.6V supply range.

In this tutorial we established bidirectional connection between two circuits. When transmitter sends signal by pressing button, led will turn on after receiving the signal.

3. Circuit

Make the following connections with Arduino –

4. Building Circuit

Transmitter:

Receiver:

5. Library File

Following library will be required to run this sketch. Download the zip file extract the same and copy this to your Arduino library folder.

This library file should be placed at the install folder of Arduino. I have a 64 bit Win7 OS and my arduino library folder address is located at

C:\Program Files (x86)\Arduino\libraries 

You may download library file from here.

6. Programming:

Once the circuit part is done, Arduino is needed to be programmed. Here is the code to run this circuit on Arduino IDE.

You may download this code (Transmitter Code) from here.

//Robo India Tutorial on nRF24L01
//Simple Code for transmitter


#include <SPI.h>                            //Communication Interface with modem
#include "nRF24L01.h"
#include "RF24.h"                          //library to control radio modem
 
RF24 radio(9,10);                          //Creating an object

const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
unsigned long Command ;
void setup()
   {
    Serial.begin(57600);
    pinMode(4,INPUT);
    radio.begin();                        //activates modem
    radio.setRetries(15,15);              //number of times modem retry to send data
    radio.openReadingPipe(1,pipes[1]);
    radio.startListening();
    radio.printDetails();
    radio.openWritingPipe(pipes[0]);     //sets address to recevier
    radio.openReadingPipe(1,pipes[1]);
    radio.stopListening();               //switch the modem to data transmission mode
   }
 
  void loop(void)
   {
    radio.stopListening();
    if (digitalRead(4)==1){
    Command=1;
   }
    else
      {
        Command=2;
      }
    radio.write( &Command, sizeof(unsigned long) );  //blocks the program until it receives the acknowledgment
    radio.startListening();
    delay(1000);                                     //stops program for one second
  }


You may download this code (Receiver Code) from here.

//Robo India Tutorial on nRF24L01
//Simple Code for Receiver


#include <SPI.h>                           //Communication Interface with modem
#include "nRF24L01.h"
#include "RF24.h"                          //library to control radio modem
 
RF24 radio(9,10);                          //Creating an object

const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
typedef enum { role_ping_out = 1, role_pong_back } role_e;
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
 
role_e role = role_pong_back;

void setup(void)
   {
    Serial.begin(57600);
    pinMode(4,OUTPUT);
    radio.begin();                         //activates modem
    radio.setRetries(15,15);
    radio.openReadingPipe(1,pipes[1]);
    radio.startListening();
    radio.printDetails();
    radio.openWritingPipe(pipes[1]);
    radio.openReadingPipe(1,pipes[0]);
    radio.startListening();
   }
 
 void loop(void)
   {
    if ( radio.available() )               //check receive data
       {
          unsigned long data = 0;
          radio.read( &data, sizeof(unsigned long) );
          Serial.println(data);
             if (data==1)
                  {
                    digitalWrite(4,HIGH);
                  }
            else
                  {
                    digitalWrite(4,LOW);
                  }
 
  delay(20);
       }
    }


6. Output

After uploading the Arduino IDE transmitter code and then the receiver code when we press the button, led attached to another Arduino board will glow. And when we release the button, led will turn off. Output will display on Serial monitor as well.

7. Troubleshooting

Led is not glowing: Try changing polarity of LED.

Still not glowing: Check the switch the multimeter, it’s terminal should be connected in pressed condition and vice-versa. .

Select the ports correctly for both Arduino boards.

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com

2 Comments

Leave a Reply