Automatic Water Level Controller Using Arduino, Ultrasonic Sensor and Submersible Pump

Introduction

In this project, we will build an Automatic Water Level Controller using Arduino Uno, an HC-SR04 ultrasonic sensor, a relay module, and a submersible water pump.

The ultrasonic sensor continuously measures the distance between the sensor and the water surface. When the water level drops below a predefined level, Arduino automatically turns ON the water pump. Once the container is filled, Arduino turns OFF the pump automatically.

This project is useful for:

  • Automatic water filling systems
  • Water tank automation
  • Aquarium water management
  • Smart home water control systems
  • Learning sensor and relay interfacing

How It Works

The HC-SR04 ultrasonic sensor is mounted at the top of the water container.

Water Level Logic

Distance from SensorWater Level
3 cmFull
5 cmMedium
4 cmLow
2 cm or moreVery Low

Arduino continuously measures the distance.

Pump Control Logic

Distance > 5 cm
        ↓
Water Level Low
        ↓
Pump ON

Distance < 3 cm
        ↓
Container Full
        ↓
Pump OFF

This method prevents frequent switching of the relay and pump.


Components Required

ComponentQuantity
Arduino Uno1
HC-SR04 Ultrasonic Sensor1
5V Relay Module1
Mini Submersible Water Pump1
External Battery Supply1
Jumper WiresAs Required
Water Container1

Circuit Diagram Connections

HC-SR04 to Arduino

HC-SR04Arduino
VCC5V
GNDGND
TrigD9
EchoD10

Relay Module to Arduino

Relay ModuleArduino
VCC5V
GNDGND
IND7

Pump Connections

Battery Positive
|
COM
Relay
NO
|
Pump +

Pump -
|
Battery Negative

Relay Terminals

TerminalPurpose
COMCommon
NONormally Open
NCNormally Closed

Connect the pump through COM and NO terminals.


Arduino Code

#define TRIG_PIN 9
#define ECHO_PIN 10
#define RELAY_PIN 7

float getDistance() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);

  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH);

  return duration * 0.0343 / 2.0;
}

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  pinMode(RELAY_PIN, OUTPUT);

  // Change HIGH to LOW if relay is active LOW
  digitalWrite(RELAY_PIN, LOW);

  Serial.begin(9600);
}

void loop() {

  float distance = getDistance();

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  if (distance > 5) {
    digitalWrite(RELAY_PIN, HIGH);  // Pump ON
    Serial.println("Pump ON");
  }

  if (distance < 3) {
    digitalWrite(RELAY_PIN, LOW);   // Pump OFF
    Serial.println("Pump OFF");
  }

  delay(500);
}

Code Explanation

Measuring Distance

The HC-SR04 sends an ultrasonic pulse and waits for the echo to return.

long duration = pulseIn(ECHO_PIN, HIGH);

The measured time is converted into distance:

float distance = duration * 0.0343 / 2.0;

Where:

  • 0.0343 cm/µs is the speed of sound
  • Division by 2 compensates for the round trip of the sound wave

Pump ON Condition

if (distance > 5)

When the measured distance exceeds 10 cm, the water level is low.

Arduino activates the relay and starts the pump.


Pump OFF Condition

if (distance < 3)

When the water reaches near the top of the container, the measured distance becomes less than 4 cm.

Arduino turns OFF the relay and stops the pump.


Advantages

  • Fully automatic operation
  • Low cost
  • Easy to build
  • Prevents water overflow
  • Reduces manual effort
  • Suitable for small water tanks and containers

Troubleshooting

Pump Runs Continuously

  • Check relay wiring.
  • Verify relay COM and NO terminals.
  • Ensure pump ON/OFF logic matches your relay type.

Incorrect Distance Readings

  • Mount the ultrasonic sensor vertically.
  • Keep the sensor centered above the water.
  • Avoid water splashes on the sensor.

Relay Works Opposite

Some relay modules are Active LOW.

If the pump behaves opposite to the expected operation, reverse the HIGH and LOW states used for relay control.


Conclusion

This Arduino-based Automatic Water Level Controller automatically monitors the water level using an HC-SR04 ultrasonic sensor and controls a submersible pump through a relay module. The system starts filling the container when the water level becomes low and stops filling when the container reaches the desire