I’ve just received a pair of nRF24L01+ radio modules for my Arduino and have been learning how to use the RF24 library.

Probably the hardest part was getting the ATmega328P running at 3.3V/8MHz on the breadboard. Funny shenanegans went on that meant I had to burn the bootloader using ArduinoISP with a 16MHz crystal and pair of 22pF caps, then remove them and upload the sketch using my CP2102.

Anyway, after figuring out that “RF24 radio(9,10);” referred to the CE and CSN pins respectively on the module being wired to digital 9 and 10 on the Arduino, things started working.

I found that the pingpair example relied on two serial monitors being open for the sketches to actually run, so I rewrote it as a couple of sketches – a transmitter and receiver, that only uses a serial monitor on the receiver and no weird printf() overloading or T/R keypresses, this way we can just plug the TX into a power supply and the RX into a computer.

BTW, “screen /dev/ttyACM0 57600” works nicer than the IDE’s serial monitor, then just ctrl-a,k to exit.

The TX script just counts rather than sending the time, as I wanted to be able to easily see that it was incrementing by one every second.

rf24ping_tx.ino

#include <SPI.h>
#include <RF24.h>

// ce,csn pins
RF24 radio(9,10);

// init counter
unsigned long count = 0;

void setup(void)
{
    radio.begin();
    radio.setPALevel(RF24_PA_LOW);
    radio.setChannel(0x4c);

    // open pipe for writing
    radio.openWritingPipe(0xF0F0F0F0E1LL);

    radio.enableDynamicPayloads();
    radio.setAutoAck(true);
    radio.powerUp();
}

void loop(void)
{
    // print and increment the counter
    radio.write(&count, sizeof(unsigned long));
    count++;

    // pause a second
    delay(1000);
}

rf24ping_rx.ino

#include <SPI.h>
#include <RF24.h>

// ce,csn pins
RF24 radio(9,10);

void setup(void)
{
    // init serial monitor and radio
    Serial.begin(57600);
    radio.begin();

    radio.setPALevel(RF24_PA_LOW);
    radio.setChannel(0x4c);

    // open pipe for reading
    radio.openReadingPipe(1,0xF0F0F0F0E1LL);

    radio.enableDynamicPayloads();
    radio.setAutoAck(true);
    radio.powerUp();
    radio.startListening();
}

void loop(void)
{
    // if there is data ready
    if (radio.available())
    {
        // dump the payloads until we've got everything
        unsigned long count;
        bool done = false;
        while (!done)
        {
            // fetch the payload, and see if this was the last one
            done = radio.read(&count, sizeof(unsigned long));
        }

        // print the payload
        Serial.println(count);
    }
}