Board keeps resetting due to relay

Hi all, I have a problem with my code, my Arduino Mega Keeps resetting itself at random times throughout the loop. I have narrowed the problem down to the relay which powers a water pump. However, I cannot determine what it is about the relay which makes it reset.

Here is my code:

int inl=52;
#define EN        8  

//Direction pins
#define X_DIR     5 
#define Y_DIR     6
#define Z_DIR     7

//Step pins
#define X_STP     2
#define Y_STP     3 
#define Z_STP     4 


//DRV8825
int delayTime=50; //Delay between each pause (uS)
int stps=5000;


void step(boolean dir, byte dirPin, byte stepperPin, int steps)

{

  digitalWrite(dirPin, dir);

  delay(100);

  for (int i = 0; i < steps; i++) {

    digitalWrite(stepperPin, HIGH);

    delayMicroseconds(delayTime); 

    digitalWrite(stepperPin, LOW);

    delayMicroseconds(delayTime); 

  }

}

void setup(){
  pinMode(X_DIR, OUTPUT); pinMode(X_STP, OUTPUT);
  pinMode(Y_DIR, OUTPUT); pinMode(Y_STP, OUTPUT);
  pinMode(Z_DIR, OUTPUT); pinMode(Z_STP, OUTPUT);
  pinMode(EN, OUTPUT);
  digitalWrite(EN, LOW);
  pinMode(inl, OUTPUT);
  pinMode(A8, INPUT);
  pinMode(A9, INPUT);
  Serial.begin(115200);
}

void loop(){
  int sensorValue = analogRead(A8);
  Serial.println(sensorValue);
   int value = analogRead(A8);
   if (value > 600)
      digitalWrite(inl, HIGH);
        delay(1000);
      digitalWrite(inl, LOW);
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
      digitalWrite(inl, HIGH);
        delay(1000);
      digitalWrite(inl, LOW);
  step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
  step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
  step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
  step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
  //Machine is Home
  delay(100);
 int Sensorvalue = analogRead(A9);
 Serial.println(Sensorvalue);
  int Value = analogRead(A9);
  if (Value > 600)
 step(false, Z_DIR, Z_STP, stps); //Z, Clockwise
 step(false, Z_DIR, Z_STP, stps); //Z, Clockwise
 step(false, Z_DIR, Z_STP, stps); //Z, Clockwise
 step(false, Z_DIR, Z_STP, stps); //Z, Clockwise
     digitalWrite (inl, HIGH);
      delay(1000);
     digitalWrite (inl, LOW);
 step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
 step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
 step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
 step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
     digitalWrite (inl, HIGH);
      delay(1000);
     digitalWrite (inl, LOW);
 step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
 step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
 step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
 step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
 step(true, Z_DIR, Z_STP, stps); //Z, Anti Clockwise
 step(true, Z_DIR, Z_STP, stps); //Z, Anti Clockwise
 step(true, Z_DIR, Z_STP, stps); //Z, Anti Clockwise
 step(true, Z_DIR, Z_STP, stps); //Z, Anti Clockwise
 //Machine is Home
 delay(100);
}

My project is a little machine that waters plants using stepper motors.
Whenever I remove

digitalWrite (inl, HIGH);
delay(1000);
digitalWrite (inl, LOW);

the machine works perfectly fine with no issues however, the water pump is obviously not working.

Would anyone have any idea as to why the board is resetting?
Thanks in advance

Show your wiring. How are you powering this relay?

 if (value > 600)
      digitalWrite(inl, HIGH);
        delay(1000);
      digitalWrite(inl, LOW);

How many of those lines were you intending for that if statement to control? As written it only controls the first one. The delay and the write to LOW happen regardless of the value.

Thanks for the reply, I will draw a schematic of it in a moment, that if statement is meant to control:

     digitalWrite(inl, HIGH);
        delay(1000);
      digitalWrite(inl, LOW);
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
      digitalWrite(inl, HIGH);
        delay(1000);
      digitalWrite(inl, LOW);
  step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
  step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
  step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
  step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
  //Machine is Home
  delay(100);

Is this where the problem is?

Well you definitely coded that wrong then. If you want an if statement to control multiple lines then you need to use braces { and } around the code you want in that block.

Delta_G:
Show your wiring. How are you powering this relay?

So it should look like this?

{
    int sensorValue = analogRead(A8);
    Serial.println(sensorValue);
    int value = analogRead(A8);
    if (value > 600)
      {
       digitalWrite(inl, HIGH);
        delay(1000);
      digitalWrite(inl, LOW);
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
      digitalWrite(inl, HIGH);
        delay(1000);
      digitalWrite(inl, LOW);
  step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
  step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
  step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
  step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
  //Machine is Home
  delay(100);
   }
}

No, more like this:

    int sensorValue = analogRead(A8);
    Serial.println(sensorValue);
    int value = analogRead(A8);
    if (value > 600)
{
      digitalWrite(inl, HIGH);
        delay(1000);
      digitalWrite(inl, LOW);
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
      digitalWrite(inl, HIGH);
        delay(1000);
      digitalWrite(inl, LOW);
  step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
  step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
  step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
  step(true, Y_DIR, Y_STP, stps); //Y, Anti Clockwise
  //Machine is Home
  delay(100);
}

except that you should learn to use the autoformat function of the IDE. This is CTRL-T on a PC.

Learning basic coding in C/C++ would also be helpful.

I am also a believer in using millis() and not using delay(...), but neither is useful until you learn how to write better code.

Here is how it is wired up

I don't see a common (signal return) wire from relay module to Arduino, can you post a link to that relay module? Also don't see a kickback diode across the motor, THAT could be the cause of resets. A 1N4004 diode connected across the motor leads (cathode (end with stripe) toward +) would suppress spikes from motor switching, also, a 0.1 uF ceramic capacitor in the same place would help with brush noise.
EDIT: Also don't see a common from moisture sensors.

(deleted)

Put a flyback diode across the relay. That is - one that is reversed across the coil pins.

Relay coils are a heavy inductive load. Without a flyback diode, when the coil is turned off there will be a spike of voltage much like that from an ignition coil in a car which can fry (or if you are lucky, reset) the Arduino. This voltage has a reverse polarity to the applied voltage, so a reversed diode shorts it out.

flyback.png

I have worked out that it must be a spike from when the relay is turned off, I unplugged the pump from the circuit and still had the relay working (just without the pump) and the board no longer reset itself. So it must be due to the spike in voltage as suggested.

If I put the diode in the circuit like @PaulMurrayCbr suggested wont the circuit just short out the whole time?

Thanks for helping find the problem tho, much appreciated

Not as long as you DO NOT reverse the motor.

@PaulMurrayCbr said to put the diode across the coil of the relay. Your latest statement alludes the problem is from the pump motor. Definitely pump and keep the diode (reversed biased) across the relay coil. Try adding one to the pump (coil contact side) as well. If the diode does not work, you make have to look into a snubber circuit, using a capacitor/resistor/diode combination. I ran into this problem a few projects back, and the pump was run from a completely power source that was completely isolated (other than same 120V source) from the arduino. Motors can generate some significant EMF.

Eveleigh:
If I put the diode in the circuit like @PaulMurrayCbr suggested wont the circuit just short out the whole time?

You reverse the diode. It doesn't short out the current you send to the coil, it shorts out the back-emf from the coil when the coil is turned off.

A long with back-EMF, a second problem is inrush current. When you first turn the relay on, for a fraction of a second an ideal coil has zero resistance (effectively) and can short out the system. Not sure how to deal with that. But the coil itself has a fair bit of ohmic resistance … so maybe it isn't really a problem.

A capacitive load tends to have a large inrush current. A coil has inductance so the current tends to ramp up over time from zero. The current becomes very large after an amount of time that depends upon the coil and other circuit components.

vaj4088:
A capacitive load tends to have a large inrush current. A coil has inductance so the current tends to ramp up over time from zero. The current becomes very large after an amount of time that depends upon the coil and other circuit components.

Yep, I had it the wrong way around. Duh.