Arduino - Water Detector / Sensor
Arduino - Advanced Water Detection
Section titled “Arduino - Advanced Water Detection”A water sensor is a simple device for detecting the presence of water, useful for projects like rain detectors, leak alarms, or plant watering systems. While it can be used for a simple wet/dry detection, its true potential is unlocked when treated as an analog device.
This updated tutorial will show you how to connect a water sensor to an Arduino’s analog input. This allows you to measure how much water is detected, not just its presence. We will also discuss the sensor’s major limitation—corrosion—and a technique to extend its lifespan.
Required Components
Section titled “Required Components”- 1 × Arduino Uno R3 (or similar)
- 1 × Resistive Water Sensor
- 1 × LED
- 1 × 220 Ohm Resistor
- 1 × Breadboard and jumper wires
Circuit and Connections
Section titled “Circuit and Connections”The sensor has three pins: Signal (S), VCC (+), and GND (-). We will connect the Signal pin to an analog input to read the varying resistance.
Sensor Pin Arduino Pin----------------------------------- S -----------> A0 + -----------> 5V - -----------> GND
LED Anode (+) ----> (220 Ohm Resistor) ----> Pin 9LED Cathode (-) --> GNDImproved Arduino Sketch
Section titled “Improved Arduino Sketch”This sketch reads the analog value from the sensor and uses a threshold to determine if water is present. It prints the raw sensor values to the Serial Monitor, which is essential for calibration.
/* * Advanced Water Detector * * This sketch reads a water sensor via an analog pin to get a variable * reading of water presence. It also prints the value to the Serial Monitor * for easy calibration. */
// --- Pin Definitions ---const int waterSensorPin = A0; // Sensor's S pin connected to Analog 0const int ledPin = 9; // LED indicator pin
// --- Calibration ---// This threshold determines at what sensor value we consider it "wet".// You will likely need to adjust this value based on your sensor and setup.// Check the Serial Monitor to find a good value.const int waterThreshold = 400;
void setup() { // Initialize Serial Communication for debugging Serial.begin(9600);
// Set pin modes pinMode(ledPin, OUTPUT);}
void loop() { // Read the analog value from the sensor (will be 0-1023) int sensorValue = analogRead(waterSensorPin);
// Print the value to the Serial Monitor for debugging/calibration Serial.print("Sensor Value: "); Serial.println(sensorValue);
// Check if the sensor value exceeds our threshold if (sensorValue > waterThreshold) { // Water is detected! digitalWrite(ledPin, HIGH); Serial.println("Status: Water Detected"); } else { // No water detected digitalWrite(ledPin, LOW); Serial.println("Status: Dry"); }
// Wait for half a second before the next reading delay(500);}Code Explanation & Calibration
Section titled “Code Explanation & Calibration”analogRead(pin): Instead of a simple HIGH/LOW, this function returns an integer value from 0 to 1023, representing the voltage on the pin (0 for 0V, 1023 for 5V). As more of the sensor’s traces are bridged by water, the resistance drops, and the voltage on pin A0 increases, resulting in a higher sensorValue.
Calibration is Key: The waterThreshold value of 400 is just a starting point. To find the right value for your setup:
- Upload the code and open the Serial Monitor (
Tools > Serial Monitor). - Keep the sensor dry and observe the readings (e.g., they might be between 0-50).
- Dip the sensor in water and observe the new, higher readings (e.g., they might be 500-700).
- Choose a threshold value that is safely between the dry and wet readings.
Critical Issue: Sensor Corrosion
Section titled “Critical Issue: Sensor Corrosion”These simple resistive sensors have a major flaw: electrolysis. When a DC current passes through the exposed metal traces in water, they will rapidly corrode and degrade, making the sensor unreliable over days or weeks. This is a common failure point in beginner projects.
Best Practice: Powering the Sensor On-Demand
Section titled “Best Practice: Powering the Sensor On-Demand”To dramatically increase the sensor’s lifespan, only power it on for the brief moment you are taking a reading. You can achieve this by connecting the sensor’s VCC pin to a digital pin on the Arduino instead of the 5V rail.
Here’s how you would modify the code for this improved setup (assuming sensor + is now connected to pin 7):
// Add this to your pin definitionsconst int sensorPowerPin = 7;
// In setup(), add:pinMode(sensorPowerPin, OUTPUT);
// In loop(), wrap your reading like this:digitalWrite(sensorPowerPin, HIGH); // Turn sensor ONdelay(10); // Wait a moment for voltage to stabilizeint sensorValue = analogRead(waterSensorPin);digitalWrite(sensorPowerPin, LOW); // Turn sensor OFF immediately
// ... rest of the logic remains the samePractical Applications and Alternatives
Section titled “Practical Applications and Alternatives”This sensor is great for a simple basement flood alarm (combine it with a buzzer!) or a rain detector. For applications requiring long-term reliability, especially in soil, consider a capacitive moisture sensor. These sensors do not have exposed electrodes and are immune to corrosion, making them a much better choice for permanent installations.