Uno resets when Serial Monitor is not used

Hello. I hope this is a quick and simple problem, but can’t figure it. I’ve fairly confident with Arduino programming now, and I’ve not come across this before.

The problem is that when I have Serial.begin in my code, and I don’t open the Serial Monitor, the Uno resets itself at random intervals, never going longer than about 38 seconds between resets. However, if I do have the Serial Monitor open, or I comment out Serial.begin, I don’t have any problems.

What is going on? I’m sure in the past I have not had this problem. Not I am not actually printing anything to that serial port at the moment…

#include <SoftwareSerial.h> 

#define  BT_BAUD_RATE 9600
#define  pinLED       8                 // output pin to LED       
#define  pinBT_RX     12                // input pin from bluetooth adapter
#define  pinBT_TX     13                // output pin to bluetooth adapter

unsigned int  LEDondelay = 500;         // length of time LED will turn on
unsigned long LEDontime  = 0;           // time that the LED was turned on

SoftwareSerial bluetooth(pinBT_RX, pinBT_TX);  // setup software serial

void setup() {

  Serial.begin(9600);              // start hardware serial for debugging
  pinMode(pinLED,   OUTPUT);       // Set up pins
  bluetooth.begin(BT_BAUD_RATE);   //Start software serial
}

void loop() {
  
  char val;        // initialise variable at start of loop (value is reset each loop)

  if(bluetooth.available() ) {               // if data is available to read
    val = bluetooth.read();                  // read it and store it in 'val'
  }

  if( val == '0' ) {                           // if signal was recieved, do something
    digitalWrite(pinLED,   HIGH);            // turn on LED
    LEDontime   = millis();                  // record the time that the LED was turned on   
    bluetooth.print(millis());               // print confirmation with timestamp to bluetooth
    bluetooth.print("#");
  }
  
  if (millis() - LEDontime > LEDondelay && digitalRead(pinLED)==HIGH) {              
    digitalWrite(pinLED, LOW);                // turn LED off
  }

}

An arduino reset is usually power related, but some other application accessing the arduino serial port may be causing the reset thru open/close operations.

OK thanks.
But why would that be affected by the Serial Monitor being open?

Serial Monitor closed = lots of resets → Serial Monitor open = no problems

How do you know it is resetting?

You need to add some debugging code - perhaps flashing the onboard LED at different rates so you can follow the progress of the code when the Serial Monitor is not open.

That would, of course, mean moving your SoftwareSerial off pin 13.

Note, also, that the circuit for pin 13 is different from the other I/O pins so it may be wise to move SoftwareSerial in any case.

...R

Hmm, funny thing though is if i put debugging code in, the prog runs ok, cos i have serial monitor open! Its when i close serial monitor that it crashes, so difficult to debug in the usual way.

I know it is resetting because i am monitoring the output to Bluetooth (on my phone) via software serial and the number of millis keeps going back to zero. Also i can see the light on the board flashing as it cycles.

I hadn't though about changing pin 13 though, i'll try with different pins this evening.

OK thanks.
But why would that be affected by the Serial Monitor being open?

Because the reset happens when the port is opened and only one app can have the port open at a time.

Mark

kissenger:
, so difficult to debug in the usual way.

Which is why I suggested flashing lights :slight_smile:

…R

holmes4:
Because the reset happens when the port is opened and only one app can have the port open at a time.

So what else would/could be trying to access the port? Is this an issue on the computer side (in which case why does it result in an issue on the arduino) or on the arduino side?

kissenger:
So what else would/could be trying to access the port? Is this an issue on the computer side (in which case why does it result in an issue on the arduino) or on the arduino side?

I presume your Arduino is still connected to your PC with the USB cable while you are "not using the Serial Monitor"

Have you tried the code with the Pin changed from 13 to something else and with some suitable flashing of the LED for debugging purposes?

There is not much point speculating without doing any tests.

...R

Yes, Arduino is connected to PC (if running on battery, it works fine).

I have tried changing pins from pin 13, no luck, same behaviour.

As I mentioned, I have the ability to debug through the bluetooth serial and you'll see from the code I also have an external LED attached. There is nothing I can think of to test now that I have not already tried, having narrowed the problem down to the serial monitor and serial.begin() command.

What I don't understand is that if this is a conflict of come sort on the serial port, what would be causing it. What could I test to find out? I am out of ideas!!

OK, I've further narrowed this down to my laptop. When I tried on another laptop or a PC, I don't get this problem. So its something to do with the laptop (its not the IDE, as 1/ I have recently updated to the latest version and I had this problem on both versions and, 2/ problem also occurs when the IDE isnt even running, the Uno is just conneted for power). Nothing obvious in the preferences to change that might help.

Anyone got any ideas??

Anyone got any ideas??

A USB jack on a pc that is no longer making a good connection often causes erratic behavior. My note pad USB jacks are getting like this as the USB cable gets wiggled all the time as the notepad is moved around.

kissenger:
Yes, Arduino is connected to PC (if running on battery, it works fine).

That is a critical piece of information that has taken a long time to emerge.
To my mind it defintely points to a PC problem.

...R

My guess is some program/service running on the laptop is polling available serial connections and as stated above when the serial port is opened the Arduino resets. When you have the serial port open in another program (Serial monitor, putty etc) the serial port can no longer be accesses by this program/service causing the reset.
Either find the program/service doing the polling and kill it, put a capacitor between reset and gnd pins to prevent auto reset when not needed or (depending on your model of board) cut/solder the pads to prevent auto reset ever.