Arduino - Water Sensor Pump

In this tutorial, we will learn how to use an Arduino to activate a pump for draining water when it is detected by a water sensor. In other words, we will be discovering how to create an automatic drainage system using an Arduino, a water sensor, and a pump

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Water level sensor
1×12V Pump
1×Vinyl Tube
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Optional) Screw Terminal Block Shield for Arduino Uno

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About Water Sensor and Pump

If you do not know about water sensor and pump (pinout, how it works, how to program ...), learn about them in the following tutorials:

How the Automatic Drainage System Works

Arduino periodically reads the value from the water sensor, then:

  • If the value is greater than a pre-defined threshold, Arduino turns on the relay to activate the pump.
  • Otherwise, Arduino turns off the pump.

Wiring Diagram

Arduino water sensor controls Pump Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code

/* * Created by * * This example code is in the public domain * * Tutorial page: */ #define RELAY_PIN A0 // The Arduino pin that connects to the relay to control the pump #define POWER_PIN 7 // The Arduino pin that provides the power to the water sensor #define SIGNAL_PIN A5 // The Arduino pin that reads the value from the water sensor #define THRESHOLD 300 // The threshold for water detectiion void setup() { Serial.begin(9600); pinMode(RELAY_PIN, OUTPUT); // configure D2 pin as an OUTPUT pinMode(POWER_PIN, OUTPUT); // configure D7 pin as an OUTPUT digitalWrite(POWER_PIN, LOW); // turn the water sensor OFF digitalWrite(RELAY_PIN, LOW); // turn the pump OFF } void loop() { digitalWrite(POWER_PIN, HIGH); // turn the water sensor's power ON delay(10); // wait 10 milliseconds int value = analogRead(SIGNAL_PIN); // read the analog value from sensor digitalWrite(POWER_PIN, LOW); // turn the water sensor's power OFF if (value > THRESHOLD) { Serial.print("The water is detected"); digitalWrite(RELAY_PIN, HIGH); // turn the pump ON } else { digitalWrite(RELAY_PIN, LOW); // turn the pump OFF } delay(1000); // pause for 1 sec to avoid reading sensors frequently to prolong the sensor lifetime }

Quick Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
Arduino IDE Upload Code
  • Embed the water sensor into the water
  • See the pump's state

Code Explanation

Read the line-by-line explanation in comment lines of source code!

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit