Keep water from overflowing with a float sensor

Sarah Packowski
5 min readMay 29, 2022

Did you ever overflow a water trough? See how to build an IoT solution so you won’t waste water and make a mess again.

The trough I don’t want to overflow

One of our horses is insulin resistant, so we soak sugars out of his hay before we feed him. To make things easier, we set up an IoT solution that automatically fills the water before we start chores. We do this in the garage, so we don’t want gallons of water going everywhere! We use a float sensor (float switch) to know when the water is full.

Float switch as water fills the trough

One float switch, many advantages

At first, I used a timer to turn the water off (I timed how long it generally took to fill and then set the timer to that.) Adding a float switch is much better:

  • Every hay bale has a different volume, so the float switch is a more accurate way to know when the water is full.
  • I’ve manually started a new fill-and-soak cycle when the trough is already full of water more than once. 😳
  • If, for some reason, the trough is full of water when an automatic fill-and-soak cycle is scheduled, the new cycle can be automatically cancelled.
  • Having the float switch on a different circuit from the water-filling circuit adds redundancy to the solution.

Arduino

The Arduino platform is:

An open-source electronics platform based on easy-to-use hardware and software. It’s intended for anyone making interactive projects.

Hardware

Here are the components needed for the solution:

  • Breadboard for easily connecting the circuit.
  • Float switch (When the hollow, outer ring — which contains a magnet — is resting at the bottom of the central stem, the reed switch is closed*. When the ring is buoyed up by the water, the switch opens.)
    *For this project, I configured the float switch to be normally closed like this. You can configure the float switch to be normally open by manually inverting the outer ring on the stem.
Float switch
How the float switch behaves when the trough is empty (left) and when the trough is full of water (right)
  • 10 kilohm pull-down resistor (This configuration causes the value of the float switch input pin to read LOW when the float switch is open. Otherwise, the value for the input pin would vary randomly when the switch is open.)
  • Arduino MKR WiFi 1010 board to connect to the internet.
Circuit diagram and hardware
Circuit diagram and hardware
  • One lead of the float switch is connected to 5V power.
  • The other lead of the float switch is connected to the 10 kilohm resistor and then to ground.
  • At the conjunction of the float switch and the resistor, pin D6 is connected.
  • *For local testing, when you send power to pin D6 on the Arduino MKR 1010 board, the built-in LED on the board lights up.

Software

You program your Arduino board by writing C code and then uploading it to the board using the Arduino integrated development environment (IDE).

The open-source Arduino Software (IDE) makes it easy to write code and upload it to the board. This software can be used with any Arduino board.

Sketch (code) for local testing

int float_pin = 6;
int float_state = 0;
void setup()
{
Serial.begin(9600);
pinMode( float_pin, INPUT );
}
void loop()
{
float_state = digitalRead( float_pin );
Serial.println( float_state );
delay( 1000 );
}
  • When the board first starts up, the setup routine initializes our serial print for debugging and sets pin D6 to read input from the float switch.
  • Then, the main logic happens in the loop routine over and over: read in the state of the float switch, write that state information out to the serial monitor, wait one second before repeating again.

Testing the solution

When this code is uploaded to the MKR WiFi 1010 board:

  • The light on the board turns on when the ring on the float switch is sitting at the bottom (left below)
  • And the light turns off when the ring on the float switch is pushed up (right below)
Trough empty: light on. Trough full: light off.
Left: Trough empty, light on. Right: Trough full, light off.

So, the basics of the solution are worked out! However, that tiny light isn’t going to help me avoid overflowing the water…

Arduino IoT Cloud

The Arduino IoT Cloud platform makes it quick and easy to turn our float switch solution into an Internet of Things solution. The graphical web interface guides you, step by step, through the process:

  1. Add a Device for the MKR 1010 board in the solution.
  2. Create a Thing linked to the device, and follow the prompts to connect it to your wifi.
  3. Add a variable to the Thing: a read-only, boolean variable, named b_empty, that is updated only when it changes.
  4. Open the Thing’s default Sketch in the full web editor, and add your code for setup and loop to the default code.
  5. Use the new variable b_empty instead of float_state.
  6. Create a dashboard with one widget: a “Status” widget, linked to the variable b_empty, with labels ✓and ×.

Conclusion

Now, when the IoT-enabled float switch solution is attached to the trough at just the right height, I can tell whether the trough is empty by checking the dashboard from a mobile device.

Trough full of water, dashboard red
Trough full of water, dashboard red
Trough empty, dashboard green
Trough empty, dashboard green

--

--

Sarah Packowski

Design, build AI solutions by day. Experiment with input devices, drones, IoT, smart farming by night.