Circuit Documentation
Summary
This circuit is designed to sense the presence of an object (such as an empty bottle) using an HC-SR04 Ultrasonic Distance Sensor and to control a Water Pump via a Relay Module based on the detected distance. The control logic is implemented on an Arduino UNO microcontroller. When the distance to the object is greater than 10 cm, the water pump is activated to dispense water. The circuit is powered by a 12V power supply, with the Arduino UNO and the HC-SR04 sensor receiving power from the Arduino's onboard voltage regulator.
Component List
Arduino UNO
- Microcontroller board based on the ATmega328P
- Provides digital I/O pins, analog input pins, and various power outputs
- Used for processing sensor data and controlling the relay module
HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)
- Ultrasonic ranging module
- Provides distance measurements by emitting ultrasonic waves and measuring the time taken for the echo to return
- Used to detect the presence and distance of an object
RELAY MODULE
- Electromechanical switch module
- Allows for control of high-power devices using low-power signals from the microcontroller
- Used to control the power to the water pump
Water Pump
- Electric pump for moving water
- Powered by the 12V power supply through the relay module
- Activated by the relay to dispense water when required
12V Power Supply
- Provides the main power source for the circuit
- Supplies power to the water pump and the relay module
Wiring Details
Arduino UNO
5Vpin connected to the VCC of HC-SR04 Ultrasonic Distance Sensor and RELAY MODULEGNDpin connected to the GND of HC-SR04 Ultrasonic Distance Sensor and RELAY MODULED7pin connected to the TRIG pin of HC-SR04 Ultrasonic Distance SensorD8pin connected to the ECHO pin of HC-SR04 Ultrasonic Distance SensorD9pin connected to the INPUT pin of RELAY MODULE
HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)
VCCpin connected to Arduino UNO 5VGNDpin connected to Arduino UNO GNDTRIGpin connected to Arduino UNO D7ECHOpin connected to Arduino UNO D8
RELAY MODULE
VCCpin connected to Arduino UNO 5VGNDpin connected to Arduino UNO GNDINPUTpin connected to Arduino UNO D9NC(Normally Closed) pin connected to Water Pump VCCCOM(Common) pin connected to 12V Power Supply -
Water Pump
VCCpin connected to RELAY MODULE NCGNDpin connected to 12V Power Supply +
12V Power Supply
+pin connected to Water Pump GND-pin connected to RELAY MODULE COM
Documented Code
/*
* Arduino Sketch for sensing an empty bottle and dispensing water
* Components:
* - Arduino UNO
* - HC-SR04 Ultrasonic Distance Sensor
* - Relay Module
* - Water Pump
*
* Connections:
* - HC-SR04 VCC to Arduino 5V
* - HC-SR04 GND to Arduino GND
* - HC-SR04 TRIG to Arduino D7
* - HC-SR04 ECHO to Arduino D8
* - Relay Module VCC to Arduino 5V
* - Relay Module GND to Arduino GND
* - Relay Module INPUT to Arduino D9
* - Relay Module NC to Water Pump VCC
* - Relay Module COM to 12V Power Supply -
* - Water Pump GND to 12V Power Supply +
*/
#define TRIG_PIN 7
#define ECHO_PIN 8
#define RELAY_PIN 9
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Ensure relay 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 distance is greater than 10 cm
digitalWrite(RELAY_PIN, HIGH); // Turn on the water pump
} else {
digitalWrite(RELAY_PIN, LOW); // Turn off the water pump
}
delay(1000); // Wait for 1 second before next measurement
}
This code is written for the Arduino UNO and is responsible for reading the distance from the HC-SR04 Ultrasonic Distance Sensor and controlling the Relay Module based on the distance measured. If the distance is greater than 10 cm, the relay is activated, which in turn powers the Water Pump. The relay is deactivated when the distance is less than or equal to 10 cm. The serial monitor outputs the measured distance for debugging purposes.
