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.