I was having some trouble with NRF communication. I followed this tutorial to set up a demo transmitter and receiver using two NRF modules.
// SimpleTx - the master or the transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const byte slaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char dataToSend[10] = "Message 0";
char txNum = '0';
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();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3,5); // delay, count
radio.openWritingPipe(slaveAddress);
}
//====================
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
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");
updateMessage();
}
else {
Serial.println(" Tx failed");
}
}
//================
void updateMessage() {
// so you can see that new data is being sent
txNum += 1;
if (txNum > '9') {
txNum = '0';
}
dataToSend[8] = txNum;
}
I used the code given is post #2 of the tutorial for the transmitter and the following code of the receiver.
// SimpleRx - the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN);
char dataReceived[10]; // this must match dataToSend in the TX
bool newData = false;
//===========
void setup() {
Serial.begin(9600);
Serial.println("SimpleRx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
}
//=============
void loop() {
getData();
showData();
}
//==============
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
newData = true;
}
}
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.println(dataReceived);
newData = false;
}
}
The transmitter seemed to be working fine printing in the serial monitor:
13:05:22.966 -> Data Sent Message 3 SimpleTx Starting
13:05:25.437 -> Data Sent Message 0 Acknowledge received
13:05:26.436 -> Data Sent Message 1 Acknowledge received
13:05:27.434 -> Data Sent Message 2 Acknowledge received
13:05:28.432 -> Data Sent Message 3 Acknowledge received
13:05:29.432 -> Data Sent Message 4 Acknowledge received
13:05:30.432 -> Data Sent Message 5 Acknowledge received
13:05:31.427 -> Data Sent Message 6 Acknowledge received
The number is being printed incrementing it by 1 after each iteration, every second after sending it successfully(radio.write() returns a true) which is what it is supposed to do. However, the problem seems to be on the receiver side:
13:06:20.318 -> Data received⸮SimpleRx Starting
13:06:21.798 -> Data received
13:06:21.831 -> Data received
13:06:21.831 -> Data received
13:06:21.864 -> Data received
It prints data received rapidly. Following the tutorial
the output of radio.printDetails() is
SPI Speedz = 10 Mhz
STATUS = 0x00 RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=0 TX_FULL=0
RX_ADDR_P0-1 = 0x0000000000 0x0000000000
RX_ADDR_P2-5 = 0x00 0x00 0x00 0x00
TX_ADDR = 0x0000000000
RX_PW_P0-6 = 0x00 0x00 0x00 0x00 0x00 0x00
EN_AA = 0x00
EN_RXADDR = 0x00
RF_CH = 0x00
RF_SETUP = 0x00
CONFIG = 0x00
DYNPD/FEATURE = 0x00 0x00
Data Rate = 1 MBPS
Model = nRF24L01+
CRC Length = Disabled
PA Power = PA_MIN
ARC = 0
Yes, it has all 0x00 and the arduino is not properly communicating with the NRF. I tried powering the NRF module with 2 alkaline AA batteries(1.5 V each). The transmitter works as before, but no luck with the receiver. I even connected a 10 μF capacitor between the power supply to the NRF but no luck. I changed the NRF module for the receiver but the "data received" flooding doesn't change.
P.S: I am using a clone of Arduino Nano, but it seem to be working fine with other applications. The NRF24L01 module has "NRF24L01" printed on its chip.