Arduino - Water Sensor Relay

In this tutorial, we will learn how to use an Arduino to activate a relay when it detects water. Then you can connect the relay to a pump, or a siren, buzzer to make a water alarm.

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Water level sensor
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) 12V Pump
1×(Optional) 12V Active Buzzer
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About Water Sensor and Relay

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

How It Works

Arduino periodically reads the value from the water sensor. If the value exceeds a pre-defined threshold, the Arduino activates the relay; otherwise, it deactivates the relay.

Wiring Diagram

Arduino water sensor controls relay Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-water-sensor-relay */ #define RELAY_PIN A0 // The Arduino pin that connects to the relay #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 500 // 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); // dectivates relay } 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); // activates relay } else { digitalWrite(RELAY_PIN, LOW); // dectivates relay } delay(1000); }

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
  • Update the value of THRESHOLD in the code to a suitable value for your application
  • Click Upload button on Arduino IDE to upload code to Arduino
Arduino IDE Upload Code
  • Embed the water sensor into the water
  • See the relay'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

※ OUR MESSAGES