Water level controller help

Hi, i want to write a code for fluid level controller as shown in the attachment such that the common line will be output 5v. When the liquid level falls below low level(when its not 5v), pump output should turn on but it should not turn off untill it reach high level. From the circuit viewpoint id need an SR flipflop, is there any way to code this in an arduino. Please help

Untitled drawing.jpg

I would use two water proof float switches.
You can monitor them with Arduino inputs, see digitalRead()

.

just write your code to say pump on if water low and not high , otherwise off .

Look for something like this as larryd said.

http://www.dwyer-inst.com/Product/Level/LevelSwitches/Float/SeriesCFS2

OMR

Beware of just sticking electrodes in the water as your image suggests: putting 5V across an electrode vs the common ground (metal container?) gives you electrolysis, and possibly severe corrosion. You’d have to go to much lower voltages, like <0.5V, to prevent this from happening. It also only works if your water has sufficient conductivity.
Float switches are a much better way, or a contactless system like ultrasound (using e.g. the popular HC-SR04 sensor - just make sure it stays dry as the sensor is not water proof).

Thank you all for your response. About electrolysis, i use magnetic reed switch inside a pipe with a ring magnet floating to sense the level, i think it should be fine. Sorry that i oversimplify the drawing. My problem about the code is when i use while loop and turn the pump output on when 'low level' is low, as soon as the water reach the 'low level' and it outputs high the pump output turns off again. I want the pump output to be high untill the water level reaches the high level like an SR latch would

Well, the code is easy to do, in your loop you need something like:

if (digitalRead(lowPin)) digitalWrite(pumpPin, HIGH);
if (digitalRead(highPin)) digitalWrite(pumpPin, LOW);

lowPin is pulled high when the low level reed is triggered (one or more times, doesn't matter); highPin is pulled high then the high level reed is triggered. Now the moment you reach the low level the pump is switched on, then when the high level switch is triggered it switches off again. I assume pin HIGH means pump on, and reed triggered. This of course may be reversed.

You'll have to build in some fail safes: what happens if for some reason your magnet is stuck at a low level and your high level is never triggered? You have to be really sure your magnet will continue to move up and down with the water.

Hi,

Can you please post your code for us thanks.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Also can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
We need to see how you have wired your level switches.

Thanks.. Tom... :slight_smile:

“You’ll have to build in some fail safes: what happens if for some reason your magnet is stuck at a low level and your high level is never triggered? You have to be really sure your magnet will continue to move up and down with the water.”

Yes, always design a fallback circuit in the event the controller or other fails.
Water overflow can be disastrous.

.

larryd:
Yes, always design a fallback circuit in the event the controller or other fails.
Water overflow can be disastrous.

I would prefer to use a single sensor without moving parts (that can get stuck) to measure the water level. Pressure at the bottom, ultrasound or even LiDAR from above - depending on the size of the tank.
If that sensor breaks, well, no water will be added (do make sure your system realises the sensor is broken and is not putting out a very low water level that needs to be corrected by opening the tap).
For the same reason your solenoid should be active open, so it closes if power is out.

wvmarle:
I would prefer to use a single sensor without moving parts (that can get stuck) to measure the water level. Pressure at the bottom, ultrasound or even LiDAR from above - depending on the size of the tank.
If that sensor breaks, well, no water will be added (do make sure your system realises the sensor is broken and is not putting out a very low water level that needs to be corrected by opening the tap).
For the same reason your solenoid should be active open, so it closes if power is out.

I prefer to have two sensors circuits.
One fails maybe the other works.
After working with SCADA for 45 years you always plan for failure if there is a chance of damage.

Of course, you ckeck circuitry routinely.

.

.

Like if the backup fails but it's still working just fine on the primary, nobody notices until the primary fails too.

That's basically what happened at Chernobyl.

Yes the tanky have fail-safe already, an overflow pipe to flow back to where its pumped from. My problem is the code. Here is my code

const int lowSense = 2;
const int highSense = 3;
const int pumpOut =  13;
int lowState = 0;  
int highState = 0;
void setup() {
  pinMode(pumpOut, OUTPUT);
  pinMode(lowSense, INPUT);
  pinMode(highSense, INPUT);
}
void loop() {
  lowState = digitalRead(lowSense);
  if (lowState == LOW) {
    digitalWrite(pumpOut, HIGH);
  } else {
    digitalWrite(pumpOut, LOW);
  }
}

My code runs the pump as soon as its below low level, but it turns off again after the low level is filled again. Is it possible when the lowState is low the pump turns on but doesnot turns off untill it reach high. For now it just works as it the low level is a switch and whenever it level changes it turns off again

Make a pumpFlag variable.

When the low level switch is detected, set pumpFlag = true
When the pumpFlag == true turn on the pump.

When the high level switch is detected, reset pumpFlag = false
When the pumpFlag == false turn the pump off.

.

Like if the backup fails but it's still working just fine on the primary, nobody notices until the primary fails too.

That's basically what happened at Chernobyl.

Not quite.

A mixture of an unstable reactor design and ghastly operator errors.

The plant was in good working order.

You're thinking of 3-mile island. A plant fault made worse by operator errors.

Allan.

So it seems theres no simple way to code latching circuit for a beginner like me. All i need is one output with two button input A and B. Button A will turn on the output and latch on untill button B is pressed. I think i have to learn more. Thank you all for your response

muanpuia:
So it seems theres no simple way to code latching circuit for a beginner like me. All i need is one output with two button input A and B. Button A will turn on the output and latch on untill button B is pressed. I think i have to learn more. Thank you all for your response

Did you try post #13

Basically :

Turn the pump on when both sensors say low.

Turn the pump off when both sensors say high.

Error - if the upper sensor says high and the lower says low, there's something wrong. Turn off the pump and eg ring a bell to say 'come and fix me'

Allan

I feel that post #13 seems right, but i dont even learn how to set the flag yet, some if and while thats all i know. Would you please write me that code larryd so that i can learn from it, im not sure but it seems like thats does exactly what i want to do

const int lowSense  = 2;
const int highSense = 3;
const int pumpOut   =  13;

void setup()
{
  pinMode(pumpOut,   OUTPUT);
  pinMode(lowSense,  INPUT_PULLUP);
  pinMode(highSense, INPUT_PULLUP);
}

void loop()
{
  //=================================
  if (digitalRead(lowSense) == LOW) //LOW = switch closed (needs water)
  {
    digitalWrite(pumpOut, HIGH);    //pump ON
  }

  if (digitalRead(highSense) == LOW)//LOW == switch closed (no more water)
  {
    digitalWrite(pumpOut, LOW);     //pump OFF
  }
  //=================================

}