NRF24l01 not receiving (or transmitting?)

Im working on a project with 2 Arduino nanos, each with its own NRF24l01. The transmitter has 2 buttons and a joystick, and the receiver is connected to a motor driver. Everything seems to be working (and was actually working when I tested it on a breadboard), but now the radios won't communicate.
This is the transmitter code:

#include "nRF24L01.h" // NRF24L01 library created by TMRh20 https://github.com/TMRh20/RF24
#include "RF24.h"
#include "SPI.h"

int SentMessage[1] = {000}; 
RF24 radio(9,10);
const uint64_t pipes[2] = {   0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

#define Button1 2
#define Button2 3

int button1=0;
int button2=0;

int xPosition = 0;
int yPosition = 0;

int directionX;
int directionY;

const int x_pin = A1;
const int y_pin = A2;

void setup() {
  pinMode(Button1, INPUT);
  pinMode(Button2, INPUT);
  pinMode(x_pin, INPUT);
  pinMode(y_pin, INPUT);

  Serial.begin(9600);

  pinMode(Button1, INPUT_PULLUP); 
  digitalWrite(Button1,HIGH); 
  pinMode(Button2, INPUT_PULLUP);
  digitalWrite(Button2, HIGH);

  radio.begin();
  //radio.openWritingPipe(pipe);   

  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(1,pipes[1]);
}

This is the receiver code:

#include "nRF24L01.h" // NRF24L01 library created by TMRh20 https://github.com/TMRh20/RF24
#include "RF24.h"
#include "SPI.h"

int ReceivedMessage[1] = {000};
RF24 radio(9,10);
const uint64_t pipes[2] = {   0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

#define MotorIN1 2
#define MotorIN2 3
#define ENA 5

#define MotorIN3 7
#define MotorIN4 4
#define ENB 6


int pwmSignal = 0;


void setup(void) {
  pinMode(MotorIN1, OUTPUT);
  pinMode(MotorIN2, OUTPUT);
  pinMode(ENA, OUTPUT);
  
  Serial.begin(9600);

  radio.begin(); // Start the NRF24L01
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1,pipes[0]);
  radio.startListening(); // Listen to see if information received

}

next in the receiver I have this:

void loop(void) {
  while (radio.available()){

but the loop never seems to start? Im thinking that this is either because:

  1. The transmitter never transmits
  2. The receiver never receives
  3. One of the radio is not operating

Please help ! Im at a loss

Vital parts of the code are missing, so impossible for anyone to help. There are numerous guides on how to use NRF24 with Arduino, try to get one of those working before you add your own code for handling other things.

I have followed a guide. Tested the same setup on a breadboard and it worked, so I don't see why this won't. Will post the complete code :+1:

Transmitter:

#include "nRF24L01.h" // NRF24L01 library created by TMRh20 https://github.com/TMRh20/RF24
#include "RF24.h"
#include "SPI.h"

int SentMessage[1] = {000}; 
RF24 radio(9,10);
const uint64_t pipes[2] = {   0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };



#define Button1 2
#define Button2 3

int button1=0;
int button2=0;

int xPosition = 0;
int yPosition = 0;

int directionX;
int directionY;

const int x_pin = A1;
const int y_pin = A2;


void setup() {
  pinMode(Button1, INPUT);
  pinMode(Button2, INPUT);
  pinMode(x_pin, INPUT);
  pinMode(y_pin, INPUT);

  Serial.begin(9600);


  pinMode(Button1, INPUT_PULLUP); 
  digitalWrite(Button1,HIGH); 
  pinMode(Button2, INPUT_PULLUP);
  digitalWrite(Button2, HIGH);

  radio.begin();
  //radio.openWritingPipe(pipe);   

  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(1,pipes[1]);


}

void loop() {
  button1 = digitalRead(Button1);
  button2 = digitalRead(Button2);
  xPosition = analogRead(x_pin);
  yPosition = analogRead(y_pin);

  directionX = "0";
  directionY = "0";

  if (analogRead(x_pin) > 600){
    Serial.println("TOP");
    directionY = "TOP";
  }
  if (analogRead(x_pin) < 300){
    Serial.println("BOTTOM");
    directionY = "BOT";
  }
  if (analogRead(y_pin) > 600){
    Serial.println("RIGHT");
    directionX = "R";
  }
  if (analogRead(y_pin) < 300){
    Serial.println("LEFT");
    directionX = "L";
  }

  if ((button1 == LOW && button2 == LOW) or (button1 == HIGH && button2 == HIGH)){
    SentMessage[0] = 000;
    radio.write(SentMessage,1);
    Serial.println(1);
  } else {
    if (button1 == HIGH){
      SentMessage[0] = 001;
      radio.write(SentMessage,1);      
    } else if (button2 == HIGH){
      SentMessage[0] = 010;
      radio.write(SentMessage,1);       
    }
    
    if ((directionY == "TOP" && directionX == "0") or (directionY == "0" && directionX == "0")){
      SentMessage[0] == 011;
      radio.write(SentMessage,1);
      Serial.println("E");   
    } else if ((directionY == "TOP" && directionX == "R") or (directionY == "0" && directionX == "R")) {
      SentMessage[0] == 100;
      radio.write(SentMessage,1);
      Serial.println("D");   
    }else if (directionY == "BOT" && directionX == "R") {
      SentMessage[0] == 101;
      radio.write(SentMessage,1);
      Serial.println("C");         
    } else if (directionY == "BOT" && directionX == "L") {
      SentMessage[0] == 110;
      radio.write(SentMessage,1);
      Serial.println("B");   
    } else if ((directionY == "0" && directionX == "L") or (directionY == "TOP" && directionX == "L")) {
      SentMessage[0] == 111;
      radio.write(SentMessage,1);
      Serial.println("A");      
    }
  }
  Serial.println(SentMessage[0]);
  delay(1000);

  if (radio.available()){
    Serial.println("OK");
  }
}

Receiver:

#include "nRF24L01.h" // NRF24L01 library created by TMRh20 https://github.com/TMRh20/RF24
#include "RF24.h"
#include "SPI.h"

int ReceivedMessage[1] = {000};
RF24 radio(9,10);
const uint64_t pipes[2] = {   0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

#define MotorIN1 2
#define MotorIN2 3
#define ENA 5

#define MotorIN3 7
#define MotorIN4 4
#define ENB 6


int pwmSignal = 0;


void setup(void) {
  pinMode(MotorIN1, OUTPUT);
  pinMode(MotorIN2, OUTPUT);
  pinMode(ENA, OUTPUT);
  
  Serial.begin(9600);

  radio.begin(); // Start the NRF24L01
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1,pipes[0]);
  radio.startListening(); // Listen to see if information received

}

void loop(void) {
  while (radio.available()){
    radio.read(ReceivedMessage, 1);

    if (ReceivedMessage[0] == 000){
      pwmSignal = 0;
      analogWrite(ENA, pwmSignal);
      digitalWrite(MotorIN1, LOW);
      digitalWrite(MotorIN2, LOW);
    }   
    if (ReceivedMessage[0] == 001){
      pwmSignal = 0;
      analogWrite(ENA, pwmSignal);
      digitalWrite(MotorIN1, HIGH);
      digitalWrite(MotorIN2, LOW);
    }
    if (ReceivedMessage[0] == 010){
      pwmSignal = 0;
      analogWrite(ENA, pwmSignal);
      digitalWrite(MotorIN1, LOW);
      digitalWrite(MotorIN2, HIGH);
    }

    if (ReceivedMessage[0] == 011){
      pwmSignal = 255;
      analogWrite(ENA, pwmSignal);  
    }
    if (ReceivedMessage[0] == 100){
      pwmSignal = 100;
      analogWrite(ENA, pwmSignal);
    }
    if (ReceivedMessage[0] == 101){
      pwmSignal = 0;
      analogWrite(ENA, pwmSignal);
    }
    if (ReceivedMessage[0] == 110){
      pwmSignal = 0;
      analogWrite(ENA, pwmSignal);
    }
    if (ReceivedMessage[0] == 111){
      pwmSignal = 100;
      analogWrite(ENA, pwmSignal);
    }
    delay(2);
    Serial.print(pwmSignal);
  }

  Serial.print("ABC");
  delay(1000);

}

Here is your problem:

int SentMessage[1] = {000}; //An "int" is 2 bytes, and "000" is the same as "0"
...
SentMessage[0] = 000; //Again, why do you want three 0's?
radio.write(SentMessage,1); //Here you send one byte, but the message is 2 bytes.

//radio.write(SentMessage, sizeof(SentMessage)); //This is how to do it

The same problem is present in the receiver.

So I need to change

radio.write(SentMessage,1);

to

radio.write(SentMessage,2);

?

Yes, but I would encourage you so use "sizeof()" or a constant instead of defining the size as inlined constants. If you change the size of the buffer, you must also change all the inlined constants and that is cumbersome.

//Declare
const int BUFLEN = 1; //Number of elements in the array
const int BUFSIZE = BUFLEN * sizeof(int); //Size in bytes of the array
int buffer[BUFLEN] = {0}; //The array

//Use
radio.write(buffer, BUFSIZE);
radio.read(buffer, BUFSIZE);

Does the constant declare the size of bytes? So I would just use BUFSIZE = 2 so I can use values from 0 to 111?

Hi,

What have you got it constructed on now, if not breadboard?

Can you please post some circuit and construction information, pictures, circuit?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

Sorry, I made a mistake and I have corrected it in the example in #7

Why octal?

what does octal mean?

I have soldered everything to pcbs. I am trying to construct an rc
Here are the schematic for the receiver and a picture of the pcbs :slight_smile:Processing: IMG_0076.jpg...
Uploading: IMG_3573 2.JPG...

10 - a decimal number (base 10)
0x10 - a hexadecimal number (base 16)
010 - an octal number (base 8)
0b10 - a binary number (base 2)

How long have you have the RF24s?
When did they last work?
Did they ever work?
What range do you want?
How much interference can you tolerate?
Have they had any hard knocks?

My solution is just to buy RF nanos. They are basically nRF24s but soldered onto a nano. They work very well, if not without a bit of range loss.

I ask about knocks because the timing crystal on RF24 modules can be misaligned and fail.

oh okay, I'm trying to use binary ig. Is there one that is better than the others to use?

This is the first project im using them on, so about a month (have tried to switch them into complete new ones, but still won't work). They worked just fine before I soldered everything. I have changed the code a bit so im guessing that's where the mistake is.
Dont need long range, just a couple of metres. Im just making a RC car as a hobby project

Is it possible that the heat from soldering might have damaged them?

EDIT - Another possibility is that your PCB is flawed.

Im sorry, but I still dont understand how to implement this into the code. And wont the receiver still receive the 000 signal? And if so the while loop would start, which it dosent...

As I said, I have tried to change them into someone that Is completely new...