Arduino resets if serial monitor closes

Hello to all. This is my first post and after reading a lot i decided to write because i can’t find a solution.
I own an Arduino Mega 2560 with ethernet shield 2.

I have an external power source 12v for:

  1. Arduino Power
  2. 2 Relays (input)
  3. Internal funs
  4. 12v to 5v regulator for sensors.

I 've made a sketch for 11 sensors (only 1 is connected right now), 1 lcd display, 1 led alert.
I’ve managed to read an sql database and arm/disarm a relay and write all sensor data to a database.

I get the 3.3v out of arduino to power a led controller and also power a TTL/RS232 chip to output data.

The sketch runs perfect while serial monitor from ide is open. If i close it or disconnect the usb from pc restarting every some loops (not standard 2 or 3). As i open the serial monitor from ide, runs fine.

I replaced serial prints to serial1 prints (all of them) and watched through com. It happens exactly the same: arduino resets if i close the serial monitor from ide.

I removed all the serials commands and keeps reseting.

I also tried to remove 12v power source from arduino, keeping only usb power with the same result.

I hope not to confuse you, please ask me if something misunderstood.

Any suggestions/

Full2.ino (14.2 KB)

Restarting when a PC program opens the serial port is standard behaviour for Arduinos - it is how they detect if there is a new program to be uploaded.

If you don't want that behaviour then I reckon the simplest solution is for the PC to communicate with the Arduino using a USB-TTL cable connected to the Tx Rx and GND pins.

...R

Robin2:
Restarting when a PC program opens the serial port is standard behaviour for Arduinos - it is how they detect if there is a new program to be uploaded.

If you don't want that behaviour then I reckon the simplest solution is for the PC to communicate with the Arduino using a USB-TTL cable connected to the Tx Rx and GND pins.

...R

I don't care if is connected to pc. But if it is not and serial monitor not on arduino resets.

that's a pretty old article but may be some good food for thoughts

gordona1:
I don't care if is connected to pc. But if it is not and serial monitor not on arduino resets.

Sorry but I can't make sense of that. if the Arduino is not connected to the PC then nothing you do with the serial monitor can affect the Arduino.

Please describe in full step-by-step detail what you do that causes the problem you want to avoid.

...R

Robin2:
Sorry but I can't make sense of that. if the Arduino is not connected to the PC then nothing you do with the serial monitor can affect the Arduino.

Please describe in full step-by-step detail what you do that causes the problem you want to avoid.

...R

Thanks for the reply.
Usb Connection (com3)-> Upload the sketch. Arduino resets every 2-3 cycles. If I open the serial monitor it won't reset again.
If i disconnect usb cable and leave the 12v power, again resets every 2-3 cycles. If i disconnect 12v and leave only the usb cable, also resets.
I changed the serial output to serial1. Connected to pc (com1) again resets. If I open the serial monitor from ide (com3) works fine.
If I delete all Serial outputs again resets. It works only if I open serial monitor from IDE, even I have nothing to show..

Post the code

gordona1:
Usb Connection (com3)-> Upload the sketch. Arduino resets every 2-3 cycles.

You need to post the program. I don't know what you mean by a "cycle"

...R

The program is at my first post. By "cycle" i ment loop.
I disconnected the 2 3.3v (leds and ttl) and seems to working now only with external power supply, not the usb..

what's the power consumption of the whole thingy ? can you describe exactly how things are hooked up and powered ? a diagram would be nice

if something is drawing too much current, the voltage will drop and what's left for your arduino is not enough to keep it running. it will fail and reset itself. That might be what you see.

gordona1:
I disconnected the 2 3.3v (leds and ttl) and seems to working now only with external power supply, not the usb..

I don't know what that means in order to understand why it might matter. A wiring diagram will help - a photo of a simple pencil drawing is best.

The program is at my first post. By "cycle" i ment loop.

Sorry. I missed that.

You have a huge amount of code in loop(). If you separate the parts you describe as (for example) "Boiler section" into separate functions it will make the code easier to manage and to debug. For example, if you don't call the code in different sections you may be able to isolate which part is causing the problem.

...R
Planning and Implementing a Program

I am attaching a fast diagram. If it is necessary to visualize the whole thing, please give me time to draw it.

You are absolutely right about the big loop, maybe later i will do something about this.

If i connect all of them (except 2 sensors) my program resets after some loops but not in a specific point or specific loop. If i open the serial monitor from IDE the program runs perfectly.

P.S. I also took some pictures, but i don't believe that will help anybody..

do you have pull-ups on your switches ?

have you tried with a different MEGA ? different USB cable ? (just to validate it’s not a faulty part there)

What sort of relay are you using and is the relay coil designed for 5v low-current operation - the max current from an I/O pin is 40mA and 20mA would a sensible limit.

...R

J-M-L:
do you have pull-ups on your switches ?

have you tried with a different MEGA ? different USB cable ? (just to validate it’s not a faulty part there)

No i don’t.
I don’t have another mega… Some tests i’ve done to uno, but not the whole code. Just to mention that program run perfect only if i have a usb connection plus serial monitor on (even nothing to show). If i close the serial monitor, arduino resets after a while and again.

Robin2:
What sort of relay are you using and is the relay coil designed for 5v low-current operation - the max current from an I/O pin is 40mA and 20mA would a sensible limit.

…R

I am using this one: https://www.seeedstudio.com/Grove-2-Channel-SPDT-Relay.html
But i can turn the relay on/off the program works just fine…

How are the switches wired then ?

J-M-L:
How are the switches wired then ?

Hello.
Digital pin->ground
Inside the program:

pinMode(sw1, INPUT_PULLUP);
pinMode(sw2, INPUT_PULLUP);

Edit: I disconnected the switches, but i have the same result.

Ok si you do have pull-up :wink:

I did some changes to stabilize my project.

  1. I changed my power supply to a pc PSU for 12v and 5v.
  2. I rechecked my code and splitted into functions as Robin suggested.
  3. I remade all the connections from the beginning.

For now the system is stable but:
If i connect the usb cable, system freezes in the next loop (not specific point). However, i can upload if i reset it. If i open the serial monitor through IDE, system unfreezes and working.

Any ideas?

Post the new code