Two Float Switches and a Pump

Hello all,
I'm working on a project to pump out a bucket when it gets full. I have two float switches (reed switches), one at the top and one at the bottom (so I don't burn out the pump), and an IOT relay that my pump plugs into. So far I am struggling with the logic and thought this forum might be able to help. Sure I could use one float switch and a timer, but I have two switches and would like to use them.

Logic:
When the top float switch is triggered (HIGH, bucket full), the pump should run until the bottom switch is NOT triggered (LOW, Inch or so of water left in bucket).

Here is my current bit of code that doesn't quite get me to what I'm trying to do. It performs well until the top float switch is triggered then the pump starts running and never turns off when the bottom float switch goes LOW. The LED turns on every time the pump is on.

//Float Switch
const int pinTopFloatSwitch = 5; //Pin Reed
const int pinBtmFloatSwitch = 4;//Pin Reed
const int pinLed = 13; //Pin LED
int TFS = 0;
int BFS = 0;

// Relay
#define relaypower 7 //Define relay power signal

void setup()

{

pinMode(pinLed, OUTPUT); //set up LED
pinMode(pinTopFloatSwitch, INPUT);//setup top float switch
pinMode(pinBtmFloatSwitch, INPUT);//setup bottom float switch
pinMode(relaypower, OUTPUT);//setup relay trigger
Serial.begin(9600);//print to serial

}

void loop(){


TFS = digitalRead(pinTopFloatSwitch); //
BFS = digitalRead(pinBtmFloatSwitch); //

if (TFS == HIGH) {
  do{
    digitalWrite(relaypower, HIGH);
    digitalWrite(pinLed, HIGH);
    Serial.println("Pump is Running");
    delay(500);
    } while (BFS == HIGH);
}
if (TFS == LOW && BFS == LOW){
  digitalWrite(relaypower, LOW);
  digitalWrite(pinLed, LOW);
  Serial.println("Both switches off, pump not running");
  delay(500);
}
else {
  digitalWrite(relaypower, LOW);
  digitalWrite(pinLed, LOW);
  Serial.println("else");
  delay(500);
}
}

Other things I've tried:

You need to digitalRead the low float in your do while loop.

1 Like

Sorry, I'm going to ask you to elaborate a little bit more on that. I thought that defining BFS as digitalRead would work in this context. From this example, I thought I had written the do...while statement correctly, though I do agree the issue is probably in that vicinity.

It's a common misconception. You didn't define BFS as digitalRead, you read the value of the pin once and stored the result in BFS.

Then your loop runs pumping water until BFS is LOW, which it never will be because you never read the pin again. BFS is just a variable, it isn't tied to digitalRead by your earlier assignment.

1 Like

I see! Thanks for spelling that out for me. That worked. I changed my while statement to :

while (digitalRead(pinBtmFloatSwitch) == HIGH);

and that did it.

Be very careful while testing to see if any turbulence/waves in the container cause multiple reed switch open/closures.My irrigation water storage system uses the opposite logic and the container is filled switch goes on and off at the point the containers are filled due to the well water coming into the tank. Had to watch for the FIRST switch closure, not just BEING closed.
Good luck,
Paul

Hi,
Have you got pullup or pulldown resistors on your reed switches?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

You could do all this with just a relay - when it reaches to top , operate relay and latch in with spare contact . When it reaches the bottom , break the latch .
Relay also to power the pump .
Simple reliable , no power supply, Arduino, codes or loads of wires needed

Have you got pullup or pulldown resistors on your reed switches?

Yes pull-up resistors

You could do all this with just a relay - when it reaches to top , operate relay and latch in with spare contact . When it reaches the bottom , break the latch .
Relay also to power the pump .
Simple reliable , no power supply, Arduino, codes or loads of wires needed

You're probably right... wish I had thought of that sooner...