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
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.
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.
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 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.