How to use an nrf24l01 (rf24) with an Arduino Ethernet Shield

rf24Ethernet
I have been working on a project that requires the use of both a wiznet-type w5100 Ethernet shield like this and an nrf24l01 radio at the same time.  The problem I ran into was that both of these peripherals use the SPI protocol, and I could not get them both working simultaneously (the Ethernet shield has it’s pins hardwired to the hardware SPI, of course).  After doing some reading on the SPI protocol, I found out that there seems to be some kind of issue that, according to the freetronics website, causes the w5100 chip to maintain control of the SPI bus even when another device has been selected.

After much searching, and much experimenting, I finally found a workaround that seems to be working reliably for me.  The solution was to use ‘software’ or ‘bit-banged’ SPI.  This allows the Ethernet shield to happily commandeer the hardware SPI pins, while the nrf24l01 (or any other peripheral, I suppose) to operate on a separate, emulated SPI bus.

The credit for this goes to a user on the Arduino forums, nicoverduin, who suggested using some software SPI libraries already available and modifying maniacbug’s nrf24 library to use the software spi interface.

Here is the step by step:

1.)  Download the modified maniacbug library and softSPI libraries here.
2.) Copy the libraries to your Arduino libraries folder.
3.) Configure your Arduino/Ethernet-shield/nrf24l01 like this (you can also modify the pin configuration if needed.  Instructions are in the readme file on the linked github repository):

 

Connect to the soft-spi designated pins
Connect to the soft-spi designated pins

 

If you would like to download the fritzing file, here you go:
rf24-softSPI_plusEthernetShield

4.) Add the library references to your code or use example code provided below.

This sketch is a companion sketch to the nrf24l01_send_string sketch I posted previously.

Once you have completed the above steps, you can upload your sketch to the Arduino and you should be able to browse to the IP of your Arduino with Ethernet shield and you will see a web page that tells you the last message received over the nrf24l01.

There is one major caveat to all of this, however.  I have found that sometimes when I am working with the softSPI version of the nrf24 library and then I switch back to the regular/hardware version (e.g. when I work on the sending device, which in this case has no ethernet shield, after I upload a sketch to the one with the ethernet shield), the hardware-based SPI nrf24l01 breaks.  This seems to be happening because the Arduino IDE is holding on to the softSPI version of the rf24 library.  I am just speculating on that, but it kind of makes sense since the internal naming is identical between the libraries.

The fix for this problem is simple, if a bit inconvenient.  I have been removing my modified version of the rf24 library from the Arduino libraries folder before working with the hardware-SPI version.  I am sure there is a smarter way of fixing this, and if someone could point it out to me, I would be glad to post the solution here.

Another NRF24L01 Sketch – string send/receive

In a previous post I described a very simple setup to send and receive data using the NRF24L01 breakouts that can be purchased (very!) inexpensively on eBay.  I wanted to show another sketch, using basically the same physical setup, to send and receive text from one arduino to another.  Forgive the mostly copy-and-paste nature of this post, if you are just looking for the code, scroll to the bottom.

You will need (hardware):
(2) nrf24L01 2.4ghz wireless transcievers
(2) Arduino Uno (or compatible)
Recommended: male-female jumpers for connecting nrf24L01 modules to Arduino

You will need (software):
Arduino IDE
RF24 libraries by maniacbug (https://github.com/maniacbug/RF24)

The image below shows the view of the nrf24L01 from the top.  Note: the pins are on the bottom-side.  The image shows the top-side.

This is what the breakout looks like from the top.
This is what the breakout looks like from the top.

For this sketch, both arduino boards will need to be setup like this:

fritzing drawing of nrf24l01 connections

fritzing drawing of nrf24l01 connections
closeup of nrf24l01 to arduino connections
closeup of nrf24l01 to arduino connections

Upload the send and receive sketches to corresponding Arduinos physically configured as above.  The sending unit will take a string and break it down into individual characters, then send each character until the end of the string is reached.  After that, it sends a ‘termination message’, in this case, the integer ‘2’.  This tells the receiving end that the message is complete.  The sending unit then powers down it’s radio for one second, then powers it back up and runs through the loop again.

You will see some comments in the code about this delay period causing some occasional lost data when sending.  I’m hoping someone with more experience with the nrf24 radios can comment on this and provide some code improvements, but this method seems to work well for the most part.  I have added an alternative receive-side sketch below that checks for a proper message length as a sort of ‘checksum’ function.

On the receiving end, the sketch begins listening for available messages.  Once it begins receiving characters, it begins appending them to the receive string.  It continues to append characters until it receives the termination character (again, the integer ‘2’ in this case, but you could set it to something else).  It then prints the complete string to serial.

There is a second version of the receive sketch (receive_string_withChecksum) that also checks the final string length against an expected message length.  If the actual received message length does not match the expected length, the string is rejected and the radio begins listening for a new message.  Obviously, this will only work if you expect a message of a certain fixed size every time.

 

Simple nrf24L01 with Arduino Sketch and Setup

nrf24L01

I have been working with the 2.4Ghz nrf24L01 modules on a few projects. These modules are very inexpensive (about US $1.25 ea. when bought in quantities of 2 or more on eBay), and they seem to work well for short range, small payload data transmittal. This walk-through will show you how to wire them up, and then will demonstrate a pair of very basic sketches (one for the sender, and one for the receiver) used to verify that data is being sent wirelessly.

You will need (hardware):
(2) nrf24L01 2.4ghz wireless transcievers
(2) Arduino Uno (or compatible)
(2) LEDs: Red and Green (or whatever you have)
Recommended: male-female jumpers for connecting nrf24L01 modules to Arduino

You will need (software):
Arduino IDE
RF24 libraries by maniacbug (https://github.com/maniacbug/RF24)

The image below shows the view of the nrf24L01 from the top.  Note: the pins are on the bottom-side.  The image shows the top-side.

nrf24l01_draw

This drawing shows the basic connections from nrf24L01 to Arduino.  For this sketch, the only difference between the sender and the receiver will be that two LEDs will need to be added on pins 3 and 5 (or whichever you choose, really).
nrfFritzingBasic

closeup of nrf24l01 to arduino connections
closeup of nrf24l01 to arduino connections
The send and receive Arduino setups.  Pay no attention to the LEDs without resistors.
The send and receive Arduino setups. Pay no attention to the LEDs without resistors.

Once you have the sender and receiver wired up, you will need to upload sketches to each.  All the sender sketch does is send values 0 to 255 to the receiver, repeatedly.

On the receiver side, the sketch just reads the sent data and compares it to the last value received.  If the value received is equal to the last value plus one, the green LED will light.  If it is not, the red LED will light.  This works pretty well to visually display how reliable the signal is.  If you see lots of red, you know you are dropping packets.  Note, though, that this sketch will blink the red LED once each cycle to indicate that a complete 0-255 cycle has taken place.

Again, the idea behind these two simple sketches is to get down to the most basic possible setup for use with nrf24L01.  I hope this helps someone trying out the nrf24L01 transceivers for the first time.  The video embedded below shows the behavior of the receiver when it experiences lost wireless packets.

Sketch for the sender:Sketch for the receiver: