Trouble sending button state using NRF modules

Hello everyone, :slight_smile:

I'm using the nrf24l01 modules with Arduino Nanos and wondering if someone can help me out. I have pretty much copy and pasted code to get 1 way comms for now. PROGRAM SOURCE: https://create.arduino.cc/projecthub/muhammad-aqib/nrf24l01-interfacing-with-arduino-wireless-communication-0c13d4?f=1

  • I want the Tx Arduino to send the state of a button to the Rx Arduino.
  • When the button is pushed the Rx Arduino should turn on its buzzer.

I know the buzzer and buttons are wired correctly and the NRFs seem to be ok with the example tests i've used from the library.

Not sure what's wrong with the code any advice would be appreciated. Thank you

rx_trial.ino (1011 Bytes)

tx_trial.ino (1.2 KB)

1 Like

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.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

There is also a connection test program to check that the Arduino can talk to the nRF24 it is connected to.

A common problem with nRF24 modules is insufficient 3.3v current from the Arduino 3.3v pin. This seems to be a particular problem with nanos. At least for testing try powering the nRF24 with a pair of AA alkaline cells (3v) with the battery GND connected to the Arduino GND.

...R

1 Like

Hello R,

Thank you for your quick response and advice! It has been excellent help. So I've used 2x 1.5V batteries and am using the example program you've shared (thanks again).

When I first upload/turn on the Tx mode, a message is sent, the Tx node acknowledges this

"Data Sent Message 0 Acknowledge received"

and the Rx: "Data received Message 0"

But then the Tx constantly fails unless I restart the Arduino using the button on the Arduino.
This is the fail message: "Data Sent Message 1 Tx failed"

I am so glad there is now a blip of the thing working at least. It's getting there!

Cheers again.

Edit:

I should add that when I've tried to swap the Tx and Rx nodes it does not work.
When manually restarting the Tx as described before, it is not always guaranteed that the 0th message transmits successfuly and can take a few restarts.

I've checked the wiring and seems ok. Also the example code I tried before works tempermentally.

As I said, wireless problems can be difficult to debug.

Post the programs that YOU have uploaded to your Arduino and post a sample of the output from both Arduinos.

I presume you have tried the connection test program for both Arduinos?

Do you have some spare nRF24s in case one of them is faulty?

...R

I've used 2x 1.5V batteries

What have you used them for ?

Apologies for not being clearer. I will share the code below, thanks again for this trial code R.

This is the Tx code:

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

This is the Rx Code:

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

(all rights to R for this code!)

From this code, I sometimes get the 0th message sent through as I mentioned before, however never works when I reverse which node is the Tx and which is the Rx.

Sorry, when you say connection test, do you mean GettingStarted example from the NRF library? If so, then yes I have tried and the ping pong works tempermentally. Only once or twice did a message get relayed between nodes. When I reversed which was the ping and which was the pong, no messages got sent successfully/recieved successfully..

I don't have a spare NRF but will order another.

UKHeliBob, I put the positive of one battery to the negative of another for a 3V total. This was used to power the NRF modules. (And used another 2 x 1.5V batteries in same config for the other node).

Cheers again.

I put the positive of one battery to the negative of another for a 3V total. This was used to power the NRF modules.

Did you connect the GND pins of each RF24 module and the associated Arduino ?

DopeySnailS:
I will share the code below,

Please edit your Reply and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor See How to use the Forum

I need to be able to compare it with the original from my Tutorials.

...R

UKHeliBob:
Did you connect the GND pins of each RF24 module and the associated Arduino ?

Yes, the ground of the NRF is connected to the ground of the batteries and ground of Arduino. (For both nodes)

Robin2:
Please edit your Reply and use the code button </>

I have editted the previous message to match the coding format.

Thank you both.

DopeySnailS:
Sorry, when you say connection test, do you mean GettingStarted example from the NRF library?

No. There is a connection test program in my Simple nRF24L01+ Tutorial

...R

Hello again,

I have used the connection test program available on the link R posted. I have attatched print screens of the serial monitors for both Arduinos. (2 pics per Arduino and in order.)

Code: (Rights to R)

// 18 Mar 2018 - simple program to verify connection between Arduino
//      and nRF24L01+
//  This program does NOT attempt any communication with another nRF24

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>


#include "printf.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);
    printf_begin();

    Serial.println("CheckConnection Starting");
    Serial.println();
    Serial.println("FIRST WITH THE DEFAULT ADDRESSES after power on");
    Serial.println("  Note that RF24 does NOT reset when Arduino resets - only when power is removed");
    Serial.println("  If the numbers are mostly 0x00 or 0xff it means that the Arduino is not");
    Serial.println("     communicating with the nRF24");
    Serial.println();
    radio.begin();
    radio.printDetails();
    Serial.println();
    Serial.println();
    Serial.println("AND NOW WITH ADDRESS AAAxR  0x41 41 41 78 52   ON P1");
    Serial.println(" and 250KBPS data rate");
    Serial.println();
    radio.openReadingPipe(1, thisSlaveAddress);
    radio.setDataRate( RF24_250KBPS );
    radio.printDetails();
    Serial.println();
    Serial.println();
}


void loop() {

}

From what is said on the post R shared, lots of 0x00's and 0xFF's are a problem. I seem to get a lot of 0's for the RX_PW_P0-6 on both as well as DYNPD/FEATUR. What does this mean?

I would like to add that I have tried using a 10uF capacitor between ground and live of the nrf. This did not work. I tried the same method with the 3.3V Arduino out pin to power the nrf's, still nothing.

I've checked the connections and they are correct I believe.

I also bought a pack of 8 nrfs and I seem to get the same problems for them all.

Is there something really obvious I'm missing here?

Thank you for any help.

Please do not post pictures of text when you could have posted the actual text

Use the mouse to select the text in the Serial monitor then Ctrl/C on the keyboard to copy it to the clipboard and then post it here in code tags

For some reason when I try to copy and paste the text from the serial monitor it will only copy certain parts of the text and ignore the rest. Sorry about this.

Images from Reply #10 so we don't have to download them. See this Simple Image Posting Guide

...R

Sorry, those images are unreadable.

Copying and pasting text is usually just a case of Ctrl-A to select it all, Ctrl-C to copy it to the clipboard and Ctrl-V to paste it.

...R

Updated print screens to text format:

Arduino/nrf 1:

 CheckConnection Starting

 FIRST WITH THE DEFAULT ADDRESSES after power on
  Note that RF24 does NOT reset when Arduino resets - only when power is removed
   If the numbers are mostly 0x00 or 0xff it means that the Arduino is not
      communicating with the nRF24
 
 STATUS		 = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
 RX_ADDR_P0-1	= 0xe7e7e7e7e7 0x0000000106
RX_ADDR_P2-5	=0xc3 0xc4 0xc5 0xc6
TX_ADDR		 = 0xe7e7e7e7e7
RX_PW_P0-6	= 0x00 0x20 0x00 0x00 0x00 0x00
EN_AA		 = 0x3f
 EN_AA	=	0x3f
EN_RXADDR	= 0x03
RF_CH		 = 0x4c
RF_SETUP	=0x07
CONFIG		 = 0x0f
DYNPD/FEATURE	=0x00 0x00
Data Rate	 = 1MBPS
 CRC Length	 = 16 bits
 PA Power	 = PA_HIGH


 AND NOW WITH ADDRESS AAAxR  0x41 41 41 78 52   ON P1
  and 250KBPS data rate

 STATUS		 = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1	=0xe7e7e7e7e7 0x0000000106
 RX_ADDR_P2-5	=0xc3 0xc4 0xc5 0xc6
 TX_ADDR		 = 0xe7e7e7e7e7
 RX_PW_P0-6	=0x00 0x20 0x00 0x00 0x00 0x00
EN_AA		 = 0x3f
EN_RXADDR	=0x03
 RF_CH		 = 0x4c
 RF_SETUP	= 0x27
CONFIG		 = 0x0f
 DYNPD/FEATURE	= 0x00 0x00
 Data Rate	 = 250KBPS
 Model		 = nRF24L01+
 CRC Length	 = 16 bits
 PA Power	 = PA_HIGH

Arduino/nrf 2:

CheckConnection Starting

 FIRST WITH THE DEFAULT ADDRESSES after power on
   Note that RF24 does NOT reset when Arduino resets - only when power is removed
   If the numbers are mostly 0x00 or 0xff it means that the Arduino is not
     communicating with the nRF24

 STATUS		 = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1	=0x000000010b 0x0000000106
RX_ADDR_P2-5	=0xc3 0xc4 0xc5 0xc6
TX_ADDR		 = 0x000000010b
RX_PW_P0-6	 =0x20 0x20 0x00 0x00 0x00 0x00
EN_AA		 = 0x3f
 EN_AA	=	0x3f
EN_RXADDR	= 0x03
RF_CH		 = 0x4c
RF_SETUP	=0x07
CONFIG		 = 0x0c
DYNPD/FEATURE	=0x00 0x00
Data Rate	 = 1MBPS
 CRC Length	 = 16 bits
 PA Power	 = PA_HIGH


 AND NOW WITH ADDRESS AAAxR  0x41 41 41 78 52   ON P1
  and 250KBPS data rate
 
 STATUS		 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1	= 0x000000010b 0x0000000106
RX_ADDR_P2-5	=0xc3 0xc4 0xc5 0xc6
 TX_ADDR		 =  0x000000010b
 RX_PW_P0-6	= 0x20 0x20 0x00 0x00 0x00 0x00
 EN_AA		 = 0x3f
 EN_RXADDR	=0x03
 RF_CH		 = 0x4c
 RF_SETUP	= 0x27
CONFIG		 = 0x0c
 DYNPD/FEATURE	= 0x00 0x00
 Data Rate	 = 250KBPS
 Model		 = nRF24L01+
 CRC Length	 = 16 bits
 PA Power	 = PA_HIGH

Sorry for any formatting errors. Had to c + p each line manually, what a nightmare this is turning out to be! Thanks for everyones patience.

Those outputs suggest that your Arduinos are correctly communicating with their own Arduinos.

Had to c + p each line manually, what a nightmare this is turning out to be!

That suggests a general unfamiliarity with using a PC rather than anything specific to Arduino programming. Selecting, copying and pasting in the Arduino IDE is done in the normal way used by most PC programs.

...R

Hi there,

I promise you guys I know how to copy and paste it just wasn't working for me.

I fiddled around with my circuit again, repinning everything and unfortunately I've been unable to get the nrf simple one way transmission code to work that was uploaded by R and I have no idea why.

I went back to the 'GettingStarted' example in the nrf example library and this is for some reason working and I've no idea why. So I'm just working backwards from that now.

Thanks for all the help.

Merged topics

I think this is another problem on my side. If I use the radio.Write function three times I seem to get repeated sending of the messages. Any less than that and the messages only get sent once.

Solved... Syntax error.