Techatronic
Published

How to make automatic soap dispenser

This is an automatic soap dispenser project that we made using Arduino and servo motors.

BeginnerProtip2 hours3,252
How to make automatic soap dispenser

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×2
Ultrasonic Sensor - HC-SR04
SparkFun Ultrasonic Sensor - HC-SR04
×1
9V battery (generic)
9V battery (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
USB-A to B Cable
USB-A to B Cable
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

CODE

C/C++
#include <Servo.h>
Servo myservo;
Servo myservo1;// create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0;
int duration_front=0;
long distance_front=0;
void setup() {
// put your setup code here, to run once:
myservo.attach(9);
myservo1.attach(10);
myservo.write(180);
myservo1.write(180);
pinMode(2,OUTPUT);
pinMode(3,INPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(2, HIGH);
delayMicroseconds(10);
digitalWrite(2, LOW);
delayMicroseconds(2);
duration_front= pulseIn(3,HIGH);
distance_front=duration_front*0.034/2;
Serial.println(distance_front);
if(distance_front<=10)
{
// put your main code here, to run repeatedly:
myservo.write(180); // tell servo to go to position in variable ‘pos’
myservo.write(0); // tell servo to go to position in variable ‘pos’
delay(500); // waits 15ms for the servo to reach the position
myservo.write(180); // tell servo to go to position in variable ‘pos’
delay(500); // waits 15ms for the servo to reach the position
// waits 15ms for the servo to reach the position
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(113); // waits 15ms for the servo to reach the position
}
}
}

Credits

Techatronic
73 projects • 132 followers
Electronic engineer
Contact

Comments

Please log in or sign up to comment.