Professional Documents
Culture Documents
D53 Mar Lab 5
D53 Mar Lab 5
TITLE OF THE EXPERIMENT = Interfacing Ultrasonic sensor and Servo motor with Arduino
Aim: - To interface Arduino with servo motor and ultrasonic sensor to find how long a particle is
from the spot its angular position etc.
Tasks: -
Circuit: -
Methodology (Steps for constructing circuit): -
1.Here first we defined the pins and initialized variables time and dist
2.Then we have written digital low and high so that we will be able to measure the distance at which
object is placed in particular range of ultrasonic sensor
1.Here we were supposed to rotate servo at particular angles after a particular delay so firstly we
imported libraries and then used servo.attach(pin,min,max).
2.Then we have written a code so as to make the servo motor at particular angles by using the
commands in code and giving a small delay over there
1.Combined circuit of servo motor and ultrasonic sensor in which the motor rotates at particular
angles when object changes it’s positions in the ultrasonic sensors.
2.So we have initialized pins where the servo is connected and the pins where ultrasonic sensor is
connected
3.Then we will measure distance of object in range of sensor by making some calculations
4.Also we will make servo motor to rotate as per the distance of the object
Code :- Copy and Paste complete code of each task (written in Arduino window)
#define trig A5
#define echo A4
void setup()
Serial.begin(9600);
void loop ()
delay (2);
delayMicroseconds (10);
Serial.print(dist);
Serial.print("\n");
delay(10000);
}
Task 2 Arduino Code :- Interfacing Arduino with Servo motor
# include <Servo.h>
Servo myservo;
void setup(){
myservo.attach(9,600, 2300);
void loop(){
myservo.write(0);
delay(1000);
myservo.write(90);
delay(500);
myservo.write(135);
delay(500);
myservo.write(180);
delay(1500);
}
Task 3 Arduino Code :- Combined control of Ultrasonic sensor and Servo motor
#include<Servo.h>
Servo servo;
#define trig A5
#define echo A4
#define servP 10
void setup()
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
Serial.begin(9600);
servo.attach(servP);
void loop()
digitalWrite(trig,LOW);
delay(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
time = pulseIn(echo,HIGH);
Serial.print(dist);
servo.write(pos);
Serial.println(pos, DEC);
delay(1000);
}
Result (serial monitor, circuit, output images 4/5 ): -
Before simulation :
After Simulation :
Task 2 :- Interfacing Arduino with Servo motor
Before Simulation :
After Simulation :
Task 3:- Combined control of Ultrasonic sensor and Servo motor
Before Simulation :
After Simulation :
Conclusion : We successfully studied to Interface the Ultrasonic sensor and Servo motor with
Arduino.