5V DC Single ch relay turning On but not getting turned off(solved)

I am designing a project using relay and submersible pump(currently not connected will be at Normally Open) in esp-12f where it should turn on after 1second and turn off after 1 minute, the relay is connected to GPIO5 and powered by breadboard power supply MB102.But don't know why it turns on but won't turn off and stay as it is.. kindly guide

int passflag = 0; // to let the loop execute only once 
const int relayInput = 4; // the input to the relay pin
unsigned long time1 = 0;
boolean relayStatus = false;
unsigned long turnOnDelay = 1000;
unsigned long turnOffDelay = 60000;
 
void setup() {
  pinMode(relayInput, OUTPUT); // initialize pin as OUTPUT
  digitalWrite(relayInput, HIGH);
  
}
 
void loop() {
  
  if ((passflag==0 && relayStatus == false) && (millis() - time1 >= turnOnDelay)) {
    time1 = millis();
    digitalWrite(relayInput, HIGH); // turn relay on
    relayStatus = !relayStatus;
    passflag++;
  }
 
  else if ((passflag==0 && relayStatus == true) && (millis() - time1 >= turnOffDelay)) {
    time1 = millis();
    digitalWrite(relayInput, LOW); // turn relay off
    relayStatus = !relayStatus;
    passflag++;
  }
}

don't see how passFlag gets set to zero which prevent either condition from being true

aruneshdutta:
halting using passflag, hope that's fine.

if passFlag is used to control when the pump is allowed to cycle, why is it being set by this code? or should is be tested for > 0 instead of == 0?

Does your relay turn on with a HIGH or LOW?

the relay is connected to GPIO5

??const int relayInput = 4; // the input to the relay pinOnce the first if condition is met

if ((passflag==0 && relayStatus == false) && (millis() - time1 >= turnOnDelay)) {
    time1 = millis();
    digitalWrite(relayInput, HIGH); // turn relay on
    relayStatus = !relayStatus;
    passflag++;

  }

passFlag never returns to zero, so neither of the if statements are going to be true. Actually i suggest to just remove passFlag all together.
Still if i would want something to be turned on for 1 second every minute, i would use the modulo 60000 of millis() and turn on if lower than 1000, else turn off.

It's working now used a low level relay trigger

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