Automatic Hand Sanitizer

Arduino Project – Touch Free Automatic Hand Sanitizer

In this project, we will learn how to make an automatic hand sanitizer using an ultrasonic sensor and a servo motor.

The ultrasonic sensor is a device that uses high-frequency sound waves (ultrasound) to detect objects, measure distances, or detect changes in the environment. It emits sound waves and measures the time it takes to bounce off an object and return to the sensor. Visit Exploring the Power of Ultrasonic Sensors: A Brief Introduction for more information.

The servo motor is a device that can rotate to a specific angle or position, making it ideal for applications such as moving robotic arms, steering mechanisms, and camera gimbals. Visit SG90 Servo Motor Basics: What You Need to Know for more information.

Hardware Required

  • Arduino UNO
Arduino UNO

For more information, visit Getting to Know Your Arduino Uno: A Beginner’s Guide to Its Components.

  • HC-SR04 Ultrasonic Sensor
Ultrasonic Sensor
  • SG90 Servo Motor
Servo Motor
  • 9 V Battery
9V Battery
9V Battery Connector with DC Plug
  • Jumper Wires
Jumper Wires - Male to Male Male to Male & Male to Female
  • Hand Sanitizer
Hand Sanitizer
Double Sided Foam Tape
  • Hot Glue Gun
Hot Glue Gun

Working Principle

The ultrasonic sensor detects the presence of a hand approximately 5 cm away. Once a hand is detected, the sensor sends a signal to the Arduino. The Arduino then activates the servo motor by sending an output signal. In response, the servo motor rotates and triggers the dispenser pump.

Diagram

Setup

Automatic Hand Sanitizer
1. Attach the Arduino board to the hand sanitizer bottle using the double sided tape
Automatic Hand Sanitizer
2. Mount the ultrasonic sensor on the bottle using the tape
3. Connect the ultrasonic sensor to the Arduino using jumper wires
Ultrasonic SensorArduino
VCC3.3V
GNDGND
TrigD6
EchoD5
4. Secure the servo motor to the bottle using a hot glue gun
5. Connect the servo motor to the Arduino using jumper wires
Servo MotorArduino
VCC5V
GNDGND
SIGD3
6. Connect the servo motor to the dispenser pump
7. Upload the program to the Arduino using a USB cable

Connect the Arduino to your computer using a USB cable. Upload the program to the Arduino through the Arduino IDE software. Visit Getting Started with Arduino IDE: A Beginner’s Guide to Coding and Creating for more information.

8. Attach the 9V battery to the bottle using the tape
9. Connect the battery to the Arduino to power the system

Code

#include <Servo.h>

// defines pins numbers
const int trigPin = 6; 
const int echoPin = 5;

long duration;
int distance;
 
Servo myServo;  // create servo object

void setup() {
  myServo.attach(3);  // attach to digital pin 3

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}

void loop() {
  distance = calculateDistance();
  
  myServo.write(0);  // move to 0 degrees

  if ( distance < 5){
    myServo.attach(3);
    myServo.write(160);  // move to 160 degrees
    delay(500);
    myServo.write(0);  // move to 0 degrees
    delay(1000);
  }
  else{
    myServo.detach();
  }
}

int calculateDistance(){ 
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(5);

  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH); 
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH); 

  // Calculating the distance
  distance= duration*0.034/2;
  return distance;
}