Did you leave the hose running?
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.
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:
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:
- Water flow meter for sensing when water is flowing
- Arduino MKR WiFi 1010 board to connect to the internet
- 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_pinrefers to the input pin being used, D8.flow_countwill be used to count the pulses coming from the flow sensor.prev_countwill be used to keep track of the previous value offlow_count, as we keep checking the value offlow_countover time.prev_timewill be used to remember what the onboard clock said the last time we checked the value offlow_count.b_wheel_turningis 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
flowCounterroutine 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_countwas last checked, go into the logic for checking the value. - Set
b_wheel_turningto false ifflow_counthasn’t changed since the last time we checked. Otherwise, set it to true. (See: Conditional operator) - Use
prev_countto remember the current value offlow_countand useprev_timeto 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.
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:
- Add a Device for the MKR 1010 board in the solution.
- Create a Thing linked to the device, and follow the prompts to connect it to your wifi.
- Add a variable to the Thing: a read-only, boolean variable, named
b_water_off, that is updated only when it changes. - Open the Thing’s default Sketch in the full web editor, and add your code for
setupandloopto the default code. - Add a new line of code to
loopthat setsb_water_offequal to the opposite value ofb_wheel_turning. - 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.








