rf communication issue with NRF24L01 and UNO programme

Hi Guys

I am new to Arduino’s but am keen to learn. I have a NRF24L01 RF module coupled with a 3.3v adaptor board plugged into a Uno and another for the receiver. Image below

My problem is that for some reason it is not printing “hello world” when it receives a signal from the transmitter, it actually prints a blank line and jumps down to print “blank”. I know that is receiving a signal as it is printing “blank” which I think it should only do if it gets a signal. Does anyone know what is wrong with this Sketch. As a second point it seems pretty sporadic when it prints or receives a signal is this normal or is there something else that I could improve.

Receiver sketch
/*

  • Arduino Wireless Communication Tutorial
  • Example 1 - Receiver Code
  • by Dejan Nedelkovski, www.HowToMechatronics.com
  • Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
    */
    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>
    RF24 radio(7, 8); // CE, CSN
    const byte address[6] = “00001”;
    void setup() {
    Serial.begin(9600);
    radio.begin();
    radio.openReadingPipe(0, address);
    radio.setPALevel(RF24_PA_MIN);
    radio.startListening();
    }
    void loop() {

if (radio.available()) {
char text[11] = “”;
radio.read(&text, sizeof(text));
Serial.println(text);
Serial.println(“blank”);
}
}

Transmitter Sketch

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = “00001”;
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
const char text = “1”;
radio.write(&text, sizeof(text));
delay(10);
}

You need to send and receive the exact same size of data. You seem to be sending 2 characters and expecting to receive 11.

Have a look at this Simple nRF24L01+ Tutorial

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

…R

Hi robin thanks for your feedback. I will have a look at the web page. What I have found is it doesn't seem to make any difference as I started with "hello world" which is exactly 11 characters although maybe I am missing something as you mentioned that this is actually 2 characters currently.

Robin2:
You need to send and receive the exact same size of data. You seem to be sending 2 characters and expecting to receive 11.

IIRC the setting of the driver is 32 byte fixed size unless you use setPayloadSize or enableDynamicPayloads.

void RF24::setPayloadSize( uint8_t size )

Set Static Payload Size

This implementation uses a pre-stablished fixed payload size for all transmissions. If this method is never called, the driver will always transmit the maximum payload size (32 bytes), no matter how much was sent to write().

Parameters
size The number of bytes in the payload

Thanks Whandall

It was originally set to 32 but it still does not print.

timwoodhouse:
It was originally set to 32 but it still does not print.

Have you tried the examples in my Tutorial? - they do work.

...R

HI Robin2

Great tutorial thanks for highlighting it. I have tried to compile the simple tx.ino sketch on my Uno and I get the following error message. Sorry I know it must be something obvious as no one else has had the issue. I have looked up this function on google but cant find a reference to it.

'send' was not declared in this scope. Could you explain what I need to do.

To help with my learning could you please explain what this whole section is doing please.

currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
prevMillis = millis();
}

timwoodhouse:
'send' was not declared in this scope. Could you explain what I need to do.

Post the complete program that YOU uploaded to your Arduino. It seems likely that you made a mistake in copying the program.

...R

here it is

// 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();
}
}

//====================

Why did you copy only the first half of the tutorial sketch?

Yes sorry it wouldn't let me post back but when I went back I realised that I had only copied half of the sketch. Sorry school boy error.
Thanks a lot for your help

The adapters kept my radios from communicating. I removed the adapters and hooked up directly.

Also read the return value from radio.begin if it is a zero then your not talking to it. If it's a one then you are.

its interesting I an getting the message “dataReceived” on the screen at much more than second intervals and when I disconnect the transmitter it continues adding the message.

How do I read the return value to see if they are communicating and I thought that the above message should not appear if they are not communicating?

timwoodhouse:
I an getting the message "dataReceived" on the screen at much more than second intervals and when I disconnect the transmitter it continues adding the message.

That points to a communication problem with the NRF (bad wiring, bad wires, bad module, ...).

timwoodhouse:
How do I read the return value to see if they are communicating and I thought that the above message should not appear if they are not communicating?

if (!radio.begin()) {
  Serial.println(F("radio.begin() failed"));
}

timwoodhouse:
its interesting I an getting the message "dataReceived" on the screen at much more than second intervals and when I disconnect the transmitter it continues adding the message.

How do I read the return value to see if they are communicating

If that happens you can be pretty certain the nRF24s are NOT communicating.

As @Whandall has said, you should be looking for a hardware problem between the Arduino and its nRF24

...R

HI Whandall

Could you explain exactly how the line you wrote works. I have added a delay in so I can see what it is presenting and it prints

SimpleRx Starting
radio.begin() failed //then waits for the delay then writes the following at high speed.
Data received
Data received
Data received

I have then removed the voltage adapter plate and connected both RF boards directly to the UNO as the tutorial demonstrates and double checked the wiring. I have not added the capacitor as I am not 100% sure what is meant by connect 10uF between vcc and gnd and how this should be connected. Robin2 mentioned that he is also running the Uno without any issues

I then only get
SimpleRx Starting

Which is why I am trying to understand exactly what your lines are doing and what might be going on. I have added the sketch also below.

// 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();

if (!radio.begin()) {
Serial.println(F(“radio.begin() failed”));
delay(10000);
}
}

//=============

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;
}
}

timwoodhouse:
Could you explain exactly how the line you wrote works.

It only shows that you have a communication error.

Robin2:
If that happens you can be pretty certain the nRF24s are NOT communicating.

you should be looking for a hardware problem between the Arduino and its nRF24

timwoodhouse:
Could you explain exactly how the line you wrote works. I have added a delay in so I can see what it is presenting and it prints

Adding a delay() into the receiving program is not a good idea. It should be listening all the time.

...R

substitute below for your radio.begin() statement

if (radio.begin() == 0)
   Serial.println("Not communicating")

noweare:
substitute below for your radio.begin() statement

if (radio.begin() == 0)

Serial.println("Not communicating")

That does not make any sense.

There is never any communication between a pair of nRF24 devices until a message is actually sent with radio.write() - and then the communication only lasts for the brief period necessary to send the message and receive the acknowledgement.

...R