Nono + momentary buttons + 2 relay module

Hello,
I have nano connected with two momentary buttons and 2 relay module.
I programmed it , when the button 1 is pressed to switch on relay 1, delay some time and going back off and same for button 2 and relay 2. So far, so good it is work.

The main goal is never to have both relays switched on in the same time and my code work fine.
The problem is, that if the power is lost for very short time and nano is restarting , during this process on some check (i don't know what check) it switch on both relays in same time for short time, which is my issue. I have to avoid that.
If the power is out for more than 2-3 second and nano boot again - this problem is not persist.

https://codeshare.io/VZNKD8

Please post your code here following the advice given in the link below

#define button2 4
#define button1 3

boolean buttonState1;
boolean buttonState2;

int relayState1 = LOW;
int relayState2 = LOW;
int relay1 = 5;
int relay2 = 6;


void setup() {
pinMode(button1,INPUT);
pinMode(button2,INPUT);
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);


Serial.begin(9600);
}
void loop() {
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);

if (buttonState1 == LOW) {
      digitalWrite(relay1,LOW);
  } else {
    digitalWrite(relay1,HIGH);
    delay(5000);
    digitalWrite(relay1,LOW);
  } 

if (buttonState2 == LOW) {
      digitalWrite(relay2,LOW);
  } else {
    digitalWrite(relay2,HIGH);
    delay(5000);
    digitalWrite(relay2,LOW);
  } 
}

Okay. This is my code

afbeelding
As indicated in the the drawing, many of these relay shields work in reverse logic: if the IO is at 0V, then the relay is activated, please check if this is the case.
Then, it could be so that once your board boots, the IO is at 0V so the relay is activated.
Regarding the inputs:
You can decide to use INPUT_PULLUP instead of INPUT and use the switch to connect the IO to the ground. This way you can prevent stray signals from creating unwanted triggers.

ON power-on all IO-pins are in input-mode. This means a connectd device sees a "floating" voltage. To avoid this you can add an external pullup-resistor.
Pullup-resistor means one end is connected to +5V the other end of the resistor is connected to the IO-pin.

As long as the IO-pin is not defined as output the relay-modul gets 5V which keeps the relay switched off. If the IO-pin is defined as output and set to LOW the relay will switch on.

If your relay-board is HIGH-active you have to wire it opposite as a pull-down-resistor
best regards Stefan

#define button2 4
#define button1 3
#define relay1 5
#define relay2 6

boolean buttonState1;
boolean buttonState2;


void setup() {
pinMode(button1,INPUT);
pinMode(button2,INPUT);
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);

Serial.begin(9600);
}
void loop() {
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);

if (buttonState1 == LOW) {
      digitalWrite(relay1,LOW);
  } else {
    digitalWrite(relay1,HIGH);
    delay(5000);
    digitalWrite(relay1,LOW);
  } 

if (buttonState2 == LOW) {
      digitalWrite(relay2,LOW);
  } else {
    digitalWrite(relay2,HIGH);
    delay(5000);
    digitalWrite(relay2,LOW);
  } 
}

I think this solve the problem

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