Relay with button keeps going on and of

I use two buttons to control a relay. When I press them both, I want to turn on the relay. It does work when I push both the buttons, but when I release one of them, the relay keeps going on and off very fast. The code is very simple so I can't see the problem. Thanks for your help.

void setup() {
  pinMode(4, OUTPUT);
  pinMode(10, INPUT);
  pinMode(12, INPUT);

}

void loop() {
  if (digitalRead(12) == HIGH && digitalRead(10) == HIGH) {
    digitalWrite(4, LOW);
  }
  else {
    digitalWrite(4, HIGH);
  }
}

How are the inputs wired. Are there any pulldown resistors in place to keep the pins in a known state ?

Consider using INPUT_PULLUP in the pinMode()s and changing the program logic and circuit wiring to take the inputs LOW when the buttons are pressed.

Hi,
This might help.


Tom.. :slight_smile:

Thank you very much! I connected the INPUT to the ground with a 10K resistor as a Pulldown resistor and now it works fine.