Arduino Pins all go low after serial port closes


I’m new to the forum and have been stumped by this problem for a while now. I’m trying to control the state of a few digital pins from the serial port on the Arduino. My issue is that when I close the serial port on the host machine that is feeding the commands to the Arduino, all of the Arduino’s pins go low. Can anyone assist or maybe have an answer as to why this happens ? I have posted some sample code below.


int Line[5] = {0,0,0,0};
int input = 0;

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(5, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);

void print() {
int loop = 1;
Serial.print(“Relay Control\n”);
while (loop <= 4) {
Serial.print("Line “);
Serial.print(”: ");
loop = loop + 1;

int check(int in) {
int pin = in + 1;
if (Line[in] == 0) {
digitalWrite(pin, LOW);
return 1;
else {
if (Line[in] == 1) {
digitalWrite(pin, HIGH);
return 0;

void loop() {
if (Serial.available() > 0) {
input =;
input = input - 48;
Line[input] = check(input);

Arduino is reset when the serial monitor is opened or closed.

Thanks for the response, I had that suspicion. Now to find a way around it.


You can disable the auto reset feature: