Devices & Components
Arduino Uno Rev3
Ultrasonic Sensor - HC-SR04 (Generic)
Breadboard (generic)
SG90 Micro-servo motor
Jumper wires (generic)
Project description
Code
Arduino IDE code
arduino
1#include <Servo.h> 2/*contains 3 * attach() 4 * write() 5 * writeMicroseconds() 6 * read() 7 * attached() 8 detach()*/ 9#define trigPin 3 10#define echoPin 2 11 12 13Servo servo1; 14Servo servo2; 15int sound = 250; 16 17void setup() 18 { 19 Serial.begin (9600); 20 pinMode(trigPin, OUTPUT); 21 pinMode(echoPin, INPUT); 22 servo1.attach(4); 23 servo2.attach(5); 24 } 25void loop() 26 { 27 long duration, distance; 28 digitalWrite(trigPin, LOW); 29 delayMicroseconds(2); 30 digitalWrite(trigPin, HIGH); 31 delayMicroseconds(10); 32 digitalWrite(trigPin, LOW); 33 duration = pulseIn(echoPin, HIGH); 34 distance = (duration/2) / 29.1; 35 if (distance < 5) 36 { 37 Serial.println("the distance is less than 5, dettol liquid dispensed"); 38 servo1.write(100); 39 /*will rotate servo1 to 100degress clockwise*/ 40 servo2.write(100); 41 /*will rotate servo2 to 100degress clockwise*/ 42 } 43 else 44 { 45 servo1.write(0); 46 servo2.write(0); 47 /*no effect on the two servos*/ 48 } 49 if (distance > 60 || distance <= 0) 50 { 51 Serial.println("The distance is more than 60, move your hand closer"); 52 } 53 else 54 { 55 Serial.print(distance); 56 Serial.println(" cm"); 57 } 58 delay(500); 59}
Downloadable files
Circuit Diagram #1
representing connection of sensor, arduino & servos.
Circuit Diagram #1

Circuit Diagram #1
representing connection of sensor, arduino & servos.
Circuit Diagram #1

Comments
Only logged in users can leave comments