Arduino Mega Repeatedly Crashes when USB is disconnected

I have a very small test program (below) which runs fine when the USB port is connected to my laptop. If I disconnect the USB port and reboot, the program crashes, restarts and crashes ad infinitum. The hardware is a mega2560 with an XBee (900MHz radio) mounted above on a shield board. The XBee uses software serial lib and using a XCTU program and another free standing XBee I can see output from the radio (mimicking the serial monitor which I disconnect to see the failure) which demonstrates the failure. I have also seen this behavior using a Nano33IOT. So i'm suspecting some change in the serial I/O lib because the program that led me to this very stripped down version has been working fine for over a year. Its only after I updated to that latest IDE 2.1.0 and updated all the libs that this started. Really need some help to get this fixed as it is incorporated into a controller that manages irrigation in our vineyard and its gonna be hot soon.

// Disconnect USB Test
#include <SoftwareSerial.h>

const byte XBeeRxdPin = 12;            // "RXD" Mega Pin 12 on XBee pin 2 Tx
const byte XBeeTxdPin = 11;            // "TXD" Mega Pin 11 on XBee pin 3 Rx
//Not all pins on the Mega and Mega 2560 support change interrupts,
//so only the following can be used for RX: 10, 11, 12, 13, 14, 15,
//50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
// Software Serial ports Rx and Tx are opposite the XBee Rx and Tx
// Create Software Serial Port for XBee
SoftwareSerial XBee(XBeeRxdPin, XBeeTxdPin);  //RX, TX not using Serial1 in production

void setup() { 

  Serial.begin(9600);
  //while (!Serial) {
  //  ; // wait forever for serial port to connect or timeout
  // } // I try this method of starting serial but it fails the same way
  delay(3000); // wait for serial port to connect
  XBee.begin(9600);
  delay(3000);  // wait for xbee opening

  XBee.println(F("Serial disco test"));
 
  XBee.println(F("end setup"));  
  

}  //  *********  end setup ************************

void loop() {  // ******* Top of LOOP ****
XBee.println(F("in loop"));
delay(5000); 

}  //end loop

Sounds like a power problem. How is the Arduino powered when not connected to the computer via USB?

Post a wiring diagram (hand drawn is fine).

Its only after I updated to that latest IDE 2.1.0 and updated all the libs that this started.

Try going back to 1.8.19 and see if it still crashes.

1 Like

Side note - why would you use SoftwareSerial on a MEGA when you have 3 other hardware Serial ports to play with?

Curious to know how the mega is powered when you remove the USB. Might be indeed part of the problem but you should have seen that before

Here's the schematic:

For the Mega, 5.7 V is too low for Vin and too high for the 5V pin.

Either apply 7V or greater to Vin, or regulated 5.0 V to the 5V pin.

Just lazy I guess, I used a similar configuration with Nano's and UNO's so software serial was easier. I can try to change that to hardware but I see the same problem on the NANO33IOT where I use Tx Rx (presumably hardware serial).

Ok, but this all worked for a year with the 5.7 v on Vin. The XBee wants 3.3v and the shield has an onboard voltage shifter but can't handle the 7.5 v.

Sorry, the labels on the Mega are PWRIN for the >7V unregulated power, and Vin for the 5.0V input.

5.7V on Vin exceeds the MCU specification, so you are lucky that it worked for so long.

I did try 1.8.19 and it still crashes. Where is the serial lib for the hardware serial ports?

I believe you have a power supply problem.

Where is the serial lib for the hardware serial ports?

It is built in. For the Mega, discard the SoftwareSerial library and just use

Serial1.begin(baud);
Serial1.print("something");

Same for Serial2, Serial3. Use the associated RXn, TXn pins.

will try that. Just so, the XBee is using the software serial and it keeps working. It's disconnecting the USB that causes the problem. Note that if I just disco the USB it keeps running ok. It starts to fail only when I reboot the Mega.

MAKES no difference this some kind of weird software bug, not a power or hardware issue. Can we focus on why the software would force a restart?

Hi @sderodeff

Your sketch's only dependency is the "Arduino AVR Boards" platform of the Mega 2560.

Do you happen to know which version of the "Arduino AVR Boards" platform you were using previously?

With 5.7V on Vin you are using the USB as a power supply if both are plugged in. When you unplug the USB your board switches source and is powered through Vin but 5.7V is not enough to provide stable operation for the Mega. The spec recommends more than 7V there.

As you have a 7.5V power supply, try to go directly from there to Vin and let the MEGA’s onboard regulator / diode do the work of providing stable 5V to the system.

I don't know how to tell. I assume whatever version came with the 1.8.19.

I tried the 7.5v supply directly and it worked. However the other boards weren't happy. So I went back to the regulator and bumped the 5.7v up to 6.2v. Now it seems to be stable and all the boards are working. So weird that it worked for so long and suddenly failed. Anyway, thanks for all the advice.

You could power the other like before - as long as you share the GND

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.