I'm new to wireless hardware, and have been having issues with the nRF24l01. After a few hours of troubleshooting, I've consistently found that the write() function has always returned false and the receiver has always received nothing or noise (right now it's nothing). I have yet to find a reason for why these issues are occurring.
Here are the things I've been keeping an eye out for:
Power/Voltage: The Transmitter is an Arduino Uno, powered via USB, which is sending 3.3v to the nRF. The Receiver is an Arduino Nano Every, powered via 5v Power Supply, and the nRF is being powered by its own 3.3v supply, with a decoupling capacitor to reduce noise.
Pins: All SPI and digital pins are in the correct ports.
Distance: nRFs are far enough apart that the signal should not be overwhelming.
Code: Code is taken from this post. Below is what I've been importing into the Arduinos. I believe the only change I've made is which ports are the CE and CSNs.
Hardware: Swapped antenna and had the same issue, so it's unlikely that they are broken.
Right now, the Transmitter simply repeats "Data Sent Message 0 Tx failed", and the receiver prints "SimpleRx Starting" and then nothing.
Transmitter Code
#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;
}
Receiver Code
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 5
#define CSN_PIN 8
#define led 2 //for debugging only
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);
pinMode(led, OUTPUT);
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;
}
}
Your problem is most likely lack of power. The Uno 3.3V is marginal. Sometimes the addition of a 10uF cap across the radio module power input can help, but the high power radios take more current so that is no guarantee. I do not know how good the Nano Every 3.3V is, but on the classic Nano the 3.3V is not up to the task at all.
I use homemade adapters like these to supply a solid 3.3V to the radio modules.
The first thing to do here would probably be to identify where the problem is occurring.
I would suggest running the gettingstarted example that comes with the RF24 library, but you could also modify your example to do the same thing.
A: If radio.begin() returns false, there is a SPI communication issue with the radio. You will get a message "radio hardware is not responding!!" with the gettingstarted example. Double and triple check wiring and hardware to resolve this issue. B: If radio.begin returns true, but communication is still failing, try calling radio.setPALevel(RF24_PA_MIN,0); to help rule out power supply issues.
Well your code is fine, the radios are responding to SPI commands, just not working. I would start thinking about hardware issues with the radios themselves. It could be other things though.
What do you get if you do the following?:
Hmm, that still indicates you are getting bad SPI communication between the radio and the Nano Every. The two devices are not 'talking' together correctly. The output should look very similar to the TX device which appears to be functional. The PA power is showing incorrect because of this, along with other settings.
I don't have any more suggestions other than to shorten the wires and verify you have your pins correct. It seems to be a hardware issue of some sort.
Swapped some wires and the Nano, and I think the SPI communication is working now. Unfortunately, there's still no communication happening. Here's what the details look like now.
OK looking good, that's a big step in the right direction!
You've got your main SPI pins correct, the next thing to verify is that you have the CE and CS pins correct as well. They need to be correct to transmit/receive.
There shouldn't be much else that can be wrong now besides software problems, so as long as you are using working code, like the gettingstarted examples or the initial code you posted, it should start working.
Is it possible that the radios are broken without SPI communication being disrupted? I've verified that every pin is in the right spot, and I've tried both the example code I showed earlier, as well as the GettingStarted file. Every time the write() function is called, the reciever gets nothing.
While I've been swapping the radios out to check if it's a hardware issue, I only have 3. It's not too unlikely that more than 1 is broken.
Yes, usually at this point things start working. If you got all the radios from the same place, there is a chance they are faulty. The easiest way to know is to test with some known working modules to verify.