How to Build a Water Tank Level Monitoring System Using Arduino and Ultrasonic Sensor

Use an ultrasonic sensor like the HC-SR04 to detect the water level in a tank and use the Arduino Nano to control the LED indicators based on the water level.

Components you will need:

  1. Arduino Nano
  2. HC-SR04 Ultrasonic Sensor (to measure distance/water level)
  3. 3 LEDs (Green, Yellow, and Red)
  4. Resistors for the LEDs (220 ohms for each LED)
  5. Jumper wires
  6. Breadboard (optional)

Wiring:

  1. HC-SR04 Ultrasonic Sensor:
    • VCC -> 5V
    • GND -> GND
    • Trig -> Digital Pin 9 (for example)
    • Echo -> Digital Pin 10 (for example)

Here’s the wiring for the LEDs:

  1. Green LED:
    • Anode (long leg)Digital Pin 2 through a 220-ohm resistor.
    • Cathode (short leg)GND (on the Arduino Nano).
  2. Yellow LED:
    • Anode (long leg)Digital Pin 3 through a 220-ohm resistor.
    • Cathode (short leg)GND.
  3. Red LED:
    • Anode (long leg)Digital Pin 4 through a 220-ohm resistor.
    • Cathode (short leg)GND.

Code:

Here’s a simple Arduino code to control the LEDs based on the water level:

#define trigPin 9
#define echoPin 10
#define greenLED 2
#define yellowLED 3
#define redLED 4

void setup() {
  // Set the LED pins as outputs
  pinMode(greenLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  
  // Set the Ultrasonic Sensor pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  // Start serial communication
  Serial.begin(9600);
}

void loop() {
  long duration, distance;
  
  // Clear the trigPin by setting it LOW
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  // Trigger the ultrasonic pulse by setting trigPin HIGH
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Read the time it takes for the echo to return
  duration = pulseIn(echoPin, HIGH);
  
  // Calculate the distance in cm
  distance = (duration / 2) / 29.1;
  
  // Print the distance for debugging
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  
  // Calculate the water level (assuming the tank height is 30 cm)
  int tankHeight = 30; // Adjust this to the height of your tank
  int waterLevel = tankHeight - distance;  // Distance is the empty space, so subtract from the total height
  
  // Water level percentages
  int waterPercentage = map(waterLevel, 0, tankHeight, 0, 100);
  
  // Control LEDs based on water level
  if (waterPercentage > 70) {
    digitalWrite(greenLED, HIGH);  // Green LED ON
    digitalWrite(yellowLED, LOW);  // Yellow LED OFF
    digitalWrite(redLED, LOW);     // Red LED OFF
  }
  else if (waterPercentage <= 70 && waterPercentage > 30) {
    digitalWrite(greenLED, LOW);   // Green LED OFF
    digitalWrite(yellowLED, HIGH); // Yellow LED ON
    digitalWrite(redLED, LOW);     // Red LED OFF
  }
  else if (waterPercentage <= 30) {
    digitalWrite(greenLED, LOW);   // Green LED OFF
    digitalWrite(yellowLED, LOW);  // Yellow LED OFF
    digitalWrite(redLED, HIGH);    // Red LED ON
  }
  
  delay(500);  // Wait for 0.5 seconds before the next reading
}

Explanation of the Code:

  1. Sensor Measurements:
    • The ultrasonic sensor sends out a pulse and waits for it to bounce back. The time taken for the pulse to return is measured, which can be converted into the distance to the water level.
  2. Water Level Calculation:
    • Based on the distance measured, we calculate the water level by subtracting the distance from the total height of the tank (assuming you know the height of the tank).
  3. LED Control:
    • Using the water level as a percentage (mapped between 0 and 100), we check the value:
      • If the water level is above 70%, the green LED turns ON.
      • If the water level is between 30% and 70%, the yellow LED turns ON.
      • If the water level is below or equal to 30%, the red LED turns ON.
  4. Serial Monitor (for debugging):
    • You can check the measured distance and water level percentage by opening the Serial Monitor in the Arduino IDE.

Adjustments:

  • Make sure to adjust the tankHeight variable to the height of your tank (in cm).
  • You can also tweak the LED control logic to fit any other thresholds you prefer.