Arduino 5V relay connection to 2.5-6V water pump

Hello, I need to design a system for watering the soil based on moisture level. For that, I use 2.5V-6V water pump and 5 V relay. The problem is that the motor keeps running continuously from the first connection. I added the conditions on the code part for controlling its operation, but it is not working. For powering the pump, I use the 9V battery. What could be problem that the pump works continuosly? Battery's + is connected to relay's COM, pump's + to the NC of relay.

Code part:
int relay=8;
void setup()
{
Serial.begin(9600);//baud rate
digitalWrite(relay, LOW);
pinMode(relay, OUTPUT);
}
int read_soil_moisture(int pin)
{
int x = analogRead(pin);
return map(x, 0, 1023, 0, 100);

}

void loop()
{
val0 =read_soil_moisture(soil_pin1);
val1 =read_soil_moisture(soil_pin2);
val2 =read_soil_moisture(soil_pin3);
val3 =read_soil_moisture(soil_pin4);
if ((val0>66 && val1>66) || (val0>66 && val2>66) || (val0>66 && val3>66) || (val1>66 && val2>66) || (val1>66 && val3>66) || (val2>66 && val3>66)) //if statement for any of the 2 sensors are both more than 800 value
{

  digitalWrite(relay, HIGH);
  Serial.println("Pump is ON"); 
}

if (val0<33 && val1<33 && val2<33 && val3<33) {
  digitalWrite(relay, LOW);
  Serial.println("Pump is OFF"); 
  lcd.setCursor(0,1); 
  lcd.print("Pump is CLOSED");


}

}

You have created a switch that is always closed, until you Arduino code opens the switch. Perhaps changing the connection from the NC, normally closed, to the NO, normally open will create a switch that is open until your Arduino code closes the switch.

Looks like you have a "LOW true" relay module (very common), the relay is ON when the output pin is LOW, OFF when pin is HIGH. Put the battery + wire on the relay NO terminal, motor + wire on COM.
Here's your code with reversed logic:

int relay=8;
void setup()
{
Serial.begin(9600);//baud rate
digitalWrite(relay, HIGH);
pinMode(relay, OUTPUT);
}
int read_soil_moisture(int pin)
{
int x = analogRead(pin);
return map(x, 0, 1023, 0, 100);

}

void loop()
{
  
val0 =read_soil_moisture(soil_pin1);
val1 =read_soil_moisture(soil_pin2);
val2 =read_soil_moisture(soil_pin3);
val3 =read_soil_moisture(soil_pin4);
if ((val0>66 && val1>66) || (val0>66 && val2>66) || (val0>66 && val3>66) || (val1>66 && val2>66) || (val1>66 && val3>66) || (val2>66 && val3>66)) //if statement for any of the 2 sensors are both more than 800 value
{

  digitalWrite(relay, LOW);
  Serial.println("Pump is ON"); 
}

if (val0<33 && val1<33 && val2<33 && val3<33) {
  digitalWrite(relay, HIGH);
  Serial.println("Pump is OFF"); 
  lcd.setCursor(0,1); 
  lcd.print("Pump is CLOSED");


}

}

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