Cant make the relay be turned off

Hello Folks I have some issues I cant make my relay to turn of the connected water pump its always on.

below is how I connected the wires:

So the relay is connected to digital pin 3.
Its quite straight forward the code so please help me out.

int sensor = A0;  //Sets pin  A0 as our sensor input.
int RELAY_0 = 3;  //sets digital pin 2 as our relay control.

unsigned long startBlink = 0UL;  // because millis() returns unsigned long
unsigned long waitBlink = 0UL;   // because millis() returns unsigned long

void setup() {
  Serial.begin(9600);  //starts up the serial monitor.
  pinMode(RELAY_0, OUTPUT);
}

void loop() {

  Serial.println("turn off pump per default");
  digitalWrite(RELAY_0, LOW);  // turn off pump 5 seconds not working.
  Serial.println("run plant zero");
  Plant_zero();
}


void Plant_zero() {
  //sets the relay to cut the power to the track
  int SOIL_MOISTURE_0 = analogRead(sensor);  //Sets valA1 as whatever the sensor reads
  delay(5000);
  Serial.println("read sensor wait..");
  Serial.println(SOIL_MOISTURE_0);

  if (SOIL_MOISTURE_0 < 500) {
    digitalWrite(RELAY_0, LOW);  //sets the relay to cut the power to the track

    delay(3000);
  }

  if (SOIL_MOISTURE_0 > 500) {
    //digitalWrite(RELAY_0, HIGH);  //keeps the power on while the sensor is not tripped
    digitalWrite(RELAY_0, LOW);  //keeps the power on while the sensor is not tripped
    delay(3000);
  }
    delay(86400000UL);
  Serial.println("delay done.");
}

In my Plant_zero function I am away of the if and else return the same outcome just for test-purposes.
but the pump is always on I dont get it Please help me out

I also went back to the basic tested following code:

// constants won't change
const int RELAY_PIN = 3;  // the Arduino pin, which connects to the IN pin of relay

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin as an output.
  pinMode(RELAY_PIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(RELAY_PIN, HIGH);
  delay(500);
  digitalWrite(RELAY_PIN, LOW);
  delay(500);
}

that code works fine with the delay so it seems like everything is connected corretly and responding well.

Thank you

Usually a LOW on these relays turn on that relay.


If you want to de-energize the relay use:

digitalWrite(RELAY_0, HIGH);

You never write a HIGH to the relay.

Oh its inverted? high means off and low // on?

void loop() {
  digitalWrite(RELAY_PIN, HIGH);  // turn relay OFF
  delay(500);
  digitalWrite(RELAY_PIN, LOW); // turn relay ON
  delay(500);
}

Opto-isolators are logic inverters.

Hello xsisec

Design and declare in this way:

enum SwitchState {ON,OFF}; 

instead of HIGH/LOW.

Have a nice day and enjoy coding in C++.

Thanks for all replies:
Is this a ideal way of it?

I want following:

  1. Keep the relay off per default.
  2. Read soil moisture wait some time for the sensor to calculate complete.
  3. Turn on the relay if threshold is over value 500.
  4. Have the relay on for 3 seconds.
  5. Wait 24 hours for next run.
int sensor = A0;  //Sets pin  A0 as our sensor input.
int RELAY_0 = 3;  //sets digital pin 2 as our relay control.

unsigned long startBlink = 0UL;  // because millis() returns unsigned long
unsigned long waitBlink = 0UL;   // because millis() returns unsigned long

void setup() {
  Serial.begin(9600);  //starts up the serial monitor.
  pinMode(RELAY_0, OUTPUT);
  digitalWrite(RELAY_0, HIGH);
}

void loop() {
  Serial.println("Turn off relay");
  digitalWrite(RELAY_0, HIGH);
  Plant_zero();
}


void Plant_zero() {
  digitalWrite(RELAY_0, HIGH);
  int SOIL_MOISTURE_0 = analogRead(sensor);  //Sets valA1 as whatever the sensor reads
  delay(5000);
  Serial.println("read sensor wait..");
  Serial.println(SOIL_MOISTURE_0);
  delay(10000);

  Serial.println("turn on pump");
if (SOIL_MOISTURE_0 > 500) {
    digitalWrite(RELAY_0, LOW);  //sets the relay to cut the power to the track
    Serial.println("delay 3 sec");
    delay(3000);
  }
  Serial.println("Tun on Relay");
  digitalWrite(RELAY_0, HIGH);
  delay(2000);
  Serial.println("Tun off Relay");
  digitalWrite(RELAY_0, HIGH);
  delay(86400000UL);
}

Very happy for all suggestions to improve this.

  1. Turn on the relay if threshold is over value 500.
  • Please confirm a value > 500 means the relay should be ON, water pump ON ?
  • Please confirm a value < 500 means the relay should be OFF, water pump OFF ?

Correct if the value is over 500 turn on. otherwise turn off.

  • Please confirm a value > 500 means the relay/pump should be ON ?

  • Please explain what is happening here ?

  digitalWrite(RELAY_0, HIGH);
  int SOIL_MOISTURE_0 = analogRead(sensor);  //Sets valA1 as whatever the sensor reads
  delay(5000);
  Serial.println("read sensor wait..");
  Serial.println(SOIL_MOISTURE_0);
  delay(10000);

Try this example.

Ask questions if you do not understand things.



#define LEDon                       HIGH
#define LEDoff                      LOW

#define pumpON                      LOW
#define pumpOFF                     HIGH

const byte sensor                 = A0;  

const byte heartbeatLED           = 13;
const byte RELAY_0                = 3; 

int SOIL_MOISTURE_0;

//timing stuff
unsigned long heartbeatTime;
unsigned long machineTime;

unsigned long commonTime;
unsigned long commonInterval;
//const unsigned long pumpOnTime       = 3000ul;                //3 seconds
//const unsigned long pumpRestartTime  = 24ul * 60 * 60 * 1000; //24 hours

//for testing
const unsigned long pumpOnTime       = 5000ul; //5 seconds
const unsigned long pumpRestartTime  = 10000;  //10 seconds

//State Machine stuff
enum MachineStates {Startup, State0, State1, State2};
MachineStates mState = Startup;


//                                      s e t u p ( )
//********************************************^************************************************
void setup()
{
  Serial.begin(9600);  //starts up the serial monitor.

  digitalWrite(heartbeatLED, LEDoff);
  pinMode(heartbeatLED, OUTPUT);

  digitalWrite(RELAY_0, HIGH);
  pinMode(RELAY_0, OUTPUT);

} //END of   setup()

//                                       l o o p ( )
//********************************************^************************************************
void loop()
{
  //**********************************************                h e a r t b e a t   T I M E R
  //is it time to toggle the heartbeatLED ?
  if (millis() - heartbeatTime > 500)
  {
    //restart this TIMER
    heartbeatTime = millis();

    //toggle LED
    digitalWrite(heartbeatLED, digitalRead(heartbeatLED) == HIGH ? LOW : HIGH);
  }

  //**********************************************                m a c h i n e   T I M E R
  //is it time to service the State Machine ?
  if (millis() - machineTime > 1000ul)
  {
    //restart this TIMER
    machineTime = millis();

    checkMachine();
  }

} //END of   loop()


//                               c h e c k M a c h i n e( )
//********************************************^************************************************
void checkMachine()
{
  switch (mState)
  {
    //******************
    case Startup:
      {
        //next state
        mState = State0;
      }
      break;

    //******************
    case State0:
      {
        SOIL_MOISTURE_0 = analogRead(sensor);
        Serial.print("Soil Moisture = ");
        Serial.println(SOIL_MOISTURE_0);

        //is the soil dry ?
        if (SOIL_MOISTURE_0 > 500)
        {
          digitalWrite(RELAY_0, pumpON);
          Serial.println("\nTurning ON the pump");
          Serial.println("Waiting for 3 sec");

          //set the wait time
          commonInterval = pumpOnTime;

          //restart the common TIMER
          commonTime = millis();

          //next state
          mState = State1;
        }
      }
      break;

    //******************
    case State1:
      {
        //has the common TIMER expired ?
        if (millis() - commonTime >= commonInterval)
        {
          digitalWrite(RELAY_0, pumpOFF);
          Serial.println("\nTurning OFF the pump");
          Serial.println("Waiting for 24 hours\n");

          //set the wait time
          commonInterval = pumpRestartTime;

          //restart the common TIMER
          commonTime = millis();

          //next state
          mState = State2;
        }
      }
      break;

    //******************
    case State2:
      {
        //has the common TIMER expired ?
        if (millis() - commonTime >= commonInterval)
        {
          //we will now restart the cycle

          //next state
          mState = State0;
        }
        break;
      }

  } //END of   switch()

} //END of   checkMachine()

//********************************************^************************************************


If that grey box is the pump, it is permanently connected to the batteries, so the relay cannot switch it on/off.

What will happen is that when the relay is activated, it will short-circuit the battery and drain it very quickly. This may also make the batteries and wires hot. It will stop the pump from running, but not in a good way.

Oh I see many thanks for clarification!

The code is itself is intact back the behavior is not working.
Pump is always on also the relay is toggling every second.

did you change the circuit, or are you still shorting the battery with the relay?

How should the circuit be instead?
Please help me out. :blush:

The relay should be used to interrupt the current to the motor, not short circuit the motor/battery. That's how your light switch in your house works. It doesn't short circuit the grid, it opens the circuit to the light.

Hi,

The relay contacts should be in series with the power supply to the pump.

Tom.. :smiley: :+1: :coffee: :australia:

Do you understand what is happening in the sketch.