Arduino wireless communication via NRF24L101 only works properly when receiver unit is connected to Computer and serial monitor is running

I have a set-up for remotely controlling 2 high torque DC motors. I am using two Arduino Megas, and wireless communication is achieved using NRF24L101 module.

The problem is: I can communicate between the two arduinos, only when the receiver unit is connected to computer via USB, AND serial monitor is running.

Otherwise, it works sporadically.

I have attached my circuit diagram, and codes.

Did I ground everything properly?
Is my code at fault?
Receiver Unit
ReceiverUnit
Transmitter Unit
TransmitterUnit

Receiver Unit Code

#include <nRF24L01.h>
#include <RF24.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <QMC5883LCompass.h>
#include <SdFat.h>
#include <SPI.h>

#define CE_PIN   7
#define CSN_PIN 8
#define SS 53

//new SPI pins
const uint8_t SD_CS_PIN     = 31;
const uint8_t SOFT_MOSI_PIN = 33;
const uint8_t SOFT_MISO_PIN = 35;
const uint8_t SOFT_SCK_PIN  = 37;

SoftSpiDriver<SOFT_MISO_PIN, SOFT_MOSI_PIN, SOFT_SCK_PIN> softSpi;
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SD_SCK_MHZ(0), &softSpi)
SdFat SD;


static const int RXPin = 19, TXPin = 18;
static const uint32_t GPSBaud = 9600;


byte Index = 0;

QMC5883LCompass compass; //create Compass object

// For stats that happen every 5 seconds
unsigned long last = 0UL;

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);

float msg[2];

byte leds = 0;

//This setup is to control the speed of a DC motor by a potentiometer using l298n driver
//We read the value from the analog input, calibrate it then inject to the module
//Refer to "L298nPot2DirAutoMan" sketch for more information

int ConA = 3;// Don't forget this is a PWM DI/DO
int in1 = 25; //Declaring where our module is wired
int in2 = 24;
int ConB = 4;// Don't forget this is a PWM DI/DO
int in3 = 22; //Declaring where our module is wired
int in4 = 23;

int motorSpeedA = 0;
int motorSpeedB = 0;

int LEFT = 0;
int RIGHT = 0;
  
void setup() {

  Serial.begin(9600);
  Wire.begin();
  compass.init();
  compass.setCalibration(-1138, 870, -700, 1312, -1, 1026);

  
  radio.begin();
  radio.openReadingPipe(0, thisSlaveAddress);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();

  Serial.print("Initializing SD card...");
    if (!SD.begin(SD_CONFIG)) {
    SD.initErrorHalt();
  }
  else {
        Serial.println("initialization done.");
    }
    
  pinMode(SD_CS_PIN, OUTPUT);
  pinMode(CSN_PIN, OUTPUT);
  pinMode(SS, OUTPUT);
  digitalWrite(SS,HIGH);
  
// Attach the L298n to MEGA
  pinMode(22, OUTPUT);
  pinMode(23, OUTPUT);  
  pinMode(4, OUTPUT);

  pinMode(24, OUTPUT);
  pinMode(25, OUTPUT);  
  pinMode(3, OUTPUT);
  
}

void loop()
{
  
      if(radio.available())    //checks whether any data have arrived at the address of the modem       
      radio.read(&msg, sizeof(msg));
      
      int xAxis = (msg[0]);
      int yAxis = (msg[1]);

      if (xAxis < 470) 
      {
      digitalWrite(in1, HIGH); //Switch between this HIGH and LOW to change direction
      digitalWrite(in2, LOW);
      digitalWrite(in3, HIGH); //Switch between this HIGH and LOW to change direction
      digitalWrite(in4, LOW);
      motorSpeedA = map(xAxis, 470, 0, 0, 255);
      motorSpeedB = map(xAxis, 470, 0, 0, 255);
      }

     else if (xAxis > 550) 
     {
     digitalWrite(in1, LOW); //Switch between this HIGH and LOW to change direction
     digitalWrite(in2, HIGH);
     digitalWrite(in3, LOW); //Switch between this HIGH and LOW to change direction
     digitalWrite(in4, HIGH);
     motorSpeedA = map(xAxis, 550, 1023, 0, 255);
     motorSpeedB = map(xAxis, 550, 1023, 0, 255);
     }

   if (yAxis < 470)
     {
      //Turn Left, decrease left motor speed, increase right motor speed
      LEFT = map(yAxis, 470, 0, 0, 255);
      motorSpeedA = motorSpeedA - LEFT;
      motorSpeedB = motorSpeedB + LEFT;

      // Confine the range from 0 to 255
      if (motorSpeedA < 0) {
      motorSpeedA = 0;
      }
      if (motorSpeedB > 255) {
      motorSpeedB = 255;
      }
     }
     
     else if (yAxis > 550)
     {
      //Turn Right, increase left motor speed, decrease right motor speed
      RIGHT = map(yAxis, 550, 1023, 0, 255);
      motorSpeedA = motorSpeedA + RIGHT;
      motorSpeedB = motorSpeedB - RIGHT;

      // Confine the range from 0 to 255
      if (motorSpeedA > 255) {
      motorSpeedA = 255;
      }
      if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
     }

     
     analogWrite(ConA,motorSpeedA);// Then inject it to our motorA
     analogWrite(ConB,motorSpeedB);// Then inject it to our motorB
     
     Serial.print(motorSpeedA);
     Serial.print(motorSpeedB);
     Serial.println();    
}

Transmitter Unit Code

// SimpleTx - the master or the transmitter

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


#define CE_PIN   7
#define CSN_PIN 8


const byte slaveAddress[5] = {'R','x','A','A','A'};

const byte numChars = 64;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

boolean newData = false;


RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
int motor_Pin1 = A1;
int motor_Pin2 = A2;

float msg[2];

byte leds = 0;

void setup() {
    
  radio.begin();
  radio.openWritingPipe(slaveAddress);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();

}
void loop() {
  radio.stopListening();
    
  msg[0] = analogRead(motor_Pin1);
  msg[1] = analogRead(motor_Pin2);

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

What is voltage of your LiPo ? And where it is connected to Mega - to VIN pin?

The voltage of the LiPo is 11.1v, and it is connected through the DC Barrel Plug.

It's wrong.
With your connection you feed arduino and all the modules via Mega's internal regulator, but as the regulator is only capable of powering the arduino itself, you would require external source of regulated 5 V to power the other modules, especially motors etc...
For using the LiPo as power source you need to add external stepdown converter with current, suitable for feeding all your modules. You could power from it your Mega too, feeding it to +5v pin

If I understood correctly, you're saying that with my connections, the modules (NRF24L01, compass, sdcard) are not getting power?

It also doesn't work when I power the arduino mega via USB without running serial monitor?

Does your battery have built-in protection circuits?

Like @b707 i would recommend buck converters to drop the battery voltage to 5V for connection to the 5V pin of the Megas. In addition an external 3.3V regulator for the radio modules. It is very important that you get the 3.3V supply to the radio modules to supply enough current. This is especially true for the high power (external antenna) modules. I use homemade adapters like these. They are powered by 5V and have a 3.3V regulator on the board.

yes, with your connection they don't get enough power

Perhaps this problem is related to the first. Or may be this is a separate issue.
But in any case, you first need to solve the power issue. With a circuit like yours, your device simply cannot work stably.

@b707 @groundFungus

I did a couple of tests, and yes, I agree it is a power issue.

When powered with LiPo through the DC barrel jack, it works sporadically (unstable), in between long delays.
When powered with USB, it works okay (with short delay, even without running Serial Monitor ).
It works best (no delay), when both LiPo and USB is connected.

The DC motors have their own power supply.

However, I would like to power the arduino and the other modules with the LiPo battery.

@groundFungus I will try this. Connect LiPo battery to buck converter, and power arduino and modules from the resulting 5V/ground of the buck converter? Something like this
BuckConverter

Yes, that is the good way, but aren't the rf24 modules 3.3V powered?
Connect 5V to the Arduino 5V pin, not Vin. That bypasses the weak on board 5V regulator.

Yes, the rf24 modules requires 3.3v. I will fix that in the actual set-up.

I used LM2596 dc-dc converter, and everything works now!

Thanks @groundFungus @b707

Great!