Sunday, December 13, 2009

Using the Nordic nRF24L01+ 2.4GHz transceiver with Adruino

I picked up a Nordic nRF24L01+ breakout board and keychain remote FOB from Sparkfun.com a while ago to see about using it as part of a Aging In Place electronics project. Finally got around to experimenting with the pair this weekend. It was a cathartic break from working a plumbing problem.

I could not find much info on using it with the Arduino platform. To get my feet wet I just moved an example program written for AVR PIC to Arduino's language. I have posted it below.

The Nordic communication chips seem to be a good product for low power, long life remote control. The questions of range and clashes with other products in the 2.4GHz bands are still open.

The steps to talk with the Nordic chips are very straight forward. It uses the SPI communications for interfacing and has a simple interrupt architecture for status [the code below does not use the interrupt]. The Sparkfun breakout board is a nice starting point as it allows hookup to either 5 volt or 3.3 volts products by providing onboard voltage for the Nordic. And the Nordic chips are natively able to talk 5 or 3.3 volt.

I will compare some of these functions and price points with some of the other similar products out there. The Texas Instruments wireless products are one set that I want to compare.




// nfr2401_02
// 13-December-2009
//
// learning how to communicate with a Nordic nfr2401 wireless communications module.
// this program will set up a nfr2401 as a receiver and receive data from the key presses on a Sparkfun Nordic nfr2401 keyfob.
//
// parts:
// Transceiver nRF24L01+ Module with Chip Antenna Sparkfun sku: WRL-00691
// Nordic FOB Sparkfun sku: WRL-08602
// Arduino Deiecimila
//

// based on Nordic-FOB-Tester-v10.c written by Nathan Seidle at Sparkfun.com in 6-19-2007
// based on article and code 'Interfacing a Serial EEPROM Using SPI' at Arduino.cc by Heather Dewey-Hagborg

// SPI registers defined in basic Arduino:
// SPCR SPI Control Register
// SPDR SPI Data Register
// SPSR SPI Status Register

/* Arduino SPI register info
Data registers simply hold bytes.
For example, the SPI data register (SPDR) holds the byte which is about
to be shifted out the MOSI line,
and the data which has just been shifted in the MISO line.

Status registers change their state based on various microcontroller conditions.
For example, the seventh bit of the SPI status register (SPSR) gets set to 1
when a value is shifted in or out of the SPI.

The SPI control register (SPCR) has 8 bits, each of which control a particular SPI setting.

SPCR
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| SPIE | SPE | DORD | MSTR | CPOL | CPHA | SPR1 | SPR0 |

SPIE - Enables the SPI interrupt when 1
SPE - Enables the SPI when 1
DORD - Sends data least Significant Bit First when 1, most Significant Bit first when 0
MSTR - Sets the Arduino in master mode when 1, slave mode when 0
CPOL - Sets the data clock to be idle when high if set to 1, idle when low if set to 0
CPHA - Samples data on the falling edge of the data clock when 1, rising edge when 0
SPR1 and SPR0 - Sets the SPI speed, 00 is fastest (4MHz) 11 is slowest (250KHz)

*/

#define DATAOUT 11 //MOSI
#define DATAIN 12 //MISO
#define SPICLOCK 13 //SCK
#define CHIPSELECT 10 //CS, also called SS slaveselect

#define NFR_CE 9 // nRF2401 sets the chip to RX or TX mode
#define NFR_IRQ 8 // active low interrupt from nRF2401

byte clr; // temp to hold discardable SPI return data

byte data_array[4]; // holds data in packet received from Key FOB

byte incoming; // holds status register of nfr2401

int button_presses; // holds count of number of times a button had been pressed on FOB [since battery replaced]


// routine to send and receive a byte on SPI

char spi_transfer(volatile char data)
{

SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};

return SPDR; // return the received byte
}

// main arduino setup routine

void setup()
{
Serial.begin(9600);

Serial.println("Configuring the nRF2401.");

pinMode( DATAOUT, OUTPUT);
pinMode( DATAIN, INPUT);
pinMode( SPICLOCK, OUTPUT);
pinMode( CHIPSELECT, OUTPUT);

pinMode( NFR_CE, OUTPUT);
pinMode( NFR_IRQ, INPUT);


//interrupt enabled,spi enabled,msb 1st,master,clk low when idle,
//sample on leading edge of clk,system clock/4 rate (fastest)

SPCR = (1<<SPE)|(1<<MSTR);
clr=SPSR;
clr=SPDR;
delay(10);


digitalWrite(CHIPSELECT, HIGH); //disable SPI comm with device
digitalWrite(NFR_CE, LOW); //chip disable the nRF2401


digitalWrite(CHIPSELECT, LOW); //enable comm with SPI device
spi_transfer(0x20); //enable RX irq, CRC enabled, be a receiver
spi_transfer(0x39);
digitalWrite(CHIPSELECT, HIGH); //disable SPI comm with device

digitalWrite(CHIPSELECT, LOW); //enable comm with SPI device
spi_transfer(0x21); // disable auto-acknowledge
spi_transfer(0x00);
digitalWrite(CHIPSELECT, HIGH); //disable SPI comm with device

digitalWrite(CHIPSELECT, LOW); //enable comm with SPI device
spi_transfer(0x23); // set address width to 5 bytes (default, not really needed)
spi_transfer(0x03);
digitalWrite(CHIPSELECT, HIGH); //disable SPI comm with device

digitalWrite(CHIPSELECT, LOW); //enable comm with SPI device
spi_transfer(0x26); //air data rate 1 Mbit, 0dBm, setup LNA
spi_transfer(0x07);
digitalWrite(CHIPSELECT, HIGH); //disable comm with SPI device

digitalWrite(CHIPSELECT, LOW); //enable comm with SPI device
spi_transfer(0x31); // 4 byte receive payload
spi_transfer(0x04);
digitalWrite(CHIPSELECT, HIGH); //disable comm with SPI device

digitalWrite(CHIPSELECT, LOW); //enable comm with SPI device
spi_transfer(0x25); // rf channel 2 (default, not really needed)
spi_transfer(0x02);
digitalWrite(CHIPSELECT, HIGH); //disable comm with SPI device

digitalWrite(CHIPSELECT, LOW); //enable comm with SPI device
spi_transfer(0x2a); //set RX pipe 0 address
spi_transfer(0xe7);
spi_transfer(0xe7);
spi_transfer(0xe7);
spi_transfer(0xe7);
digitalWrite(CHIPSELECT, HIGH); //disable comm with SPI device

digitalWrite(CHIPSELECT, LOW); //enable comm with SPI device
spi_transfer(0x20); // RX interrupt, power up, be a receiver
spi_transfer(0x3b);
digitalWrite(CHIPSELECT, HIGH); //disable comm with SPI device

Serial.println("Done setting up.");
digitalWrite(NFR_CE, HIGH); //receiver enable the nRF2401

}

// main arduino program loop

void loop()
{
Serial.println("Startup up communications with nRF2401.");
Serial.println("Waiting for message from Nordic Key FOB.");

// loop forever
while(1)
{
digitalWrite(CHIPSELECT, LOW); //enable comm with SPI device
incoming = spi_transfer(0xff); // get nRF2401 status register contents
digitalWrite(CHIPSELECT, HIGH); //disable comm with SPI device
// transmission received from FOB
if (incoming & 0x40)
{

// get data from nRF2401
digitalWrite(NFR_CE, LOW); //disable receiver in the nRF2401
digitalWrite(CHIPSELECT, LOW); //enable comm with SPI device
spi_transfer(0x61);
data_array[0] = spi_transfer(0xff);
data_array[1] = spi_transfer(0xff);
data_array[2] = spi_transfer(0xff);
data_array[3] = spi_transfer(0xff);
digitalWrite(CHIPSELECT, HIGH); //disable comm with SPI device

digitalWrite(CHIPSELECT, LOW); //enable comm with SPI device
spi_transfer(0xe2); // flush RX FIFO
digitalWrite(CHIPSELECT, HIGH); //disable comm with SPI device

digitalWrite(CHIPSELECT, LOW); //enable comm with SPI device
spi_transfer(0x27); // clear RF FIFO interrupt
spi_transfer(0x40);
digitalWrite(CHIPSELECT, HIGH); //disable comm with SPI device
digitalWrite(NFR_CE, HIGH); //receiver enable the nRF2401

// figure out which key on FOB was pressed

switch( data_array[0] )
{
case 0x17: Serial.print("Left button "); break;
case 0x1e: Serial.print("Bottom button"); break;
case 0x1b: Serial.print("Right button "); break;
case 0x1d: Serial.print("Top button "); break;
case 0x0f: Serial.print("Center button"); break;
default: Serial.print( "No button! "); break;
}
// display the total number of times that a button had been pressed on the FOB
// since battery in FOB replaced
button_presses = (data_array[1] << 8) + data_array[2];
Serial.print(" pressed ");
Serial.print( button_presses, DEC );
Serial.println(" times.");

// wait a bit before checking for another key press packet, may need some adjusting.
delay(10);
}
}
}







2 comments:

  1. THANK YOU SO MUCH!!! I have been looking for some sort of information concerning these three components. This code breaks it all down so very clearly. Again, thank you for posting.

    ~k

    ReplyDelete
  2. This is very helpful. Thanks a lot !

    ReplyDelete