You are on page 1of 7

Lab Handout #2: Designing a free roaming, obstacle avoiding mobile robot

Instructor: Dr. Afaque Manzoor Soomro

Name: UNSA JAN


CMS ID: 033-19-0048
Semester: VII

Department of Electrical Engineering


TEL-413: Introduction to Robotics
Rubrics for Lab Task Assessment

Rubrics for Marks Breakdown


Demonstration
Design Manual Total (Out of 100)
Manual

Criteria 100% 70% 30% 0%


Design (40%) Designed as per Partially designed Not designed as Code not
instructions. as per instructions. per instructions. submitted.
Demonstration Successfully Successfully Demonstrated Failed to
(40%) demonstrated the demonstrated with with Major demonstrate the
task as intended. minor mistakes. mistakes. project.
Manual (20%) The manual The manual The manual not The manual not
submitted on time submitted on time submitted on time submitted.
with proper without proper but with proper
solution code. solution code. solution.
Lab Objective: Designing an autonomous mobile robot that roams around freely & avoids
collisions

Required components:
1. Robot chassis with motors
2. Battery
3. Motor Driver circuit board
4. Arduino/Pi/ESP32 Dev Module with data cable
5. Connecting wires
6. PC (Arduino IDE installed)
7. IR sensor
8. Ultrasonic sensor (HC-SR04)

The robot should roam freely in its environment and avoid any collision. For the purpose, we need
to use sensors with the robot that can detect objects or hindrance in-front of the robot. The robot
should change its direction to avoid the obstacle.

Sensors:
i) Infra Red (IR) Sensor can be used to detect the objects in-front of the robot and avoid the
collision. An IR LED-Sensor pair can help to detect the object. The IR led (transmitter) emits the IR
signals which are reflected by any nearby object in the straight direction of IR transmitter. If there is
any object in-front of the receiver, the transmitted light will be reflected and received by the sensor.
In case of no object, IR sensor will not receive any light.

The IR sensor used in this lab consist of three pins; Vcc, GND & Out. In order to interface with the
controller (Arduino/ESP32), the sensor pin “Out” will be connected to one of the pins in
Arduino/ESP. In case of no object in-front of the sensor, the “Out” pin will provide logic “High”
and “Low” if there is an object.
________________________________________________________________________________
int input_pin = 4;
int noObstacle = HIGH;
void setup() {
Serial.begin(115200);
pinMode(input_pin, INPUT);
}

void loop() {
noObstacle = digitalRead(input_pin) ;
if (noObstacle == LOW) {
Serial.println("OBSTACLE!!, OBSTACLE!!");
} else {
Serial.println("clear");
}
}
________________________________________________________________________________
You can see the printed messages on serial monitor. Click on “Tools → Serial Monitor” in order to
open a serial monitor window.

ii) Ultrasonic Sensor can also be used to detect the objects. The sensor is used in pair with a
ultrasonic signal transmitter. The transmitter, transmits the sound waves of high frequency and the
receiver receives the returned sound waves after colliding with an object in the path. The distance
between the sensor and the object can be calculated using the time taken by the sound waves during
the journey.

Ultrasonic sensor module contains 4 pins; GND, Echo, Trig and Vcc. Trig (Trigger) pin as set as
output as it needs a “HIGH” logic to transmit the ultrasonic signal. Echo pin is set be as input and
total number of waves are counted. The echo pin at the end returns the time (in micro seconds)
taken by the waves from transmitter to the receiver. The time can be used to calculate the distance,
as we know the velocity of the sound waves (340m/s).
________________________________________________________________________________
int trigPin = 22;
int echoPin = 23;

// defines variables
long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds


digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance


distance= duration*0.034/2;

// Prints the distance on the Serial Monitor


Serial.print("Distance in cm: ");
Serial.println(distance);
}
________________________________________________________________________________

Practice Task: Write the program for obstacle avoiding robot using IR sensor that moves in
straight direction and changes path to 90 degree whenever an object is detected in the path.
CODE
int left_wheelA = 5; int left_wheelB = 11;
int right_wheelA = 6; int right_wheelB = 10;
int noObstacle = HIGH;
int input_pin = 4;
void setup() {
Serial.begin(115200);
pinMode(input_pin, INPUT);
pinMode(left_wheelA, OUTPUT); pinMode(left_wheelB, OUTPUT);
pinMode(right_wheelA, OUTPUT); pinMode(right_wheelB, OUTPUT);
}
void loop() {
noObstacle = digitalRead(input_pin) ;
if (noObstacle == LOW) {
Serial.println("OBSTACLE!!, OBSTACLE!!");
analogWrite(left_wheelA, 70); analogWrite(left_wheelB, LOW);
analogWrite(right_wheelA, HIGH); analogWrite(right_wheelB, LOW);
} else {
Serial.println("clear");
analogWrite(left_wheelA, 40); analogWrite(left_wheelB, 40);
analogWrite(right_wheelA, LOW); analogWrite(right_wheelB, LOW);
}
}

Exercise Task: Write a program for obstacle avoiding robot using ultrasonic sensor that roams
around freely and changes path to any random direction whenever an object is detected in the path.
Code
int left_wheelA = 5; int left_wheelB = 10;
int right_wheelA = 6; int right_wheelB = 9;

int trigPin = 4;
int echoPin = 2;
// defines variables
long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200); // Starts the serial communication
pinMode(left_wheelA, OUTPUT); pinMode(left_wheelB, OUTPUT);
pinMode(right_wheelA, OUTPUT); pinMode(right_wheelB, OUTPUT);
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds


digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance


distance= duration*0.034/2;

// Prints the distance on the Serial Monitor


Serial.print("Distance in cm: ");
Serial.println(distance);
if(distance<20)
{
analogWrite(left_wheelA, 70); analogWrite(left_wheelB, LOW);
analogWrite(right_wheelA, HIGH); analogWrite(right_wheelB, LOW);
}
else
{
analogWrite(left_wheelA, 70); analogWrite(left_wheelB, 70);
analogWrite(right_wheelA, HIGH); analogWrite(right_wheelB, LOW);
}
}

Exercise Question: Which of the two sensors performed better? Why do you think it performed
better?
Answer:
Out of these two sensors, ultrasonic sensor performed better. It happened because, IR sensor
had very low range of detecting object and nor it had any flexibility to set the range. So even
though we set very low speed but before it could stop while detecting any object at distant
position but it was getting too close to move or change its direction. On the other hand,
Ultrasonic sensor had the feasibility of setting range for detecting object and then of stopping
at a distance and move or make a turn accordingly.

You might also like