Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Based Water Pump Control System with HC-SR04 Ultrasonic Sensor and Relay

View and Edit Project In Cirkit DesignerImage of Arduino-Based Water Pump Control System with HC-SR04 Ultrasonic Sensor and Relay

Water Pumping System for Ship's Ballast

Summary

This circuit is designed to control a water pump based on the water level measured by an HC-SR04 ultrasonic sensor. The system uses an Arduino UNO to process the sensor data and control a relay, which in turn controls the water pump. The relay is powered by a 12V battery.

Component List

  1. HC-SR04 Ultrasonic Sensor

    • Description: Measures the distance to an object using ultrasonic waves.
    • Pins: VCC, TRIG, ECHO, GND
  2. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  3. 1 Channel Relay 5V

    • Description: Controls the high-power water pump using a low-power signal from the Arduino.
    • Pins: VCC, GND, IN, NC, COM, NO
  4. Water Pump

    • Description: Pumps water when powered.
    • Pins: VCC, GND
  5. 12V Battery (mini)

    • Description: Provides power to the water pump.
    • Pins: -, +

Wiring Details

HC-SR04 Ultrasonic Sensor

Arduino UNO

1 Channel Relay 5V

Water Pump

12V Battery (mini)

Code Documentation

/*
 * Water Pumping System for Ship's Ballast
 * This code controls a water pump based on the water level
 * measured by an HC-SR04 ultrasonic sensor. The pump is
 * controlled via a relay.
 */

#define TRIG_PIN 9
#define ECHO_PIN 8
#define RELAY_PIN 7

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW); // Ensure pump is off initially
  Serial.begin(9600);
}

void loop() {
  long duration, distance;
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = (duration / 2) / 29.1; // Convert to cm
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  if (distance < 10) { // If water level is below 10 cm
    digitalWrite(RELAY_PIN, HIGH); // Turn on pump
  } else {
    digitalWrite(RELAY_PIN, LOW); // Turn off pump
  }
  delay(1000); // Wait for 1 second before next measurement
}

This code initializes the pins for the ultrasonic sensor and the relay, measures the distance to the water surface, and turns the water pump on or off based on the measured distance. The distance is also printed to the serial monitor for debugging purposes.