Water Leak / Level Alert (Arduino)
Light an LED when water reaches the sensor — a leak detector or tank-level warning. Learn to power a sensor from a pin.
Beginner15 min
What you need
Wiring
Hover a pin or a wire to trace the connection.
Code
water_alert.ino
// Light an LED when water reaches the sensor.
const int sensorPower = 7; // power the sensor from a pin
const int sensorPin = A0;
const int ledPin = 13;
const int threshold = 300; // calibrate dry vs. wet
void setup() {
pinMode(sensorPower, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(sensorPower, HIGH); // power on
delay(10);
int level = analogRead(sensorPin);
digitalWrite(sensorPower, LOW); // power off (less corrosion)
Serial.println(level);
digitalWrite(ledPin, level > threshold ? HIGH : LOW);
delay(500);
}
Steps
- Wire: + to D7, − to GND, S to A0. LED on D13 through a 220Ω resistor to GND.
- Open the Serial Monitor; note readings dry vs. with water, set `threshold` between them.
- When water bridges the traces, the LED turns on.
- Powering + from D7 (not 5V) keeps the traces from corroding between reads.