Simple Arduino example adapted from here to transmit numbers between two RF components (model number nRF24L01+ 2.4GHz). The rx Arduino prints out received numbers.
2 x nRF24L01 RF components
2 x Arduino Unos
jumper wires
Both the TX and RX have the same setup from here.
To be clear, connect the following:
There are two sketches that follow:
for the transmitter (TX)
for the receiver (RX)
Both rely on the RF24 library. To install it, follow the following instructions:
Quit the Arduino application, if it is open.
Download this ZIP file with the library.
Unzip the contents.
Rename the resulting directory from RF24-master to RF-24.
Move the RF-24 directory into your ~/Documents/Arduino/libraries directory.
Open the Arduino application.
Verify that the library was successfully installed:
Go to the File -> Examples menu and checking for a RF24 menu item.
Within it, open the GettingStarted sketch.
Press the checkmark button at the upper left corner of the sketch window to Verify the sketch and check that it prints Done compiling at the bottom.
Transmitter (TX) sketch adapted from this tutorial.
/**
* Simple RF transmitter code for NRF24L01 component.
* Sends the numbers 0 - 255 in a loop.
* Adapted from http://shanes.net/simple-nrf24l01-with-arduino-sketch-and-setup/
* Requires nRF24L01 library from https://github.com/maniacbug/RF24/archive/master.zip
* Other than CF and CS pins, others must be connected to board-specific pins
**/
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#define CE_PIN 9
#define CS_PIN 10
// for the RF component on pins 9 and 10
RF24 radio( CE_PIN, CS_PIN);;
const uint64_t pipe = 0xE8E8F0F0E1LL;
// a message to be sent
int msg;
void setup()
{
Serial.begin(9600);
// initialize the RF component
radio.begin();
radio.openWritingPipe(pipe);
// start message at 0
msg = 0;
}
void loop()
{
// send the message
radio.write(&msg, 1);
// restart messages after 255
if ( msg == 255 )
msg = 0;
// otherwise, increment
else
msg++;
// wait a little before sending another message
delay( 100 );
}
Receiver (RX) sketch adapted from this tutorial.
/**
* Simple RF transmitter code for NRF24L01 component.
* Receives and prints messages.
* Adapted from http://shanes.net/simple-nrf24l01-with-arduino-sketch-and-setup/
* Requires nRF24L01 library from https://github.com/maniacbug/RF24/archive/master.zip
* Other than CF and CS pins, others must be connected to board-specific pins
**/
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#define CE_PIN 9
#define CS_PIN 10
// for the RF component on pins 9 and 10
RF24 radio( CE_PIN, CS_PIN);;
const uint64_t pipe = 0xE8E8F0F0E1LL;
// the message received
int msg;
void setup()
{
Serial.begin(9600);
// initialize the RF component
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
}
void loop()
{
// if there is data to read
if (radio.available())
{
// read the message; if successful, read will return true
bool msgRead = radio.read(&msg, 1);
// if successful
if ( msgRead )
// print out the received message
Serial.println( msg );
// otherwise, error
else
Serial.println( "failed to read message" );
}
}