Arduino-Based Water Pump Control System with HC-SR04 Ultrasonic Sensor and Relay
View and Edit Project In Cirkit Designer
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
HC-SR04 Ultrasonic Sensor
- Description: Measures the distance to an object using ultrasonic waves.
- Pins: VCC, TRIG, ECHO, GND
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
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
Water Pump
- Description: Pumps water when powered.
- Pins: VCC, GND
12V Battery (mini)
- Description: Provides power to the water pump.
- Pins: -, +
Wiring Details
HC-SR04 Ultrasonic Sensor
- VCC: Connected to 5V on the Arduino UNO.
- TRIG: Connected to D9 on the Arduino UNO.
- ECHO: Connected to D8 on the Arduino UNO.
- GND: Connected to GND on the Arduino UNO.
Arduino UNO
- 5V: Connected to VCC on the HC-SR04 Ultrasonic Sensor and VCC on the 1 Channel Relay 5V.
- D9: Connected to TRIG on the HC-SR04 Ultrasonic Sensor.
- D8: Connected to ECHO on the HC-SR04 Ultrasonic Sensor.
- GND: Connected to GND on the HC-SR04 Ultrasonic Sensor and GND on the 1 Channel Relay 5V.
- D7: Connected to IN on the 1 Channel Relay 5V.
1 Channel Relay 5V
- VCC: Connected to 5V on the Arduino UNO.
- GND: Connected to GND on the Arduino UNO.
- IN: Connected to D7 on the Arduino UNO.
- COM: Connected to + on the 12V Battery (mini).
- NO: Connected to VCC on the Water Pump.
Water Pump
- VCC: Connected to NO on the 1 Channel Relay 5V.
- GND: Connected to - on the 12V Battery (mini).
12V Battery (mini)
- +: Connected to COM on the 1 Channel Relay 5V.
- -: Connected to GND on the Water Pump.
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.