In this project, we’ll be making an automated water filling system for an aquarium, a swimming pool or a roof top hot water reservoir. In this example we’ll just imagine that we got a water tank that needs a refill once in a while.

The system consists of a power valve and a water level sensor. When the sensor does not sense water, the system will open the valve to let water flow through the valve. As the water level rises to the level at which the sensor is,  the sensor will report this and the power valve will be closed effectively preventing water from flowing.

Important: The working principle described here would probably be viable for other fluids than pure water but make sure the valve and sensor can withstand the kind of fluid you’ll be using and DO NOT USE WITH PETROL, ALCOHOL OR OTHER INFLAMMABLE FLUIDS!

We’ll be using a water level sensor with a flotation device containing a magnetic switch fitted inside a waterproof casing. The floater contains a magnetic ring that, when in a certain position, will make the switch open or close.

Below is an image of the sensor we’ll be using:

The floater is the marshmallow looking cylinder and the switch is hidden inside the other half of the sensor. A sensor like the one above typically sells for about 3 US dollars.

We’ll also be using a power valve for opening and closing the water inlet. In this case, we are going to use a valve containing a solenoid that will operate the water valve.

Below is a picture of the power valve that I used in this example:

A valve like the one depicted above can be purchased for approximately 6 US dollars. As this particular valve needs 12V which the PeekyPokey cannot supply directly, we’ll be using a relay to control the valve. There’s a separate example project on how to control a relay where its connectors are also outlined and explained.

The setup
This is what the system looks like when everything is wired up. Since I don’t have access to water in my lab, you’ll have to imagine the water hose and the tank. We are using a 12V AC/DC wall adapter which is not depicted in itself.

Look carefully at the image above and you'll notice there’s a diode in parallel with the +12V power supply connector.

About the diode
In this project, the diode is there to prevent back-voltage from the coil in the solenoid. If omitted you will send spikes back to your power supply which could potentially damage it or other things. The basic functionality of a diode is that it will only conduct current in one direction; from the anode to the cathode.

This is what your typical diode looks like:

You see the gray stripe on the otherwise black body of diode? That stripe marks the cathode.

When fitting the diode in this project, make absolutely sure you get the polarity right since this is kind of crucial  here. The cathode (gray stripe) must be facing the positive side (plus sign) of your 12V power supply. If you get it wrong, you will short circuit your wall adapter causing damage or even fire to it.

When getting a diode for this project, make sure you get one that can withstand at least 12V since that is what you will apply to it. Please note that 12V is not harmful to the human body so there’s no reason for alarm; just make sure you get the diode right, that’s all.

Enough said about the diode, you can read more on diodes and how they work on Wikipedia.

Below is how to wire the setup in terms of which pairs of connections to make.

PeekyPokey 12V adapter Valve Relay Sensor Description
gp7      IN1   Relay control
Vcc      Vcc   Power
GND      GND   Common ground
+5V       1st wire (doesn't matter which)
gp0       2nd wire (other wire)

+12V 1st pole  

GND   NO   Screw terminal
    2nd pole COM   Screw terminal

Above, empty positions mean there are no wires to be connected between the corresponding locations. Hopefully it should be clear enough, at least when compared to the full system image above.

The code
We’ll need a little application logic to control the system. In this simple example, the valve will open immediately as the sensor reports “no water” and it will close equally immediately when there is enough water. Here’s what to do next:

  1. Start Visual Studio
  2. Choose to create a new Console Application
  3. Add a reference to PeekyPokey.dll
  4. In “Program.cs”, replace existing code with the code below 
using System;
using PeekyPokey;

namespace WaterFiller
    static class Program
        private static OutputPort _powervalve;

        static void Main()
            // create a digital output for the power valve
            _powervalve = new OutputPort(Device.Pin.Gpio7, true);

            // create a digital input for the sensor
            var levelsensor = new InputPort(Device.Pin.Gpio0, Port.Trigger.Toggle);

            // attach a handle to detect when sensor output changes
            levelsensor.OnValueChanged += new Port.ValueChanged(levelsensor_OnValueChanged);

            // main thread lies dormant...

        static void levelsensor_OnValueChanged(Port sender, Port.ValueChangedEventArgs e)
            // open or close valve according to the sensor
            _powervalve.Value = !e.Value;

In a real world scenario, you would probably implement some kind of filter or delay logic to make sure the water level is stable before opening or closing the valve. As the application works now, the system could start to oscillate which would produce noise and eventually wear out the relay and valve.

Before hitting the run button in Visual Studio, make sure you put the PeekyPokey power selector switch in the +5V position or you may damage your board.

Note: The kind of solenoid based power valve used in this example tends to get fairly hot at times so be careful when touching the valve with you bare hands. Also, make sure your 12V wall adapter can supply enough current for the power valve – the valve I’m using requires about 500mA (0.5A) which is quite substantial.

You probably want to do some “dry swimming” tests before actually connecting a live water hose to your system. You can verify correct operation by blowing into the valve pipe while using you hand to tinker with the water sensor.

Why not make a really geeky beverage filling system for your next party? Guests could get one unique RFID tag each to activate the filler which would open the valve for a certain fixed duration corresponding to the time it takes to fill up a fixed sized cup. You could implement a “servings per person” limiter or use the system for logging individual consumption :-)

Last edited Sep 8, 2013 at 8:38 PM by hanzibal, version 10