Go Down

Topic: using nrf24l01 can not receive reply from another one (Read 213 times) previous topic - next topic

kissybaby

Hi bro, I can not find any error from the code ,but the project can not work.
the original code is from this youtuber link https://www.youtube.com/watch?v=4GYGwT5vJ-M&feature=youtu.be&ab_channel=KendinYap
I solve one error from the receiver part ,but still can not receive any signal from the transmitter 
any guy can help me ?

transmitter code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(8,9); // CE, CSN
const byte address[6] = "ABCDE";
 char xyData[32] = "";
 String xAxis, yAxis;
void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}
void loop() {
  xAxis = analogRead(A1); // Read Joysticks X-axis
  yAxis = analogRead(A0); // Read Joysticks Y-axis
  // X value
  xAxis.toCharArray(xyData, 5); // Put the String (X Value) into a character array
  radio.write(&xyData, sizeof(xyData)); // Send the array data (X value) to the other NRF24L01 modile
  Serial.print("xAxis:");
  Serial.println(xyData);
  // Y value
  yAxis.toCharArray(xyData, 5);
  radio.write(&xyData, sizeof(xyData));
  Serial.print("yAxis:");
  Serial.println(xyData);
  delay(20);
}



receiver code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define enA 2  
#define in1 3
#define in2 4
#define enB 7   
#define in3 5
#define in4 6
RF24 radio(8, 9); // CE, CSN
const byte address[6] = "ABCDE";
char xyData[32] = "";
int  xAxis, yAxis;
int motorSpeedA = 0;
int motorSpeedB = 0;
void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}
void loop() {
  if (radio.available()) {   // If the NRF240L01 module xy data
    radio.read(&xyData, sizeof(xyData)); // Read the data and put it into character array
    xAxis = atoi(&xyData[0]); // Convert the data from the character array (xy X value) into integer
    delay(10);
    radio.read(&xyData, sizeof(xyData));
    yAxis = atoi(&xyData[0]);
    delay(10);
    digitalWrite(10,HIGH);//D10 connect LED
  }
  else {
    yAxis = 510;
    xAxis = 510;
  }
  
  // Y-axis used for forward and backward control
  if (yAxis < 470) {
    // Set Motor A backward
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    // Set Motor B backward
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(yAxis, 470, 0, 0, 255);
    motorSpeedB = map(yAxis, 470, 0, 0, 255);
  }
  else if (yAxis > 550) {
    // Set Motor A forward
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    // Set Motor B forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(yAxis, 550, 1023, 0, 255);
    motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  }
  // If joystick stays in middle the motors are not moving
  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
  }
  // X-axis used for left and right control
  if (xAxis < 470) {
    // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
    int xMapped = map(xAxis, 470, 0, 0, 255);
    // Move to left - decrease left motor speed, increase right motor speed
    motorSpeedA = motorSpeedA + xMapped;
    motorSpeedB = motorSpeedB - xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
    if (motorSpeedB > 255) {
      motorSpeedB = 255;
    }
  }
  if (xAxis > 550) {
    // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
    int xMapped = map(xAxis, 550, 1023, 0, 255);
    // Move right - decrease right motor speed, increase left motor speed
    motorSpeedA = motorSpeedA - xMapped;
    motorSpeedB = motorSpeedB + xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA > 255) {
      motorSpeedA = 255;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
  }
  // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  if (motorSpeedA < 70) {
    motorSpeedA = 0;
  }
  if (motorSpeedB < 70) {
    motorSpeedB = 0;
  }
  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}

Robin2

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. If the first example does not work be sure to try the connection test for both of your Arduinos. Nothing will work if the connection test fails.

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 the nano. The high-power nRF24s (with the external antenna) will definitely need an external power supply. 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

PS ... When posting code please use the code button </>
Code: [Select]
so your code
looks like this

It makes it much easier to help you.
Two or three hours spent thinking and reading documentation solves most programming problems.

kissybaby

#2
Oct 15, 2020, 12:31 pm Last Edit: Oct 15, 2020, 12:32 pm by kissybaby
thanks a lot for your suggestion ,I tried the nrf24l01 example code ,and find it is work correctly ,i can receive signal from any board send to .  So I'm sure it must be the code error ,but seems out of my ability.

Robin2

I tried the nrf24l01 example code ,and find it is work correctly ,i can receive signal from any board send to .  So I'm sure it must be the code error ,but seems out of my ability.
Have your tried using my examples as the starting point and adding your additional code onto them, a little bit at a time, checking that everything works at each stage?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Whandall

Use a single struct with both x and y values as integers.

Don't apply default values if there is no packat available, this will be the default case.
If you need some fail save values, apply them after a second of no reception.

Don't use delay in a communication sketch.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

kissybaby

Have your tried using my examples as the starting point and adding your additional code onto them, a little bit at a time, checking that everything works at each stage?

...R
I tried it ,but failed ,the TX sample record    Data Sent Message 0  Tx failed    

Robin2

I tried it ,but failed ,the TX sample record    Data Sent Message 0  Tx failed   
This is a bit confusing because your Reply #2 gave me the impression that you were able to get my programs working. However the piece I have quoted suggests that they are not working.

Also, my Reply #3 envisaged that you would extend my code to include your additional project requirements so I don't know if it is those additional requirements that are causing the problem.

Please post the exact code that you tried (for the Tx and the Rx).

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

kissybaby

#7
Oct 17, 2020, 02:10 am Last Edit: Oct 17, 2020, 02:13 am by kissybaby
This is a bit confusing because your Reply #2 gave me the impression that you were able to get my programs working. However the piece I have quoted suggests that they are not working.

Also, my Reply #3 envisaged that you would extend my code to include your additional project requirements so I don't know if it is those additional requirements that are causing the problem.

Please post the exact code that you tried (for the Tx and the Rx).

...R
Exactly the same as your example,the wiring is also follow the picture shown.
GND-GND 
VCC-3.3V
CSN-D9
CE-D10
MOSI-D11
SCK-D13
MISO-D12

But i tried another example,  that can work correctly ,can send and can reveived. maybe my problem is in the library which i use.

Robin2

Exactly the same as your example
Even so, please post the code that YOU have uploaded to your Arduinos. And please clarify whether my example code was working when you wrote Reply #2

Quote
But i tried another example,  that can work correctly ,can send and can reveived. maybe my problem is in the library which i use.
What other example - you have to be specific if you want help.

What library are you using? My examples use the TMRh20 version of the RF24 library

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

kissybaby

send.ino
Code: [Select]
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
 
int value;
 
void setup()
{
  Serial.begin(9600);
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"FGHIJ");
  Mirf.payload = sizeof(value);
  Mirf.channel = 90;             
  Mirf.config();
}
 
void loop()
{
  Mirf.setTADDR((byte *)"FGHIJ");         
  value = random(255);                   
  Mirf.send((byte *)&value);               
  while(Mirf.isSending()) delay(1);
  Serial.println(value);       
  delay(1000);
}


receive.ino
Code: [Select]
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
 
int value;
 
void setup()
{
  Serial.begin(9600);
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
 
  Mirf.setRADDR((byte *)"FGHIJ");
  Mirf.payload = sizeof(value);   
  Mirf.channel = 90; 
  Mirf.config();
  Serial.println("Listening..."); 
}
 
void loop()
{
  if(Mirf.dataReady()) { 
    Mirf.getData((byte *) &value);
    Serial.print("Got data: ");
    Serial.println(value);
  }
}


and the library is from this link https://github.com/aaronds/arduino-nrf24l01

this example  work correctly. i check the send Serial window and receive window ,the random number is the same.

Robin2

and the library is from this link https://github.com/aaronds/arduino-nrf24l01

this example  work correctly.
I'm not familiar with the MIRF library.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.


Go Up