Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

VISHWAKARMA INSTITUTE OF TECHNOLOGY

Department of Engineering, Sciences and Humanities


FY – 2020-2021 -SEM 2 – ES1039: Mechatronics and Robotics
==================================================================================

EXPERIMENT NUMBER = 5 DATE = 1/7/2021

TITLE OF THE EXPERIMENT = Interfacing Ultrasonic sensor and Servo motor with Arduino

NAME OF THE STUDENT= Tejas Sarjerao Dharmik

GR. NO. = 12011252 Div = D ROLL NO = 53 BATCH = B3


==================================================================================

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: -

1. Interfacing Arduino with Ultrasonic sensor

2. Interfacing Arduino with Servo motor

3.Combined control of Ultrasonic sensor and Servo motor

Components / Apparatus required: - Tinkercad Software, servo motor, ultrasonic sensor

Circuit: -
Methodology (Steps for constructing circuit): -

Brief explanation about steps done in each task

Task 1:- Interfacing Arduino with Ultrasonic sensor

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

3.Then we have printed the calculated distance on serial monitors

Task 2:- Interfacing Arduino with Servo motor

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

3.Finally we see the motor rotating

Task 3:- Combined control of Ultrasonic sensor and Servo motor

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)

Task 1 Arduino Code :- Interfacing Arduino with Ultrasonic sensor

#define trig A5

#define echo A4

long time, dist;

void setup()

pinMode (trig, OUTPUT);

pinMode (echo, INPUT);

Serial.begin(9600);

void loop ()

digitalWrite (trig, LOW);

delay (2);

digitalWrite (trig, HIGH);

delayMicroseconds (10);

digitalWrite (trig, LOW);

time = pulseIn (echo, HIGH);

dist = time / 2 / 29.1; // 0.0343 does not work

Serial.print("Distance measured in cm is ");

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

long time, dist;

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);

dist = time / 2 / 29.1;

Serial.print("Distance measured in cm is ");

Serial.print(dist);

unsigned char pos = map(dist, 3,330,0,180);

servo.write(pos);

Serial.print(" & Angular Position of Servo motor in degreses is ");

Serial.println(pos, DEC);

delay(1000);

}
Result (serial monitor, circuit, output images 4/5 ): -

Task 1:- Interfacing Arduino with Ultrasonic sensor

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.

You might also like