nRF24L01 wireless transceiver

The Nordic Semi nRF21L01+ is a single-chip, 2.4GHz band wireless transceiver, capable of up to 2Mbps air data rate, multi-point reception, and auto-acknowledge / auto-retransmit. The nRF24L01+ consumes only 11.3mA while transmitting, and 13.5mA while receiving. It has a simple 6-pin digital connection, including a 4-wire SPI interface, an interrupt pin and dedicated transmit/receive enable pin. Sparkfun's module includes a chip antenna, and claims a range of 100m.

http://static.sparkfun.com//images/products/00691-03-L_i_ma.jpg

Hello World!

Import program

00001 #include "mbed.h"
00002 #include "nRF24L01P.h"
00003 
00004 Serial pc(USBTX, USBRX); // tx, rx
00005 
00006 nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10);    // mosi, miso, sck, csn, ce, irq
00007 
00008 DigitalOut myled1(LED1);
00009 DigitalOut myled2(LED2);
00010 
00011 int main() {
00012 
00013 // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's
00014 //  "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019)
00015 //  only handles 4 byte transfers in the ATMega code.
00016 #define TRANSFER_SIZE   4
00017 
00018     char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
00019     int txDataCnt = 0;
00020     int rxDataCnt = 0;
00021 
00022     my_nrf24l01p.powerUp();
00023 
00024     // Display the (default) setup of the nRF24L01+ chip
00025     pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
00026     pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
00027     pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
00028     pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
00029     pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
00030 
00031     pc.printf( "Type keys to test transfers:\r\n  (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
00032 
00033     my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
00034 
00035     my_nrf24l01p.setReceiveMode();
00036     my_nrf24l01p.enable();
00037 
00038     while (1) {
00039 
00040         // If we've received anything over the host serial link...
00041         if ( pc.readable() ) {
00042 
00043             // ...add it to the transmit buffer
00044             txData[txDataCnt++] = pc.getc();
00045 
00046             // If the transmit buffer is full
00047             if ( txDataCnt >= sizeof( txData ) ) {
00048 
00049                 // Send the transmitbuffer via the nRF24L01+
00050                 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
00051 
00052                 txDataCnt = 0;
00053             }
00054 
00055             // Toggle LED1 (to help debug Host -> nRF24L01+ communication)
00056             myled1 = !myled1;
00057         }
00058 
00059         // If we've received anything in the nRF24L01+...
00060         if ( my_nrf24l01p.readable() ) {
00061 
00062             // ...read the data into the receive buffer
00063             rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
00064 
00065             // Display the receive buffer contents via the host serial link
00066             for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
00067 
00068                 pc.putc( rxData[i] );
00069             }
00070 
00071             // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
00072             myled2 = !myled2;
00073         }
00074     }
00075 }
nRF24L01+ Signal Namembed pin
VccVU*
GndGnd
MOSIp5
MISOp6
SCKp7
CSNp8
CEp9
IRQp10

(* Note: the SparkFun board has an onboard voltage regulator, so the nRF24L01+ Module's Vcc must come from either the Vu pin if the mBed is powered from the USB bus, or from Vin if powered from an external power supply, which MUST NOT EXCEED 7V).

API

Import library

Public Member Functions

  nRF24L01P (PinName mosi, PinName miso, PinName sck, PinName csn, PinName ce, PinName irq=NC)
  Constructor.
void  setRfFrequency (int frequency=DEFAULT_NRF24L01P_RF_FREQUENCY)
  Set the RF frequency.
int  getRfFrequency (void)
  Get the RF frequency.
void  setRfOutputPower (int power=DEFAULT_NRF24L01P_TX_PWR)
  Set the RF output power.
int  getRfOutputPower (void)
  Get the RF output power.
void  setAirDataRate (int rate=DEFAULT_NRF24L01P_DATARATE)
  Set the Air data rate.
int  getAirDataRate (void)
  Get the Air data rate.
void  setCrcWidth (int width=DEFAULT_NRF24L01P_CRC)
  Set the CRC width.
int  getCrcWidth (void)
  Get the CRC width.
void  setRxAddress (unsigned long long address=DEFAULT_NRF24L01P_ADDRESS, int width=DEFAULT_NRF24L01P_ADDRESS_WIDTH, int pipe=NRF24L01P_PIPE_P0)
  Set the Receive address.
void  setTxAddress (unsigned long long address=DEFAULT_NRF24L01P_ADDRESS, int width=DEFAULT_NRF24L01P_ADDRESS_WIDTH)
  Set the Transmit address.
unsigned long long  getRxAddress (int pipe=NRF24L01P_PIPE_P0)
  Get the Receive address.
unsigned long long  getTxAddress (void)
  Get the Transmit address.
void  setTransferSize (int size=DEFAULT_NRF24L01P_TRANSFER_SIZE, int pipe=NRF24L01P_PIPE_P0)
  Set the transfer size.
int  getTransferSize (int pipe=NRF24L01P_PIPE_P0)
  Get the transfer size.
bool  getRPD (void)
  Get the RPD (Received Power Detector) state.
void  setReceiveMode (void)
  Put the nRF24L01+ into Receive mode.
void  setTransmitMode (void)
  Put the nRF24L01+ into Transmit mode.
void  powerUp (void)
  Power up the nRF24L01+ into Standby mode.
void  powerDown (void)
  Power down the nRF24L01+ into Power Down mode.
void  enable (void)
  Enable the nRF24L01+ to Receive or Transmit (using the CE pin)
void  disable (void)
  Disable the nRF24L01+ to Receive or Transmit (using the CE pin)
int  write (int pipe, char *data, int count)
  Transmit data.
int  read (int pipe, char *data, int count)
  Receive data.
bool  readable (int pipe=NRF24L01P_PIPE_P0)
  Determine if there is data available to read.
void  disableAllRxPipes (void)
  Disable all receive pipes.
void  disableAutoAcknowledge (void)
  Disable AutoAcknowledge function.
void  enableAutoAcknowledge (int pipe=NRF24L01P_PIPE_P0)
  Enable AutoAcknowledge function.
void  disableAutoRetransmit (void)
  Disable AutoRetransmit function.
void  enableAutoRetransmit (int delay, int count)
  Enable AutoRetransmit function.

Library

Import librarynRF24L01P

Interface library for the nRF24L01+ chip (specifically the Transceiver nRF24L01+ Module with Chip Antenna from SparkFun, talking to their Nordic Serial Interface Board, but should work with any nRF24L01+).

Reference




1 related question:


4 comments:

21 Jan 2011

Thank you very much..very useful code..great job. Do you have code for CC2500 ?

06 Jun 2012

can this be used to sync the time of a clock?

17 Sep 2012

Hello Mr. Edwards

I'm tying to communicate mbed with nRF24L01+ using your code. When I use only one chip I can only transmit, but not receiving. Then I plug one more chip (to another SPI interface and change the code correspondingly), set the first chip to transmit, the second to receive only. Both tranmission and receiving work fine.

Am I doing something wrong or it is not possible to launch your code on only one wireless chip?

21 Nov 2013

The function "enableAutoRetransmit (int delay, int count)" is not implemented? I can't use that. Someone can help me?