Relays modules reset when closing then opening again serial port of the arduino

Hi !

In my project, I'm using an Arduino Mega to control an 8 Relays module Like this one

In this project, I use a python script to get values from sensors and plot the datas on a GUI. In this GUI, I fix the differents consign where I want the relays to close or open
(e.g. I have a CO2 sensor and when the CO2 value is Higer than 400000ppm I neet to open the relay 1 ad close it when below to 350000ppm)

here is My problem:
When I open the serial port of the arduino using the python line :

ser = serial.Serial('COM5', 9600)

all the relays are activated.

then my python script sends command to open and close the relays :

ser.write(b'1') # to open relay 1
ser.write(b'A')# to close it

at the end of the script, I need to close the serial port. If I don't, I cannot run the script again (or another script) without unpluging and pluging again My arduino.

The problem is that :

When I close the port : it's ok, the Relays keep their positions.
when I open again the port : all the relays are opened by the arduino reinitialzing.

Do you know, how I can avoid this ? maybe it is more an electrical issue more than a programming issue but I'm nor sure.

Below my arduino Code:

//=====Relais=========================================================
const int Pin8 = 5;
const int Pin7 = 6;
const int Pin6 = 7;
const int Pin5 = 8;
const int Pin4 = 9;
const int Pin3 = 10;
const int Pin2 = 11;
const int Pin1 = 12;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  
//======== Relais ==================================  
  pinMode(Pin1, OUTPUT);
  pinMode(Pin2,OUTPUT);
  pinMode(Pin3,OUTPUT);
  pinMode(Pin4,OUTPUT);
  pinMode(Pin5,OUTPUT);
  pinMode(Pin6,OUTPUT);
  pinMode(Pin7,OUTPUT);
  pinMode(Pin8,OUTPUT);


void loop() {
//===== Relays===========================================
 if (Serial.available() > 0) {
    //Serial.println("h");
       // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    
   
    if (incomingByte == 'A') {
      Serial.println(incomingByte);
      digitalWrite(Pin1, HIGH);
      }
 
    if (incomingByte == '1'
    ) {
      digitalWrite(Pin1, LOW);
    }
    if (incomingByte == 'B') {
      Serial.println(incomingByte);
      digitalWrite(Pin2, HIGH);
      }

    if (incomingByte == '2') {
      digitalWrite(Pin2, LOW);
    }
    if (incomingByte == 'C') {
      Serial.println(incomingByte);
      digitalWrite(Pin3, HIGH);
      }
   
    if (incomingByte == '3') {
      digitalWrite(Pin3, LOW);
    }
    if (incomingByte == 'D') {
      Serial.println(incomingByte);
      digitalWrite(Pin4, HIGH);
      }
  
    if (incomingByte == '4') {
      digitalWrite(Pin4, LOW);
    }
    if (incomingByte == 'E') {
      Serial.println(incomingByte);
      digitalWrite(Pin5, HIGH);
      }
 
    if (incomingByte == '5') {
      digitalWrite(Pin5, LOW);
    }
    if (incomingByte == 'F') {
      Serial.println(incomingByte);
      digitalWrite(Pin6, HIGH);
      
      }
    
    if (incomingByte == '6') {
      digitalWrite(Pin6, LOW);
    }

    if (incomingByte == '7') {
      digitalWrite(Pin6, LOW);
    }
    if (incomingByte == 'G') {
      Serial.println(incomingByte);
      digitalWrite(Pin7, HIGH);
      }

    if (incomingByte == '8') {
      digitalWrite(Pin7,LOW);
    }if (incomingByte == 'H8') {
      Serial.println(incomingByte);
      digitalWrite(Pin8, HIGH);
      }

    if (incomingByte == 'H') {
      digitalWrite(Pin8, LOW);
    }
    //The command Closing all the valves
    if (incomingByte == 'I'){
     
      digitalWrite(Pin1,HIGH);
      digitalWrite(Pin2,HIGH);
      digitalWrite(Pin3,HIGH);
      digitalWrite(Pin4,HIGH);
      digitalWrite(Pin5,HIGH);
      digitalWrite(Pin6,HIGH);
      digitalWrite(Pin7,HIGH);
      digitalWrite(Pin8,HIGH) ;   
      
    }
    //The command opening all the valve
    if (incomingByte == '9'){
      digitalWrite(Pin1,LOW);
      digitalWrite(Pin2,LOW);
      digitalWrite(Pin3,LOW);
      digitalWrite(Pin4,LOW);
      digitalWrite(Pin5,LOW);
      digitalWrite(Pin6,LOW);
      digitalWrite(Pin7,LOW);
      digitalWrite(Pin8,LOW) ; 
    }  
  }

Thank you in advance.

You need to post your code in order for people to figure out what's wrong - sounds like you are initializing the pins as LOW (most relay modules are active low).

BTW opening a serial connection will toggle DTR which is wired to the RESET line on the Arduino Mega via a capacitor - this is used to trigger sketch upload without having to press the reset button.

Hi !
Thank you for your answer, I edited my post with my code.

sounds like you are initializing the pins as LOW (most relay modules are active low).

no I am not, but I used to initialyze all the pins to "HIGH" to avoid this. but this doesn't change anything to my problem.

BTW opening a serial connection will toggle DTR which is wired to the RESET line on the Arduino Mega via a capacitor - this is used to trigger sketch upload without having to press the reset button.

Ok, thank you ! so , may be I need to find a way to avoid toggling DTR or unwire the RESET from DTR ?

Regards

To disable the "reset on serial connection feature, wire a (roughly) 1 to 10 microfarad capacitor from the Reset pin (+) to the Ground pin (-). This will filter out the pulse that tries to reset the board when a serial connection is established.

Uhm, yes, you are. You'll need to do something like this in setup...

pinMode(Pin1, INPUT_PULLUP);
pinMode(Pin1, OUTPUT);

or...

digitalWrite(Pin1, HIGH);
pinMode(Pin1, OUTPUT);

... same idea for remaining relay pins.

Hi,
Can you please post a circuit diagram of your project?
What relays are you using, are they relay modules?
Can you please post link to data/specs?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

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