Automatic Touchless Hand Sanitizer & Soap Dispenser
Build a dual-function, contact-free hygiene station using Arduino UNO, HC-SR04 ultrasonic sensors, a relay-controlled water pump, and a servo motor — with full code and circuit.
📋 Table of Contents
What is this project?
The Arduino UNO-based Automatic Liquid Hand Sanitizer Dispenser is a smart, touchless hygiene solution that uses dual HC-SR04 ultrasonic sensors to detect hand presence and automatically activate dispensing — no buttons, no touching.
It controls two dispensing mechanisms independently: a 5V mini water pump via relay for liquid sanitizer, and a MG996R servo motor for a soap/gel dispenser. A potentiometer lets you tune sensitivity or pump duration.
Fully Touchless
Ultrasonic detection means zero contact from approach to dispense.
Dual Dispensing
Sensor 1 → pump for sanitizer. Sensor 2 → servo for soap. Both independent.
Adjustable
Potentiometer lets you customise dispense time or detection threshold.
Beginner Friendly
No extra libraries beyond Servo.h — standard Arduino code all the way.
What You'll Need
All parts are widely available from electronics stores or online (Amazon, Robu.in, AliExpress). Estimated cost: ₹350–600 / $5–10.
| # | Component | Qty | Purpose |
|---|---|---|---|
| 1 | Arduino UNO | ×1 | Main microcontroller |
| 2 | HC-SR04 Ultrasonic Sensor | ×2 | Hand proximity detection |
| 3 | 5V Relay Module | ×1 | Controls water pump |
| 4 | 5V Mini Water Pump | ×1 | Dispenses liquid sanitizer |
| 5 | MG996R Servo Motor | ×1 | Operates soap press mechanism |
| 6 | 10kΩ Potentiometer | ×1 | Adjusts sensitivity/timing |
| 7 | Breadboard | ×1 | Prototyping |
| 8 | Jumper Wires (M–M & M–F) | ×30 | Connections |
| 9 | External 5V Power Supply | ×1 | Powers pump & servo |
Circuit Diagram & Pin Connections
Below is the wiring diagram. Study each module's connection before powering on.
Pin Connection Table
| Module | Module Pin | Arduino Pin | Wire Color (suggested) |
|---|---|---|---|
| HC-SR04 #1 (Sanitizer) | VCC | 5V | Red |
| HC-SR04 #1 | GND | GND | Black |
| HC-SR04 #1 | TRIG | A3 | Orange |
| HC-SR04 #1 | ECHO | A2 | Yellow |
| HC-SR04 #2 (Soap) | VCC | 5V | Red |
| HC-SR04 #2 | GND | GND | Black |
| HC-SR04 #2 | TRIG | D9 | Orange |
| HC-SR04 #2 | ECHO | D8 | Yellow |
| 5V Relay Module | VCC | 5V | Red |
| 5V Relay Module | GND | GND | Black |
| 5V Relay Module | IN | D2 | Blue |
| MG996R Servo | VCC (Red) | Ext 5V+ | Red |
| MG996R Servo | GND (Brown) | GND | Black |
| MG996R Servo | Signal (Orange) | D6 | Purple |
| Potentiometer | Left | GND | Black |
| Potentiometer | Middle (wiper) | A0 | Green |
| Potentiometer | Right | 5V | Red |
| Water Pump | + terminal | Relay COM | Red |
| Water Pump | − terminal | Ext GND | Black |
Step-by-Step Instructions
Gather & Verify All Components
Lay out every component from the list. Test each HC-SR04 individually using a basic Arduino ping sketch before wiring the full circuit. Verify the relay toggles correctly at 5V.
Wire Ultrasonic Sensor 1 (Sanitizer Side)
Connect HC-SR04 #1 to Arduino: VCC→5V, GND→GND, TRIG→A3, ECHO→A2. Place this sensor at the front of your dispenser where hands approach the sanitizer nozzle.
NewPing example and confirm distance readings in Serial Monitor before continuing.Wire Ultrasonic Sensor 2 (Soap Side)
Connect HC-SR04 #2: VCC→5V, GND→GND, TRIG→D9, ECHO→D8. Position this sensor at the soap dispenser section. Both sensors can share the same 5V and GND rails on the breadboard.
Connect the 5V Relay Module
Wire the relay: VCC→5V, GND→GND, IN→D2. The relay's COM terminal connects to the positive wire of the water pump. The NO (Normally Open) terminal connects to your external 5V positive supply.
Connect the MG996R Servo Motor
Attach the servo's signal wire (orange/yellow) to D6. Power the servo from an external 5V/2A supply, not directly from Arduino's 5V. Share GND between external supply and Arduino. Attach a servo arm or lever that physically presses the soap dispenser pump.
Connect the Potentiometer
Wire left leg → GND, right leg → 5V, middle wiper → A0. This lets you dial in sensitivity at runtime. In the current code it's read but you can map it to the 10cm detection threshold or the pump duration.
Upload the Arduino Code
Open Arduino IDE, paste the code from the section below, select Board: Arduino UNO and the correct COM port. Click Upload. Open Serial Monitor at 9600 baud to see distance readings in real time.
Test & Calibrate
Place your hand within 10cm of Sensor 1 — the pump should run for 1 second. Place your hand near Sensor 2 — the servo should rotate 0° → 90° → 0°. Adjust the distance < 10 threshold or delay(1000) values to suit your dispenser and sanitizer bottle.
Mount in an Enclosure
3D-print or use a plastic box. Mount both sensors at hand level (30–40cm from the ground if for children, 80–90cm for adults). Seal pump tubing to prevent leaks. Secure the servo arm to the soap pump actuator with a small zip tie or adhesive mount.
Arduino Sketch
No additional libraries needed beyond the built-in Servo.h. Copy the code below and upload via Arduino IDE.
/* * Arduino UNO-based Automatic Liquid Hand Sanitizer Dispenser * MakeMindz.com * * Components: * - 2x HC-SR04 Ultrasonic Sensors * - 1x 5V Relay Module → Water Pump * - 1x MG996R Servo Motor → Soap Dispenser * - 1x Potentiometer (A0) */ #include <Servo.h> // ── Pin Definitions ────────────────────────────────── const int trigPin1 = A3; // Sensor 1 TRIG (sanitizer) const int echoPin1 = A2; // Sensor 1 ECHO const int trigPin2 = 9; // Sensor 2 TRIG (soap) const int echoPin2 = 8; // Sensor 2 ECHO const int relayPin = 2; // Relay (→ water pump) const int servoPin = 6; // Servo signal pin const int potPin = A0; // Potentiometer Servo soapServo; void setup() { Serial.begin(9600); pinMode(trigPin1, OUTPUT); pinMode(echoPin1, INPUT); pinMode(trigPin2, OUTPUT); pinMode(echoPin2, INPUT); pinMode(relayPin, OUTPUT); soapServo.attach(servoPin); digitalWrite(relayPin, LOW); // Pump off soapServo.write(0); // Servo home } // ── Helper: read distance from an HC-SR04 ──────────── long readDistance(int trig, int echo) { digitalWrite(trig, LOW); delayMicroseconds(2); digitalWrite(trig, HIGH); delayMicroseconds(10); digitalWrite(trig, LOW); long dur = pulseIn(echo, HIGH); return (dur / 2) / 29.1; // Convert to cm } void loop() { long distance1 = readDistance(trigPin1, echoPin1); long distance2 = readDistance(trigPin2, echoPin2); Serial.print("S1: "); Serial.print(distance1); Serial.print(" cm | "); Serial.print("S2: "); Serial.print(distance2); Serial.println(" cm"); // ── Sanitizer: pump for 1 second ───────────────── if (distance1 < 10) { digitalWrite(relayPin, HIGH); delay(1000); digitalWrite(relayPin, LOW); } // ── Soap: servo pushes actuator 90° then resets ── if (distance2 < 10) { soapServo.write(90); delay(1000); soapServo.write(0); } delay(100); // Short pause before next poll }
Code Walkthrough
readDistance() is a reusable helper that fires the ultrasonic pulse and returns the distance in centimetres. Both sensors use the same function, keeping the main loop clean.
The 10cm threshold triggers dispensing — adjust this based on how far from the sensor your nozzle sits. The 1000ms delay controls how long the pump runs; reduce to 500ms for smaller dispensing volumes.
The potentiometer on A0 is read but not yet mapped in the base code — you can use analogRead(potPin) to dynamically set either the distance threshold or the pump duration.
Working Principle
Hand Detected by Sensor 1
HC-SR04 fires ultrasonic pulses. When reflected echo returns in < 10cm range, Arduino triggers the relay, activating the water pump for 1 second to dispense sanitizer.
Hand Detected by Sensor 2
Second sensor independently monitors the soap nozzle zone. Detection rotates the MG996R servo from 0° to 90°, physically pressing the soap dispenser actuator, then returns to rest.
Potentiometer Tuning
The wiper voltage on A0 (0–5V → 0–1023 ADC) can map to any runtime variable — detection distance, pump-on duration, or servo angle — giving physical control without re-uploading.
Try It Online (No Hardware Needed)
Test the circuit and code in Cirkit Designer before buying any components. Simulate sensor readings, relay switching, and servo movement instantly in your browser.
Cirkit Designer Simulation
Full interactive simulation with Arduino UNO, HC-SR04 sensors, relay, servo, and pump. Adjust sensor distance virtually and watch the system respond.
▶ Open Simulation
Comments
Post a Comment