Arduino UNO Based Automatic Liquid Hand Sanitizer & Soap Dispenser System

Arduino UNO Automatic Liquid Hand Sanitizer & Soap Dispenser | MakeMindz
🔬 More Projects at MakeMindz.com — Arduino, ESP32, Raspberry Pi & IoT
⚡ Arduino Project

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.

Arduino UNO HC-SR04 × 2 5V Relay MG996R Servo Water Pump Potentiometer
Arduino UNO Automatic Hand Sanitizer and Soap Dispenser project photo

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
1Arduino UNO×1Main microcontroller
2HC-SR04 Ultrasonic Sensor×2Hand proximity detection
35V Relay Module×1Controls water pump
45V Mini Water Pump×1Dispenses liquid sanitizer
5MG996R Servo Motor×1Operates soap press mechanism
610kΩ Potentiometer×1Adjusts sensitivity/timing
7Breadboard×1Prototyping
8Jumper Wires (M–M & M–F)×30Connections
9External 5V Power Supply×1Powers pump & servo
⚠️
Power Note The MG996R servo and water pump together draw more current than USB can provide. Use a 5V/2A external power supply or USB power bank for the pump and servo. The Arduino UNO can be powered separately via USB.

Circuit Diagram & Pin Connections

Below is the wiring diagram. Study each module's connection before powering on.

circuit-diagram.json — Arduino Sanitizer Dispenser
Arduino UNO A0 A2 A3 D2 D6 D8 D9 5V GND HC-SR04 Sensor 1 (Sanitizer) VCC TRIG ECHO GND HC-SR04 Sensor 2 (Soap) VCC TRIG ECHO GND 5V Relay Module VCC IN GND 💧 Water Pump MG996R Servo Motor GND PWR SIG Potentiometer A0 → Sensitivity Wire Legend VCC / Power (5V) GND TRIG Signal ECHO Signal Control Signal

Pin Connection Table

ModuleModule PinArduino PinWire Color (suggested)
HC-SR04 #1 (Sanitizer)VCC5VRed
HC-SR04 #1GNDGNDBlack
HC-SR04 #1TRIGA3Orange
HC-SR04 #1ECHOA2Yellow
HC-SR04 #2 (Soap)VCC5VRed
HC-SR04 #2GNDGNDBlack
HC-SR04 #2TRIGD9Orange
HC-SR04 #2ECHOD8Yellow
5V Relay ModuleVCC5VRed
5V Relay ModuleGNDGNDBlack
5V Relay ModuleIND2Blue
MG996R ServoVCC (Red)Ext 5V+Red
MG996R ServoGND (Brown)GNDBlack
MG996R ServoSignal (Orange)D6Purple
PotentiometerLeftGNDBlack
PotentiometerMiddle (wiper)A0Green
PotentiometerRight5VRed
Water Pump+ terminalRelay COMRed
Water Pump− terminalExt GNDBlack
💡
Tip: Use a Flyback Diode Connect a 1N4007 diode across the water pump terminals (cathode to +) to protect the relay from voltage spikes when the pump switches off.

Step-by-Step Instructions

1

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.

2

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.

Quick test: Upload the NewPing example and confirm distance readings in Serial Monitor before continuing.
3

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.

4

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.

⚠️
Never draw pump current from the Arduino's 5V pin — use an external power supply to avoid burning the board.
5

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.

6

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.

7

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.

8

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.

Tip: Start with a delay of 500ms (half a second) and increase until you get the right volume of sanitizer per dispense.
9

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.

sanitizer_dispenser.ino
/*
 * 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.

HC-SR04 Detects hand Arduino UNO Processes distance Relay/Servo Actuator fires Pump / Motor Sanitizer dispensed ✅ Done Reset & wait

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
🔗
Also simulate on Wokwi Wokwi.com supports Arduino UNO + HC-SR04 + Servo. Create a free project, paste the code, and add the components from their library.

Where Can You Use This?

🏥Hospitals & Clinics
🏫Schools & Colleges
🏢Offices & Reception
🛒Retail Stores
🚌Public Transport
🏠Smart Home Entry
🔬Science Exhibitions
🌐IoT Demo Projects

Arduino · ESP32 · Raspberry Pi · IoT Projects for Makers & Engineers

www.makemindz.com

Comments

try for free