Arduino Water Level Alarm: Warns Before It Overflows

Arduino ile Su Seviyesi Alarmı Yapımı projesi kapak görseli

🇹🇷 Bu yazıyı Türkçe okuyun

What does this project do?

We build an alarm that measures how high the water in a container is and warns you before it overflows. The deeper the water level sensor is in the water, the higher the value it sends to the Arduino. When there is little water, the green LED is on; when it is half full, the yellow LED is on. When the water reaches a dangerous level, the red LED lights up and the buzzer beeps. You can use it for a plant saucer, an aquarium or a rainwater bucket.

  • Age group: 9 and up
  • Level: Beginner
  • Time: About 40 minutes
  • What you will learn: reading an analog sensor, calibrating a sensor, making decisions with if – else if – else, powering the sensor only while measuring to make it last longer

New to Arduino? Have a look at the What is Arduino and how does it work? guide first. All projects, sorted from easy to hard, are on the Getting Started with Arduino page.

Parts list

PartQty
Arduino Uno (or compatible)1
Water level sensor (red board with stripes)1
LED (green, yellow, red)3
220 Ω resistor3
Passive buzzer1
Breadboard1
Male-to-male and male-to-female jumper wires~12
Glass of water (for testing)1
USB cable1

Wiring

Arduino water level alarm wiring diagram
ComponentComponent pinArduino pin
Green LEDLong leg (through 220 Ω)D3
Yellow LEDLong leg (through 220 Ω)D4
Red LEDLong leg (through 220 Ω)D5
LEDsShort legsGND
Water level sensor+D7
Water level sensorSA0
Water level sensor–GND
Buzzer+ (long leg)D8
Buzzer–GND

We connect the sensor’s + pin to D7 instead of 5V. If the sensor is powered all the time, the metal stripes in the water corrode quickly. The code switches the sensor on for only a few milliseconds while it measures.

Code

// Arduino Water Level Alarm - ozandamci.com

const int greenLed = 3;
const int yellowLed = 4;
const int redLed = 5;
const int sensorPower = 7;
const int buzzerPin = 8;
const int sensorPin = A0;

// Adjust these for your own sensor (check the values in the Serial Monitor)
const int MEDIUM_LEVEL = 300;
const int HIGH_LEVEL = 500;

void setup() {
  pinMode(greenLed, OUTPUT);
  pinMode(yellowLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(sensorPower, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  digitalWrite(sensorPower, LOW);
  Serial.begin(9600);
}

int readLevel() {
  digitalWrite(sensorPower, HIGH);  // turn the sensor on
  delay(10);
  int value = analogRead(sensorPin);
  digitalWrite(sensorPower, LOW);   // turn the sensor off
  return value;
}

void setLeds(bool green, bool yellow, bool red) {
  digitalWrite(greenLed, green);
  digitalWrite(yellowLed, yellow);
  digitalWrite(redLed, red);
}

void loop() {
  int level = readLevel();
  Serial.print("Water level: ");
  Serial.println(level);

  if (level < MEDIUM_LEVEL) {
    // Low level: all good
    setLeds(HIGH, LOW, LOW);
    noTone(buzzerPin);
  } else if (level < HIGH_LEVEL) {
    // Medium level: careful
    setLeds(LOW, HIGH, LOW);
    noTone(buzzerPin);
  } else {
    // High level: alarm!
    setLeds(LOW, LOW, HIGH);
    tone(buzzerPin, 2000, 200);
  }

  delay(500);
}

No Arduino? Try this circuit in your browser with the Wokwi simulation → (free, nothing to install). In the simulation, a potentiometer stands in for the water level sensor.

Step-by-step instructions

  1. Prepare the GND rail. Connect the Arduino’s GND pin to the – rail of the breadboard.
  2. Place the LEDs. Connect a 220 Ω resistor to the long leg of each LED. Wire green to D3, yellow to D4 and red to D5. Connect the short legs to the – rail.
  3. Connect the sensor. With male-to-female wires, connect the sensor’s + pin to D7, the S pin to A0 and the – pin to the – rail.
  4. Place the buzzer. Connect its long leg to D8 and its short leg to the – rail.
  5. Upload the code. In the Arduino IDE, choose Arduino Uno and the right port from Tools → Board, then click Upload.
  6. Calibrate. Open Tools → Serial Monitor. Slowly dip the sensor into the water; write down the values when it is half under water and when it is almost fully under water. Put these numbers into MEDIUM_LEVEL and HIGH_LEVEL in the code and upload it again.
  7. Mount it. Tape the sensor to the side of the container with the striped part at the bottom. The top part with the wires must never get wet.

Troubleshooting

  • The value always shows 0: Check that the sensor’s + pin goes to D7 and the S pin to A0.
  • The value is high even without water: There may be a drop of water left on the sensor. Dry it and try again.
  • The values drop over time: The sensor’s metal stripes may have corroded. Don’t leave the sensor in water all the time, and calibrate it again.

Try it yourself

  • Add a button that puts the buzzer into a silent mode.
  • Add a DC water pump and a relay to make a system that tops up the water by itself when it gets low.
  • Convert the level into a percentage from 0 to 100 and print something like “65% full” in the Serial Monitor.

For teachers: a two-page activity sheet for using this project in class: lesson plan, discussion questions, answer key and a student worksheet.

Video

The narration is in Turkish. English subtitles are available: click CC on the video.

“Arduino Water Level Alarm: Warns Before It Overflows” için 2 yorum

Bir Cevap Yazın