In these hard times, we need to avoid external physical contact as much as possible. We use sanitizers and hand washes regularly, but it still serves as a source of physical contact. So, I decided to make an automatic hand sanitizing machine.
The circuit:Arduino
- Connect the 5V of the Arduino to the +ve power rail on the breadboard
- Connect the ground of the Arduino to the -ve rail of the breadboard
Note: You would later want to make your project portable. For this, you can connect the V IN pin of the Arduino to the +ve rail instead of the 5V pin. You can then connect a 9V battery to the power rails to power up your project.
HC-SR04 Ultrasonic distance sensor
- Connect the 5V pin of the ultrasonic distance sensor to the +ve rail of the breadboard
- Connect the ground pin of the sensor to the -ve power rail on the breadboard
- Connect the trigger pin of the sensor to pin 9 on the Arduino
- Connect the echo pin of the sensor to pin 10 on the Arduino
Note: You can alternatively use an IR proximity sensor for this project, or any other distance sensor.
Servo motor
- Connect the middle wire (5V) of the servo motor to the +ve rail on the breadboard
- Connect the ground wire of the servo motor to the -ve rail on the breadboard
- Connect the signal pin on the servo to pin 2 on the Arduino
That's it! the wiring is pretty simple.
WorkingWe start by including the New Ping and the Servo libraries.
#include <Servo.h>
#include<NewPing.h>
Next, we define all our variables.
int trigPin = 9;
int echoPin = 10;
float v = 343;
float d = 0;
float pingTime = 0;
int servoPin = 2;
int servoPos = 0;
int buzzPin = 5;
We create an object called myServo using the servo library, and another object called sonar using the New Ping library
Servo myServo;
NewPing sonar(trigPin,echoPin);
Then, we set up these objects, declare our pin modes and start our serial monitor in the void setup.
void setup(){
Serial.begin(9600);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
myServo.attach(servoPin);
myServo.write(servoPos);
pinMode(buzzPin,OUTPUT);
}
In the void loop, we get the distance in centimeters and check whether the distance is greater or lesser than 15cm.
d = sonar.ping_cm();
We set the servo to move 120 degrees if the distance is lesser than 15cm using a simple if-else statement. Finally, we write the servo position to the motor.
if(d < 15){
servoPos = 120;
digitalWrite(buzzPin,HIGH);
delay(100);
digitalWrite(buzzPin,LOW);
}
else{
servoPos = 0;
}
digitalWrite(buzzPin,LOW);
myServo.write(servoPos);
Note: To measure the distance using the ultrasonic distance sensor, you don't necessarily have to use the New Ping library, you can create a pulse in the trigger and echo pins and calculate the distance to the target (in this case, our hand). You can use this piece of code to create a pulse in the trigger pin and listen to the ping in the echo pin.
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
delayMicroseconds(10);
pingTime = pulseIn(echoPin,HIGH);
pingTime /= 10000;
d = pingTime*343/2;
The theoryUltrasonic distance sensor: The ultrasonic distance sensor sends out an ultrasonic pulse from the transmitter. This is called a "ping". The receiver "listens" to this ping and calculates the time it took to reach after transmission. We can calculate the distance by using this formula (assuming the speed of sound in air at 25⁰C = 343 m/s)
v = 2d/t
Where
- v - speed of sound in air
- d - distance from the sensor to object
- t - time taken for the ping to be detected by the receiver
on re-arrangement, we get
d = v*t/2
The distance we get is in meters [m]. So, we can multiply the distance by 100 to get the distance in centimeters [cm].
Servo motor: The servo motor is a special kind of motor that can turn to a specific angle on command. It is very simple to hook up and code. We will make use of the Servo.h
library for this. In this project, we will use the servo motor to pull the lever of the hand sanitizer.
- Take a piece of cardboard and fold it to make a box-like structure with appropriate dimensions to house the breadboard.
- Cut a hole in the case to accommodate the switch.
- Make 2 holes to accommodate the ultrasonic distance sensor, and attach it to the box. Remember to cut the cardboard with a larger breadth than the original box, so that it can be attached to the box with a flap.
- Make 2 holes on the lever of the sanitizer
- Put a sturdy copper wire through these holes
- Attach one end of the wire to the holes in the horn of the servo, and firmly fix the other end to the opposite side of the sanitizer.
- Attach a piece of foam to the side of the sanitizer bottle, and pass the free end of the copper wire through it.
- Bend the wire so that it is parallel to the wire on the other side.
Note: make sure that you maintain the tension in the wire as you attach it to the servo and the sanitizer.
- Get the servo library here
- Get the new ping library here
You can either download the libraries from these sites, or you can install them from your Arduino IDE. Just go to tools -> manage libraries. A window will pop up, and you can search and download the library you want.
Comments
Please log in or sign up to comment.