Sparkfun sells a nifty little gadget to control your project remotely using nRF24L01+ radios. It’s a small key fob with 5 buttons, which transmits codes when you press each button. Today we’re going to explore how to receive those signals using the RF24 library. This is all thanks to Kirk Mower who sent me these units for Christmas. Thanks, Kirk! 🙂
|1||WRL-00691||Transceiver nRF24L01+ Module with Chip Antenna||$19.95|
Connecting the Sparkfun transceiver unit to an Arduino using a breadboard is easier than with the typical modules. Does that make it worth $20 over $4? You’ll have to decide that. To connect it, we can follow the guidance on my previous post, Getting Started with nRF24L01+ on Arduino. Wiring it this way means we can use all the RF24 examples, which are set up to use these pins by default.
This is now an example for the RF24 library, nordic_fob.pde.
Setting up the radio is simply a matter of matching the radio parameters on the receiver with those of the transmitter. We can refer to the device firmware for the transmitter settings.
The payload sent by the transmitter is 4 bytes long. The first is one byte containing the current state of the buttons. The low 5 bits correspond with each button, and the bit is 1 when the button is up and 0 when pressed. Bytes 2 & 3 are a 16-bit sequence number I call the ‘id’. Every time the unit sends a packet, the sequence number is increased by 1. The 4th byte is not used. So this is what our payload looks like:
The main loop simply listens for packets, and dumps out the results, translating the button bits into words so it’s easier to read.
// Receive each packet, dump it out
// if there is data ready
if ( radio.available() )
// Get the packet from the radio
radio.read( &payload, sizeof(payload) );
// Print the ID of this message. Note that the message
// is sent 'big-endian', so we have to flip it.
printf("#%05u Buttons ",flip_endian(payload.id));
// Print the name of each button
int i = num_buttons;
if ( ! ( payload.buttons & _BV(i) ) )
// If no buttons, print None
if ( payload.buttons == _BV(num_buttons) - 1 )