Relay with button

Hey,
I now have this code for button an relais. If I now press the button twice, the relay will switch on and I have to press the button once to turn the relay off. can someone help me with adjusting the code a bit because I would like the relay to switch off automatically instead of pressing the button.

here is the code,:slight_smile:
/*
State change detection (edge detection)

Often, you don’t need to know the state of a digital input all the time, but
you just need to know when the input changes from one state to another.
For example, you want to know when a button goes from OFF to ON. This is called
state change detection, or edge detection.

This example shows how to detect when a button or button changes from off to on
and on to off.

The circuit:

  • pushbutton attached to pin 2 from +5V
  • 10 kilohm resistor attached to pin 2 from ground
  • LED attached from pin 13 to ground (or use the built-in LED on most
    Arduino boards)

created 27 Sep 2005
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/ButtonStateChange
*/

// this constant won’t change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println(“on”);
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println(“off”);
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;

// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 3 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}

}

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code

Under what circumstances do you want the relay to turn off ?

button_and_relay.ino (2.4 KB)

is this so good?

I would like to have to press the button twice to switch on the relay and that the relay switches off automatically after 3 minutes

Better, but posting here in code tgas as below would be best

/*
  State change detection (edge detection)

  Often, you don't need to know the state of a digital input all the time, but
  you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON. This is called
  state change detection, or edge detection.

  This example shows how to detect when a button or button changes from off to on
  and on to off.

  The circuit:
  - pushbutton attached to pin 2 from +5V
  - 10 kilohm resistor attached to pin 2 from ground
  - LED attached from pin 13 to ground (or use the built-in LED on most
    Arduino boards)

  created  27 Sep 2005
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/ButtonStateChange
*/

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup()
{
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop()
{
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    // if the state has changed, increment the counter
    if (buttonState == HIGH)
    {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    }
    else
    {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 3 == 0)
  {
    digitalWrite(ledPin, HIGH);
  }
  else
  {
    digitalWrite(ledPin, LOW);
  }
}

ok so like this?

What period should elapse after the relay has been switched on until it automatically switches itself off ?

the relay should turn off automatically after 3 minutes

To summarise

  • The relay starts in an off state
  • Press the button twice and the relay comes on
  • 3 minutes later the relay turns off

Questions

  • should there be a way to turn off the relay before 3 minutes ?
  • what happens if you press the button once then change your mind ?
  • is the Arduino doing anything else during the 3 minutes ?

yes the summarise is good

the arduino does nothing else and if you press the button in the 3 minutes, nothing should happen

Something for you to look at and take ideas from

const int  buttonPin = A3;
const int ledPin = 13;

int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
void setup()
{
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState)
  {
    if (buttonState == LOW)
    {
      buttonPushCounter++;
    }
  }
  lastButtonState = buttonState;
  if (buttonPushCounter == 2)
  {
    Serial.println("counter is 2");
    digitalWrite(ledPin, HIGH);
    Serial.println("waiting");
    delay(5000);
    digitalWrite(ledPin, LOW);
    Serial.println("finished waiting");
    buttonPushCounter = 0;
  }
}

the code works great there is only 1 small problem if I push the button 2 times, the relay switches on after a certain time it goes off but then I only have to press the button 1 time to turn the relay on and I want like that you always have to press the button twice to switch on the relay

Did you change any of the code or your circuit ?

Note the use of INPUT_PULLUP in pinMode() which means that a state of LOW means that the button is pressed, the need for the circuit to take the pin LOW when the button is pressed and the fact that LED turns on when its pin goes HIGH whereas it is very likely that your relay turns on when its input goes LOW

Look at the principle of my sketch rather than expecting it to be a direct solution to your requirements

Hello,
insert a small delay to debounce the input and a solution for the detecting of the switch transition .

where should I place the delay?

I have not adjusted anything, sometimes I have to push once and sometimes 2 times

think about it :slight_smile:

I dont know:(