Here is tested example code to send and receive one int number. It is adapted from the examples in Robin2's simple rf24 demo tutorial. Read and follow the tutorial and you have a good chance to get the radios to work. Especially pay attention to how to properly power the radio modules.
Sender:
// SimpleTx - the master or the transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const byte CE_PIN = 9;
const byte CSN_PIN = 10;
const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
int dataToSend = 123;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
void setup()
{
Serial.begin(9600);
Serial.println("SimpleTx Starting");
radio.begin();
//RF24_1MBPS, RF24_2MBPS, RF24_250KBPS
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3, 5); // delay, count
//radio.setChannel(10);
//RF24_PA_MIN = 0,RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(slaveAddress);
}
//====================
void loop()
{
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis)
{
dataToSend++;
send();
prevMillis = millis();
}
}
//====================
void send()
{
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
// Always use sizeof() as it gives the size as the number of bytes.
// For example if dataToSend was an int sizeof() would correctly return 2
Serial.print("Data Sent ");
Serial.print(dataToSend);
if (rslt)
{
Serial.println(" Acknowledge received");
}
else
{
Serial.println(" Tx failed");
}
}
Receiver:
// SimpleRx - the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const byte CE_PIN = 9;
const byte CSN_PIN = 10;
const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN);
int recvData;
bool newData = false;
//===========
void setup()
{
Serial.begin(9600);
Serial.println("SimpleRx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3, 5); // delay, count
//radio.setChannel(10);
radio.setPALevel(RF24_PA_LOW);
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
}
//=============
void loop()
{
getData();
showData();
}
//==============
void getData()
{
if ( radio.available() )
{
radio.read( &recvData, sizeof(recvData) );
newData = true;
}
}
void showData()
{
if (newData == true)
{
Serial.print("Data received >> ");
Serial.print("number = ");
Serial.println(recvData);
newData = false;
}
}