Sitemap
Sarah Packowski

Sarah Packowski

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

Did you leave the hose running?

5 min readJul 31, 2022

--

Have you ever lain awake at 3:00 AM wondering: “Did I leave the hose running?” See how to build an IoT solution to let you sleep at night.

The garden hose

Maybe you were watering plants, or filling a trough, or had the hose running to wash something… And then: you got distracted and left the water running. Next thing you know, you’re in line at the grocery store when you realize you forgot to turn off the water.

Press enter or click to view image in full size
I left the hose running!

Water flow sensor to the rescue

Monitoring the value from a water flow sensor can tell us whether the water is running.

Get Sarah Packowski’s stories in your inbox

Join Medium for free to get updates from this writer.

The sensor used in this solution uses the Hall effect. The following video shows how this sensor works:

How a Hall effect water flow meter works

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 our solution:

Press enter or click to view image in full size
Circuit diagram and hardware
Circuit diagram and hardware
  • The red wire of the flow sensor is connected to 5V power
  • The black wire is connected to ground
  • The yellow wire is connected to pin D8*

*In other posts (eg. tilt switch, float switch) a pull-down resistor was used to cause the input pin to read LOW when the sensor switch is open. Here, the solution uses an internal, pull-up resistor that is built into the MKR Wifi 1010 board for pin D8. This causes the input value of the pin to read HIGH when no pulse is coming from the flow sensor, and read LOW when a pulse comes from the sensor. Both pull-down and pull-up resistors can solve the problem of indeterminate values when there is no input signal. In the case of this solution, the built-in resistor is being used for a purely practical reason: to do away with the need for a breadboard and external resistor, which will make it simpler to protect the solution for outdoor use.

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

byte flow_pin = 8;
unsigned int flow_count = 0;
unsigned int prev_count = 0;
unsigned long prev_time = millis();
bool b_wheel_turning = false;
void setup()
{
Serial.begin(9600);
pinMode( flow_pin, INPUT_PULLUP );
attachInterrupt( flow_pin, flowCounter, FALLING );
}
void loop()
{
if( ( millis() - prev_time ) > 1000 )
{
b_wheel_turning = ( flow_count == prev_count ) ? false : true;

prev_count = flow_count;
prev_time = millis();

Serial.print( "flow_count: " );
Serial.println( flow_count );
Serial.print( "b_wheel_turning: " );
Serial.println( b_wheel_turning );
Serial.println( "" );
}
}void flowCounter()
{
flow_count++;
}

First, several global variables are declared:

  • flow_pin refers to the input pin being used, D8.
  • flow_count will be used to count the pulses coming from the flow sensor.
  • prev_count will be used to keep track of the previous value of flow_count, as we keep checking the value of flow_count over time.
  • prev_time will be used to remember what the onboard clock said the last time we checked the value of flow_count.
  • b_wheel_turning is a true-false variable that indicate whether the water is running. This solution doesn’t attempt to measure how much water is flowing, just whether the water is flowing or not.

When the board first starts up, the setup routine does several things:

  • Initializes our serial print for debugging.
  • Sets pin D8 to read input from the flow sensor.
  • Configures the flowCounter routine to be called whenever an pulse is received from the sensor. (See: Interrupts)

Then, the main logic happens in the loop routine over and over:

  • If 1000 milliseconds (1 second) have passed since the flow_count was last checked, go into the logic for checking the value.
  • Set b_wheel_turning to false if flow_count hasn’t changed since the last time we checked. Otherwise, set it to true. (See: Conditional operator)
  • Use prev_count to remember the current value of flow_count and use prev_time to remember the current time.
  • Print some information to the Serial monitor.

The flowCounter routine is called whenever a pulse is received from the flow sensor. All the routine does is increment flow_count.

Testing the solution

When this code is uploaded to the MKR WiFi 1010 board, we can perform a dry test of our solution by using air from a blow dryer to turn the meter wheel.

Watch the serial monitor as air from a blow dryer turns the meter wheel

The basic solution is working. Let’s get the solution working outside and hook up the hose.

Arduino IoT Cloud

The Arduino IoT Cloud platform makes it quick and easy to turn our 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_water_off, 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. Add a new line of code to loop that sets b_water_off equal to the opposite value of b_wheel_turning.
  6. Create a dashboard with one widget: a “Status” widget, linked to the variable b_water_off, with labels ✓and ×.

Conclusion

Now, whether I’m in line at the grocery store or lying awake at 3:00 AM, I can tell whether the water is running by viewing the dashboard on a mobile device.

Press enter or click to view image in full size
Water running, dashboard red
Water running, oh no! Dashboard red.
Press enter or click to view image in full size
Water not running, dashboard green
Water not running, we’re good. Dashboard green.

--

--

Written by Sarah Packowski

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