Materials:-
- Arduino uno = https://bit.ly/30v5XcY
- Servo motor = https://bit.ly/37qHusR
- Ultrasonic sensor = https://bit.ly/2XXkzCD
Diagram:-
Connections:-
Ultrasonic sensor to arduino
- Ultrasonic GND = Arduino GND
- Ultrasonic Trig = Arduino D6
- Ultrasonic Echo = Arduino D7
- Ultrasonic VCC = Arduino 3.3V
Servo motor to arduino
- Servo Black/brown = Arduino GND
- Servo Red = Arduino 5V
- Servo yellow = Arduino D8
Downloads:-
- Servo library(servo.h) = https://bit.ly/37qAkVs
- Code = https://bit.ly/2AvpwJP
Code#include <Servo.h> //servo library#define trigpin 6#define echopin 7int servoPin = 8;Servo servo;void setup(){pinMode(trigpin, OUTPUT);pinMode(echopin, INPUT);servo.attach(servoPin);servo.write(0);delay(100);servo.detach();}void loop(){int duration, distance;digitalWrite(trigpin, HIGH);delayMicroseconds(1000);digitalWrite(trigpin, LOW);duration = pulseIn(echopin, HIGH);distance = ( duration / 2) / 29.1;if ( distance<50 ) {servo.attach(servoPin);delay(1);servo.write(150);delay(4000);servo.write(0);delay(2000);servo.detach();}}