nRF24L01 Module Demo for Arduino

2028_thumb1

First of all, if you have any problem, please contact us and any feedback is welcome, and the code do not use Arduino’s SPI library, but softwore imitate SPI, maybe you can modified and complete it.

The nRF24L01 module can be controlled in many ways, one of which is Arduino. We tried and tested it, then made a demo. Let’s see how it works with Arduino.

First of all, of course, you need two Arduino boards and at least two RF modules, one to transmit and the other to receive. If you don’t have, you can get them in our store in a very low price. (Order nRF24L01 module)

If you use a standard Arduino board, you should use the Arduino board’s 3.3V pin(VDD3V3) to provide power.  Beacuse the nRF24L01 module is worked at 1.9-3.6V voltage level. Please note, do not use 5V pin(VDD5V) to provide power, which may destroy it.

The Demo pins to Arduino as below:

GND – GND, VCC – 3.3V, CS – D8, CSN – D9, SCK – D10, MOSI – D11, MISO – D12, IRQ – D13

Download the code below into the TX Arduino (transmit) — This code will drive the nRF24L01 module to send out data form 0×00 to 0xFF .

Note, between the write TX_FIFO and clear RX_DR or TX_DS or MAX_RT interrupt flag, would better not serial print anything, which maybe case ACK failed.


void setup()
{
  SPI_DIR = ( CE + SCK + CSN + MOSI);
  SPI_DIR &=~ ( IRQ + MISO);
  //  attachInterrupt(1, _ISR, LOW);// interrupt enable
  Serial.begin(9600);
  init_io();                        // Initialize IO port
  unsigned char status=SPI_Read(STATUS);
  Serial.print("status = ");
  Serial.println(status,HEX);      // read the mode’s status register, the default value should be ‘E’
  Serial.println("*******************TX_Mode Start****************************");
  TX_Mode();                       // set TX mode
}
void loop()
{
  int k = 0;
  for(;;)
  {
    for(int i=0; i<32; i++)
        tx_buf[i] = k++;
    unsigned char status = SPI_Read(STATUS);                // read register STATUS's value
    if(status&TX_DS)                                        // if receive data ready (TX_DS) interrupt
    {
      SPI_RW_Reg(FLUSH_TX,0);
      SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);     // write playload to TX_FIFO
    }
    if(status&MAX_RT)                                       // this is retransmit than  SETUP_RETR
    {
      SPI_RW_Reg(FLUSH_TX,0);
      SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);     // disable standy-mode
    }
    SPI_RW_Reg(WRITE_REG+STATUS,status);                    // clear RX_DR or TX_DS or MAX_RT interrupt flag
    delay(1000);
  }
}

 

 

 

 

 

 

Download the code below into the RX Arduino (receive) – This code will drive the nFR24L01 module to receive the data that transmit form the TX module and print it to serial port.

Note, clear RX_FIFO must bellow Read_FIFO


void setup()
{
  SPI_DIR = ( CE + SCK + CSN + MOSI);
  SPI_DIR &=~ ( IRQ + MISO);
  //  attachInterrupt(1, _ISR, LOW); // interrupt enable
  Serial.begin(9600);
  init_io();                        // Initialize IO port
  unsigned char status=SPI_Read(STATUS);
  Serial.print("status = ");
  Serial.println(status,HEX);      // read the mode’s status register, the default value should be ‘E’
  Serial.println("*****************RX_Mode start******************************R");
  RX_Mode();                        // set RX mode
}
void loop()
{
  for(;;)
  {
    unsigned char status = SPI_Read(STATUS);                // read register STATUS's value
    if(status&RX_DR)                                        // if receive data ready (TX_DS) interrupt
    {
      SPI_Read_Buf(RD_RX_PLOAD, rx_buf, TX_PLOAD_WIDTH);    // read playload to rx_buf
      SPI_RW_Reg(FLUSH_RX,0);                               // clear RX_FIFO
      for(int i=0; i<32; i++)
      {
          Serial.print(" ");
          Serial.print(rx_buf[i],HEX);                      // print rx_buf
      }
      Serial.println(" ");
    }
    SPI_RW_Reg(WRITE_REG+STATUS,status);                    // clear RX_DR/TX_DS/MAX_RT interrupt flag
    delay(1000);
  }
}

 

 

 

 

 

Now power on both Arduino , and connect the RX one to PC via USB. Open the IDE serial port monitor , change the baud rate to 9600 bps , and you can see the data that received.

If you want to change Arduino pin connecting to module , just modify the define on the NRF24L01.h .

All the project here(include API.h and NRF24L01.h)

 

 

Download nRF24L01_Demo_For_Arduino.

Download nRF24L01_Demo_For_Arduino_v1.1.

nRF24L01 with Arduinio’s SPI Library

 

VN:F [1.9.13_1145]
Rating: 7.8/10 (10 votes cast)
VN:F [1.9.13_1145]
Rating: +4 (from 4 votes)
nRF24L01 Module Demo for Arduino, 7.8 out of 10 based on 10 ratings

Related Posts

  • 为什么我接上nrf24l01,用的是上面的代码,每次显示的结果都为0

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: -1 (from 1 vote)
  • 如果不接无线模块,在串口调试窗口为什么不能看到以上的数据!

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: -1 (from 1 vote)
    • http://elecfreakc.com Kathy Yang

      Because arduino needs to read the state. If without wireless module connected, the state read would be false, so you cannot see the serial debug window outputs any data

      VN:F [1.9.13_1145]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.13_1145]
      Rating: 0 (from 0 votes)
  • http://mp.weixin.qq.com/s?__biz=MzA3MjQ2MjczNQ==&mid=200332199&idx=1&sn=9dd18a6a225d0d8e89d34e2419a7e723&key=0cbeb7802f7d2a1e884771191b34f2b77d5e47fe3a8e016b99d6d05eace0c31ea62ea76dd24 how to fix error 3194

    You almost certainly would have experienced so many
    errors, when bringing up-to-date or downgrading your current iPhone, iPod, or perhaps iPad Firmware.
    Fixing these types of errors do are terrible and waste a great deal time and first and foremost put us in tension, i.

    e oh what happens to my iphone? Will it again begin to work normally as well as not?
    You often have heard this sentence in your essay in life, Nothing is
    impossible! and it is 100% true, these errors is usually easily fixed, provided you’re
    willing to while using correct knowledge and also
    enthusiasm. Today, I will show you how using the step by step instructions, how you are able to
    fix the Mistake 3194 in iTunes as soon as Restoring
    or Updating your iOS Unit. Before we begin, first I would recommend you to
    definitely understand and troubleshoot, Why you’re having the error 3194, even though
    updating or rebuilding your iPhone, ipad, iPod or
    ipod itouch. The error 3194 comes about only, when you looking to install old firmware in addition to
    Apple’s server disallows the particular installation, it is named
    firmware downgrade.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: -2 (from 2 votes)
  • http://showthemessageisnotmywantit? bruceLiu

    I use this code also successful, but the message displayed is not the same with you, I also received the corners right, display a visual signal a value FF, where I have used wrong?

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • rushil

    DIR_SIP was not declared in this scope for arduino and nRF24l01
    this is error i am getting any ans for it

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • rushil

    how to i declere all pins in this

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • rushil

    same question

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • http://www.megahackload.com/odin-quest-hack/ online mmorpgs

    Great blog here! Also your site loads upp very fast!

    What web host are you using? Can I get our affiliate link to
    your host? I wish my site loaded up ass fast
    as yourrs lol

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: -1 (from 1 vote)
  • Scollop

    I cannot find any declaration of SPI_DIR, CE, SCK aso. Compiler told me about many vars “not declared in this scope”. Where I can find the declarations ?
    regards

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: +1 (from 1 vote)
  • tks

    i tried the code v1.1 on my arduino mega but it can not work, there are some errors inside the code
    nRF24l01_TX.pde:-1: error: expected constructor, destructor, or type conversion before ‘unsigned’
    nRF24l01_TX.cpp: In function ‘void TX_Mode()’:
    nRF24l01_TX.pde:-1: error: ‘TX_ADDRESS’ was not declared in this scope

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    • robi

      Dear sir, maybe you could try to Arduino IDE0023, because of the demo code has not post to 1.0.x yet

      VN:F [1.9.13_1145]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.13_1145]
      Rating: 0 (from 0 votes)
  • Dauhee

    Can these units be used for send and receive or must they always be a dedicated send or receive? I have tried 2 way comms but intermittently a message is lost :(

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • nobody1

    Very much appreciated!! Just ordered a couple of these modules today. Keep up the good work!

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Pingback: MP3 Music Shield – Dance With Your Arduino | ElecFreaks | The MP3 Music Players

  • Joseph Mcgowan

    The UNO SPI SCK pin is 13, not 10 as you declare… this messed me up for a few hours, so please edit your text.

    Here is a complete pinout for Uno and Mega:

    Mega SPI pinout nRF24F01+ pinout
    #define SCK_PIN 52 = 5
    #define MISO_PIN 50 = 7
    #define MOSI_PIN 51 = 6
    #define SS_PIN 53 = not used

    CE = D8 = 3
    CSN = D10 = 4 (my perfboard is set up for CSN on 10)

    Uno pinout = nRF24L01+ pinout

    GND – GND 1
    VCC – 3.3V 2
    CS (CE) – D8 3
    CSN – D10 4
    SCK – D13 5
    MOSI – D11 6
    MISO – D12 7
    IRQ – 8

    nRF24L01+ Pinout

    1 (square = GND
    2 = VCC (3.3v)
    3 = CE
    4 = CSN
    5 = SCK
    6 = MOSI
    7 = MISO
    8 = IRQ (not used mostly)

    Thanks.

    RadFly

    VA:F [1.9.13_1145]
    Rating: 5.0/5 (1 vote cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Johann

    Guys any idea how to fix this ? i get it when i try compile

    In file included from C:\Users\Johann\Desktop\arduino-1.0.1\hardware\arduino\cores\arduino/Arduino.h:213,
    from nRF24l01_TX.cpp:32:
    C:\Users\Johann\Desktop\arduino-1.0.1\hardware\arduino\variants\standard/pins_arduino.h:41: error: expected unqualified-id before numeric constant
    C:\Users\Johann\Desktop\arduino-1.0.1\hardware\arduino\variants\standard/pins_arduino.h:42: error: expected unqualified-id before numeric constant
    C:\Users\Johann\Desktop\arduino-1.0.1\hardware\arduino\variants\standard/pins_arduino.h:43: error: expected unqualified-id before numeric constant

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    • Galland

      If you look into the pins_arduino.h you will see that it defines MISO, MOSI and SCK pins.
      These are set also in NRF24L01.h, and that’s the problem. I fixed it by renaming the pins in the nRF24L01 project. (MISO –> MISO_pin in both the .h and the .ino, same for MOSI and SCK)

      Hope it helps.

      VA:F [1.9.13_1145]
      Rating: 5.0/5 (2 votes cast)
      VA:F [1.9.13_1145]
      Rating: +4 (from 4 votes)
  • Pingback: AUTOMALABS &raquo Módulo de comunicação sem fio NRF24L01

  • Pingback: nRF24L01f+Arduino 代码改进 | 醉己诏

  • Nick

    Thank you for prompt answer and fix, all my 3 modules worked fine with updated library and same wiring!

    Reading through datasheet I tried to set registers to lower datarate (and extend range) to 256K like this:
    SPI_RW_Reg(WRITE_REG + RF_SETUP, 0×27);
    Is this right setting for 256K speed? I saw that you’ve set RF_PWR register to 0 dbm for the transmitter. Will setting RF_PWR to -18dBm allow to extend range?

    Is it possible to transmit/receive data on different pipes simultaneously?
    I have few spare RF antenna connectors, is to possible to solder this connectors to internal antenna and have extended range like nRF24L01p+PA+LNA?

    Thank you!

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    • robi

      That’s a good news!
      Frist, SPI_RW_Reg(WRITE_REG + RF_SETUP, 0×27) setting for 256Kbps. Yes, and you setting the RF_PWR to 0dBm at the same time.

      Second, setting RF_PWR to -18dBm will allow to shorter range. Maybe you should setting ox2o to RF_SETUP for testing.

      Third, the nRF24L01 support 6 data pipe, duplex communication can be achieved.

      At last, solder a extend antenna, I think you’d better not to do. Then antenna’s length been customize and calculated many times. The antenna is not longer the better.

      VN:F [1.9.13_1145]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.13_1145]
      Rating: 0 (from 0 votes)
  • Nick

    Hello!

    I’ve got 3 modules of http://www.elecfreaks.com/store/24g-wireless-nrf24l01p-p-118.html nRF24L01P. I have Arduino Duemilanove 328P clone and Freeduino ADK board (which is Arduino Mega 2560). I tried your configuration changing RF modules few times connecting Arduino’s together. It seems that Receiver works well, I’ve got status 2E, same as on your screenshot. But Receiver always returns status=0. What should I do?

    I connect RF modules as follows: GND-GND, VCC-3v, CS – D8, CSN – D9, SCK – D10, MOSI – D11, MISO – D12, IRQ – D13.

    Are there any changes for Arduino Mega (Freeduino ADK) in the code?

    Thank you, Nick

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    • robi

      Hi Nick,
      That a good question. Thanks for you feedback.
      The demo doesn’t work well, because of the IO there are different define between with Arduino Dumilanove 328p and Arduino Mega, which D8, D9 pin on Arduino Dumilanove board is PORTB0 and PORTB1, but on Arduino Mega is PORTH5 and PORTH6. So the code must changes the pins define for Arduino Mega(Freaduino ADK).
      And now, we have update the demo code to v1.1 that compatible with Arduino Dumilanove and Mega, get from http://elecfreaks.com/store/download/datasheet/rf/rf24l01/nRF24L01_Demo_For_Arduino_v1.1.zip.
      Please try it again.
      If you still have any problems, please feel free to contact us.
      Good Luck!

      VA:F [1.9.13_1145]
      Rating: 0.0/5 (0 votes cast)
      VA:F [1.9.13_1145]
      Rating: 0 (from 0 votes)
      • pekka

        zip is corrupted.

        VA:F [1.9.13_1145]
        Rating: 0.0/5 (0 votes cast)
        VA:F [1.9.13_1145]
        Rating: 0 (from 0 votes)
        • Servoguy

          For some reason it’s actually a RAR file with a .zip extenstion. 7-Zip will open it fine.

          VA:F [1.9.13_1145]
          Rating: 0.0/5 (0 votes cast)
          VA:F [1.9.13_1145]
          Rating: 0 (from 0 votes)
  • Pingback: nRF24L01+ with Arduinio’s SPI Library | ElecFreaks

  • http://atmega128.org Paul Richards

    Code works! thank you

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    • robi

      That’s good news. Well done!
      And your question about define IRQ, this demo wasn’t use it but determine the state from read register STATUS’s value.
      Of course, you can modify and enjoy it.

      VN:F [1.9.13_1145]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.13_1145]
      Rating: 0 (from 0 votes)
  • http://atmega128.org Paul Richards

    I’m looking at your code for the nrf24l01, you made a comment:
    If you want to change Arduino pin connecting to module , just modify the define on the NRF24L01.h .
    The current NRF24L01.h pin out does not match your description…especially IRQ = 0×20?
    Thank you for your response.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • http://www.mstuffetsmuffet.com Jenise Steininger

    Keep posting stuff like this i really like it

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: +1 (from 1 vote)
  • Ross

    Hi,

    What range have you achieved? Indoors? Outdoors?

    Thanks,

    Ross

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    • robi

      Hi Ross,
      the range about nRF24L01 at 1Mbps is 40m outdoor and 15m indoor.The lower rate of transmission is far more.
      If you want greater range,more information http://www.elecfreaks.com/323.html.

      VN:F [1.9.13_1145]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.13_1145]
      Rating: -1 (from 1 vote)
  • http://www.lockpipesz.org daigoumee

    Keep posting stuff like this i really like it

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    • admin

      Thank you. We will do our best.

      VN:F [1.9.13_1145]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.13_1145]
      Rating: +1 (from 1 vote)
  • Pingback: 2.4G Wireless RF Module Demo for Arduino

  • http://www.step-two.ru/2011/05/the-ultimate-family-vacation-is-camping-2/ cybermagnetik

    What a great web log. I spend hours on the net reading blogs, about tons of various subjects. I have to first of all give praise to whoever created your theme and second of all to you for writing what i can only describe as an fabulous article. I honestly believe there is a skill to writing articles that only very few posses and honestly you got it. The combining of demonstrative and upper-class content is by all odds super rare with the astronomic amount of blogs on the cyberspace.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: +1 (from 1 vote)
    • admin

      We are very glad you like it. It is prised so highly by you is honor of our team and every team member.
      We are a young team, which can not be better without supports of you all. If you think ElecFreaks is helpful, you may share it with you friends. And if you have any suggestions, please let us kown.
      Thank you very much.

      VN:F [1.9.13_1145]
      Rating: 1.0/5 (2 votes cast)
      VN:F [1.9.13_1145]
      Rating: 0 (from 0 votes)
Go To Top