:: Nrf2401 ::

This library provides an interface to Nordic's Nrf2401 2.4GHz radio transceiver. The module used for testing was a small breakout from Sparkfun. The Nrf2401 is a half-duplex transceiver, so while it has the ability to send and receive data, it cannot do both simultaneously. With this library Arduino can program it to choose from 256 possible channels, select from 65536 possible recipient addresses, and deliver anywhere between 1 and 25 bytes of raw data per packet at transmission rates of up to 1 megabit per second. Don't be scared by all the numbers though, the things you'll actually want to do with a device like this can be summed up quickly:

  • switching the device between receive and transmit modes
  • specifying a message size
  • specifying home and recipient addresses
  • sending a message
  • receiving a message

Accordingly, a class is supplied with methods that make doing these things as easy as possible. Additionally there are some properties available to allow for configuration customization. The documentation section offers detailed descriptions for these, and the examples section gives two brief demonstrations.

Download -> Nrf2401.zip

To install, simply unzip and put the files in Arduino/hardware/libraries/Nrf2401/


Documentation - Pin connections and descriptions of important methods and properties


Pin connections:

  • DR1 -> 2 (digital pin 2)
  • CE -> 3
  • CS -> 4
  • CLK -> 5
  • DAT -> 6

Methods:

  • void txMode(unsigned char messageSize=0);
    • You must call this method or "rxMode()" in order to initalize and configure the radio. The messageSize parameter is a number that specifies the number of bytes you would like to send or receive (max 25). If you omit this parameter, you can simply switch modes without updating the radio's configuration data. You must specify the messageSize parameter when you call either of the mode switching methods in order to update the configuration properties.

  • void rxMode(unsigned char messageSize=0);
    • Same as above, but sets the radio in receive mode.

  • bool available(void);
    • Calling this method will return a boolean value (true or false) depending on whether or not data is available to be read.

  • void read(unsigned char* dataBuffer=0);
    • When data is available to be read, the DR1 pin on the radio hardware will go high. This can be detected by polling the with the "available()" method, or attaching an interrupt on digital pin 2 that looks for a rising edge. When you are sure data is ready to be read, calling this function will automatically download the available bytes into the "data" array property. Optionally, you can specify your own data array to download to as the first parameter.

  • void write(unsigned char dataByte);
  • void write(unsigned char* dataBuffer=0);
    • At any point while in transmit mode, you can call this method to send the number of bytes specified by message size parameter to the "txMode()" method from the "data" array property. Optionally, you can specify your own data array to send from by adding it as the first parameter, or if you have just one byte to send, you can simply include the value directly.

Properties:

  • unsigned char data[NRF2401_BUFFER_SIZE];
    • An array that can store up to 25 bytes of incoming or outgoing data. The NRF2401_BUFFER_SIZE constant is 25 by default - open the header file if you want to change this.

  • unsigned int remoteAddress;
    • A number that represents the address you would like to send data to.

  • unsigned int localAddress;
    • A number that represents the address you would like other radios to use when they send data to you.

  • unsigned char dataRate;
    • A number. 0 means 250Kbit/second, 1 means 1Mbit/second. Set to 1Mbit/second by default.

  • unsigned char channel;
    • A number between 0 and 255 that represents the frequency shift from a base of 2.4GHz in 1MHz intervals. Note that the minimum recommended spacing for channels is 8MHz. Set to 127 by default.

  • unsigned char power;
    • A number between 0 and 3, 3 being the most powerful and default setting.

  • unsigned char mode;
    • A number. 0 means the radio is in transmit mode, 1 means receive mode. This property does not need to be written as it is updated automatically when you call "rxMode()" or "txMode()".


Example 1 - A one way data transmission example.


Transmitter code:

  1. /*
  2.  *  Nrf2401 library example 1 - TX
  3.  *  March 2009 | jesse dot tane at gmail dot com
  4.  *
  5.  *  Send a 3 byte message every 2 seconds to the device at address 1.
  6.  *
  7.  */
  8.  
  9. #include "Nrf2401.h"
  10.  
  11. Nrf2401 Radio;
  12.  
  13. void setup(void)
  14. {
  15.   Radio.remoteAddress = 1;
  16.   Radio.txMode(3);
  17.   Radio.data[0] = 22;
  18.   Radio.data[1] = 33;
  19.   Radio.data[2] = 44;
  20. }
  21.  
  22. void loop(void)
  23. {
  24.   Radio.write();
  25.   delay(2000);
  26. }
  27.  

Receiver code:

  1. /*
  2.  *  Nrf2401 library example 1 - RX
  3.  *  March 2009 | jesse dot tane at gmail dot com
  4.  *
  5.  *  Listen for incoming messages targeting the device's local address
  6.  *  of 3 byte length, whose contents equal 22, 33 and 44 respectively.
  7.  *  If such a message is encountered, blink an LED on pin 13 for half a second.
  8.  *
  9.  */
  10.  
  11. #include "Nrf2401.h"
  12.  
  13. Nrf2401 Radio;
  14.  
  15. void setup(void)
  16. {
  17.   pinMode(13, OUTPUT);
  18.   Radio.localAddress = 1;
  19.   Radio.rxMode(3);
  20. }
  21.  
  22. void loop(void)
  23. {
  24.   while(!Radio.available());
  25.   Radio.read();
  26.   if(Radio.data[0] == 22 && Radio.data[1] == 33 && Radio.data[2] == 44)
  27.   {
  28.     digitalWrite(13, HIGH);
  29.     delay(500);
  30.     digitalWrite(13, LOW);
  31.   }
  32. }
  33.  


Example 2 - Establishes a bi-directional communications link and demonstrates using an interrupt to handle data reception.


  1. /*
  2.  *  Nrf2401 library example 2
  3.  *  April 2009 | jesse dot tane at gmail dot com
  4.  *
  5.  *  If two Arduinos are running this program, they will
  6.  *  automatically detect each other and synchronize their
  7.  *  transmission and reception in a very simple "blocking"
  8.  *  scheme - alternately blinking an LED for 1 second at a
  9.  *  time. In this example, a receiver will "block" (not
  10.  *  send any data) until it has successfully received data.
  11.  *  The synchronization takes place at the beginning of
  12.  *  the program only however, so if the connection is lost
  13.  *  mid-program, it could hang.
  14.  *
  15.  */
  16.  
  17. #include "Nrf2401.h"
  18.  
  19. Nrf2401 Radio;
  20. volatile bool linkEstablished = false;
  21. unsigned char hello = 128;
  22.  
  23. // note that the "linkEstablished" variable is declared volatile
  24. // this prevents the compiler from optimizing it out as it is only
  25. // modified by the interrupt handler - a function that we never directly call
  26.  
  27. void setup(void)
  28. {
  29.   pinMode(13, OUTPUT);
  30.   randomSeed(analogRead(7));                    // start the random number generator
  31.   attachInterrupt(0, messageReceived, RISING);  // look for rising edges on digital pin 2
  32.   Radio.txMode(1);
  33.   synchronize();
  34. }
  35.  
  36. void synchronize(void)
  37. {
  38.   while(!linkEstablished)
  39.   {
  40.     Radio.txMode();
  41.     Radio.write(hello);
  42.     Radio.rxMode();
  43.     delay(random(100));  // wait random times in between transmissions
  44.   }                      // in case the two Arduinos started running
  45. }                        // code at the same instant
  46.  
  47. void loop(void)
  48. {
  49.   while(Radio.mode)
  50.   {
  51.     // block (or do something else) here while the radio is
  52.     // in rxMode (Radio.mode is 1 in rxMode, 0 in txMode)
  53.   }
  54.   digitalWrite(13, HIGH);
  55.   delay(1000);
  56.   digitalWrite(13, LOW);
  57.   Radio.write(hello);
  58.   Radio.rxMode();
  59. }
  60.  
  61. void messageReceived(void)
  62. {
  63.   Radio.read();
  64.   Radio.txMode();
  65.   if(!linkEstablished && Radio.data[0] == hello) linkEstablished = true;
  66. }
  67.  

Share