Automatic Hand Sanitizer Dispenser


The working principle of the automatic sanitizer dispenser is to actuate the servo to press the sanitizer tap whenever the sensor observes a low distance reading due to an obstruction in its line-of-sight. When a person’s hand comes below the sanitizer and obstructs the sensor line-of-sight, the Arduino board receives a low distance reading and instructs the servo motor to actuate and dispense the sanitizer.

Component Required:

 Arduino Uno

 Uno cable

 Ultrasonic sensor

 Small breadboard

 SG90 Micro-Servo motor

 Jumper Wire

 9V battery

 Battery snapper connector

Circuit Diagram


Connect sensor VCC pin to VCC

Connect sensor Trig pin to Arduino pin 3

Connect sensor Echo pin to Arduino pin 2

Connect sensor GND pin to GND

Connect motor VCC pin to VCC

Connect motor GND pin to GND

Connect motor Signal pin to Arduino pin 4

Code for Automatic hand sanitizer dispenser:

#include <Servo.h>

#define trigPin 3

#define echoPin 2

Servo servo;

int sound = 250;

void setup() {

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);



void loop() {

long duration, distance;

digitalWrite(trigPin, LOW);


digitalWrite(trigPin, HIGH);


digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

if (distance < 5) {

Serial.println(“the distance is less than 5”);



else {



if (distance > 60 || distance <= 0){

Serial.println(“The distance is more than 60”);


else {


Serial.println(” cm”);




If you have any query please write us at [email protected]

Hope you understand the project well and if you are facing any errors do inform us in the comments section below.

Back to list

Leave a Reply

Your email address will not be published. Required fields are marked *