nRF24L01 communication problem

Hi there, i want to communicate between 2 arduino using nRF24L01 modules.
the probleme that the receiver can read only one message, if i do another transmission the receivere can't read the seconde message, please could you help me. ere is the code

//TX
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
#include <printf.h>
// declare variables
RF24 radio(9,10); //ce,cs pin
const uint64_t add1 = 999;
char msg[10];
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  radio.begin();
  printf_begin();
  radio.openWritingPipe(add1);
 radio.printDetails(); //check if there is an error appear when you try to connect nRF to arduino
}

void loop() {
  // put your main code here, to run repeatedly:

}
void serialEvent()
{


 memset(msg,' ',sizeof(msg));
  Serial.readBytesUntil('\n',msg,sizeof(msg));
  radio.write(msg,sizeof(msg));

  

}
//RX

#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
#include <printf.h>
// declare variables
RF24 radio(9,10); //ce,cs pin
const uint64_t add1 =999;
char msg[10];
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  radio.begin();
  printf_begin();
  radio.openReadingPipe(1,add1);
  radio.startListening();
  
}

void loop() {
  // put your main code here, to run repeatedly:
  if(radio.available())
  {
    
   memset(msg,' ',sizeof(msg));
    radio.read(msg,sizeof(msg));
    Serial.println(msg);
  }
}

How quickly do you send the second message? Maybe your receiver has not fully cleared it's receive buffer and reset the receive "listen" process before your second message arrives. It wouldn't seem to apply here, but I've also found that operating a transmit/receive pair in close proximity can overload the receiver front end unless the power is adjusted downward.

Hi thanks for the replay, i tried to send the second message with a diferent delay. but my receiver d'ont receiv rhe second mesage, but when i reset the sender th tx , it's works but only one message.
so i have to reset my tx every time when i send data.

Start simple.

  • Try sending a fixed piece of data that does not require any input by the user.
  • Make sure that the user input is working properly before extending the program to send the data

If you are using the ManiacBug version of the RF24 library that could be the problem. Remove it completely and use the TMRh20 version of the RF24 library

The pair of programs in this link may be useful.

...R

Thanks, i will try :slight_smile:

  radio.write(msg,sizeof(msg));

This is wrong. You want to send the number of characters in the array, not the entire array.

Use strlen(), NOT sizeof().

And, you should be initializing the array to NULL, not space.

Hi,

so i have to reset my tx every time when i send data.

Would it be because your code is in the void setup()? ? ?

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  radio.begin();
  printf_begin();
  radio.openWritingPipe(add1);
 radio.printDetails(); //check if there is an error appear when you try to connect nRF to arduino
}

void loop() {
  // put your main code here, to run repeatedly:

}

There is nothing in the void loop() to TX for the second time.
void setup() is executed ONCE and ONCE only when you RESET the arduino.

Hope this helps... Tom... :slight_smile:

Would it be because your code is in the void setup()?

You missed a bit...

void serialEvent()
{
 memset(msg,' ',sizeof(msg));
  Serial.readBytesUntil('\n',msg,sizeof(msg));
  radio.write(msg,sizeof(msg));
}

What is the output from the printDetails line? maybe you wired the nrf's wrong..

also how do you power the nrf's?

I struggled with this a lot to but i got it working
(with help from the guys here)

PaulS:
Use strlen(), NOT sizeof().

And, you should be initializing the array to NULL, not space.

I did it but my receiver receive only one data

Robin2:
Start simple.

  • Try sending a fixed piece of data that does not require any input by the user.
  • Make sure that the user input is working properly before extending the program to send the data

If you are using the ManiacBug version of the RF24 library that could be the problem. Remove it completely and use the TMRh20 version of the RF24 library

The pair of programs in this link may be useful.

...R

i use this library also

jone31:
i use this library also

I hope you mean "also" in the sense that you are using the same library as I am.
The words could mean that you are using the TMRh20 library as well as the ManiacBug library and that would confuse the Arduino IDE.

If you write the simple program I suggested and it does not work then post that code so we can help.

...R

it's works now, i changed the library. now i have another problem.

i want to control a motor with pwm.

i want to send a value from 0 to 255 to control my motor,

i did this but it does'nt work

//the receiver

memset(msg,' ',sizeof(msg));
radio.read(msg,sizeof(msg));
Serial.println(msg);
analogWrite(6,*msg);

could you help me please.

jone31:
could you help me please.

Only if you post the complete code for both the Tx and Rx programs. The snippet is meaningless in the context of your problem.

...R