IF Statement Repeats/Relay Sometimes Doesn't Deenergize

Hello all,

I am writing a simple program that will allow me to use a push button that will activate a pump (via relay) for a certain amount of time and will pour a shot. The issue right now is that when I run the program, the relay running the pump will sometimes remain on for an additional delay, as if the IF statement is repeating. In fact, when looking at the value of the pushbutton in the Serial monitor, the IF part of the program appears to repeat itself each time this occurs because a new value will be logged for each time the relay clicks and remains on. Sometimes pushing the button has the pump run once, sometimes twice, sometimes three times. What could be causing this? I have tried narrowing down the problem. When observing the button presses only, the button appears to work fine. When I unhook the pump from the relay, the relay works as it should. When I allow the relay to cycle on and off without a button, it also performs correctly. What is going on here?? Here is the code, please help.

int pump1 = 12;
int button1 = 4;
int val = 0;

void setup() {
Serial.begin(9600);
pinMode(pump1,OUTPUT); //set pump pin as output
pinMode(button1,INPUT); //set button pin as input
digitalWrite(pump1,HIGH); //start with pump off
}

void loop() {
val = digitalRead(button1); //read button value
if (val == HIGH) { //check if button if pressed
digitalWrite(pump1,LOW); //turn on pump1
delay(1000); //delay 1 sec
Serial.println(val);
}else {
digitalWrite(pump1,HIGH); //turn pump off
}

}

How did you wire your button - do you have a pull down resistor?

I have tried a pull down and pull up. I will have to check; I believe it is currently pulling up though. I am using a 10k resistor. I have recently read about using a transistor along with the relay (I currently am just running off of my Arduino 5v). Would this cause an issue like this to happen?

Never power a relay/motor from the Arduino 5v.
Use a driver between the Arduino and the relay/motor.

Outputs.jpg

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:

Here is the relay I am using. I believe it is meant to be ran at 5v. Is the transistor’s main purpose to increase the amps? Also, I am currently using the NO on the relay, so it has to be energized to activate. I don’t understand how having an inadequate amount of power would cause my relay to stay activated longer. When it stays activated, the relay also clicks, so I believe it is cycling off and then back on for the delayed amount of time.

Relay:

Here is my arduino board:

https://www.amazon.com/Elegoo-EL-CB-003-ATmega2560-ATMEGA16U2-Arduino/dp/B01H4ZLZLQ/ref=sr_1_4?ie=UTF8&qid=1532114715&sr=8-4&keywords=arduino+mega&dpID=51w0PuG6wHL&preST=SY300_QL70&dpSrc=srch

Button:

Please show a schéma of how your button and everything is wired

When observing the button presses only, the button appears to work fine. When I unhook the pump from the relay, the relay works as it should. When I allow the relay to cycle on and off without a button, it also performs correctly.

So, we are dealing with an electrical fault.

daniel91:
Here is the relay I am using. I believe it is meant to be ran at 5v. Is the transistor's main purpose to increase the amps?

It's to deal with "back-EMF', a voltage spike that occurs when a coil is switched off. And yes, also the amps. What everyone else said: use a driver. I like the ULN2003A http://www.ti.com/product/ULN2003A.