Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino Uno-Based Automatic Water Pump with Ultrasonic Sensor and Relay Control

Image of Arduino Uno-Based Automatic Water Pump with Ultrasonic Sensor and Relay Control

Circuit Documentation

Summary

This circuit is designed to control a water pump using an ultrasonic sensor and a relay module, all managed by an Arduino Uno R3. The ultrasonic sensor detects the presence of an object (e.g., a hand) within a certain distance, and if the object is detected, the relay is activated to turn on the water pump. The pump remains on for a specified duration before turning off automatically.

Component List

  1. Arduino Uno R3

    • Description: A microcontroller board based on the ATmega328P.
    • Pins: D8, D9, D10, D11, D12, D13, GND, AREF, SDA, SCL, D0/RX, D1/Tx, D2, D3, D4, D5, 6, D7, A5/SCL, A4/SDA, A3, A2, A1, A0, Vin, 5V, 3.3V, RESET, IOREF, NONE, USB Jack, Power Jack
  2. 1 Channel Relay 5V

    • Description: A relay module that allows a low-power microcontroller to control a high-power device.
    • Pins: VCC, GND, IN, NC, COM, NO
  3. HC-SR04 Ultrasonic Sensor

    • Description: An ultrasonic sensor used for distance measurement.
    • Pins: VCC, TRIG, ECHO, GND
  4. Water Pump

    • Description: A 12V water pump.
    • Pins: positive, negative
  5. Battery 12V

    • Description: A 12V battery used to power the water pump.
    • Pins: +, -

Wiring Details

Arduino Uno R3

  • D8: Connected to IN pin of the 1 Channel Relay 5V
  • 6: Connected to ECHO pin of the HC-SR04 Ultrasonic Sensor
  • D7: Connected to TRIG pin of the HC-SR04 Ultrasonic Sensor
  • GND: Connected to GND pin of the 1 Channel Relay 5V and GND pin of the HC-SR04 Ultrasonic Sensor
  • 5V: Connected to VCC pin of the 1 Channel Relay 5V and VCC pin of the HC-SR04 Ultrasonic Sensor

1 Channel Relay 5V

  • IN: Connected to D8 pin of the Arduino Uno R3
  • GND: Connected to GND pin of the Arduino Uno R3
  • VCC: Connected to 5V pin of the Arduino Uno R3
  • COM: Connected to positive pin of the Water Pump
  • NO: Connected to + pin of the Battery 12V

HC-SR04 Ultrasonic Sensor

  • ECHO: Connected to 6 pin of the Arduino Uno R3
  • TRIG: Connected to D7 pin of the Arduino Uno R3
  • GND: Connected to GND pin of the Arduino Uno R3
  • VCC: Connected to 5V pin of the Arduino Uno R3

Water Pump

  • positive: Connected to COM pin of the 1 Channel Relay 5V
  • negative: Connected to - pin of the Battery 12V

Battery 12V

  • +: Connected to NO pin of the 1 Channel Relay 5V
  • -: Connected to negative pin of the Water Pump

Code Documentation

// Ultrasonic Sensor Pins
const int trigPin = 7;
const int echoPin = 6;

// Relay Pin
const int relayPin = 8;

// Threshold Distance (in cm)
const int thresholdDistance = 10;

// Variables to track motor state and time
bool motorState = false;         // To track motor ON/OFF state
unsigned long motorStartTime = 0; // To track motor ON duration
bool handDetected = false;       // To detect if hand is in range

void setup() {
  // Initialize serial monitor
  Serial.begin(9600);

  // Set up pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(relayPin, OUTPUT);

  // Ensure relay is off initially (for active LOW relay)
  digitalWrite(relayPin, HIGH); // Relay off (HIGH for active LOW relays)
}

void loop() {
  // Measure distance
  long distance = getDistance();

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

  // Check if hand is detected
  if (distance > 0 && distance <= thresholdDistance) {
    if (!handDetected) {
      handDetected = true;            // Mark hand as detected
      motorStartTime = millis();      // Record the time
      digitalWrite(relayPin, LOW);    // Turn ON motor (LOW for active LOW relay)
      motorState = true;
      Serial.println("Motor ON");
    }
  } else {
    handDetected = false; // Reset detection when hand is removed
  }

  // Turn off motor after 3 seconds
  if (motorState && millis() - motorStartTime >= 3000) {
    digitalWrite(relayPin, HIGH); // Turn OFF motor (HIGH for active LOW relay)
    motorState = false;
    Serial.println("Motor OFF");
  }

  delay(100); // Stabilization delay
}

// Function to measure distance using ultrasonic sensor
long getDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH, 30000); // Timeout to avoid infinite wait
  long distance = duration * 0.034 / 2;

  return (distance > 0 && distance < 400) ? distance : -1; // Return valid distance
}

This code initializes the pins for the ultrasonic sensor and the relay, measures the distance using the ultrasonic sensor, and controls the relay to turn the water pump on and off based on the measured distance. The pump is turned on when an object is detected within the threshold distance and turned off after 3 seconds.