LabHandout#3:Designingalinefollowingrobot(LFR)usingLDRandIR
Instructor:Dr.GhulamAbbasLashari
Name: Hammad
CMS ID: 133-21-0009
Semester: CSE-VIII
DepartmentofElectricalEngineering
TEL-413:Robotics
RubricsforLabTask Assessment
RubricsforMarks Breakdown
Demonstration
Design Manual Total (Out of
Manual 100)
Needs
Excellent(90- Satisfactory(50- Obtai
Criteria Scor Good(75- Improvement(
100%) 74%) ned
e 89%) 0-
scor
49%)
e
Resultsareaccurat Results are Results
Resultsaresomewhat
e, well- mostlyaccur are
Results 0.5 accurate, with some
documented, and ate with inaccurate
errors.
thoroughly minor errors. or poorly
analyzed. document
ed.
Well-
Adequatelyorganized
organized Poorlyorganized
Repo Exceptionallywe but with noticeable
0.5 with minor with major
rt ll- structural or
issues in structuralissues.
Struct organized. grammatical issues.
structure or
ure
grammar.
LabObjective:Designinganautonomousmobilerobotthatfollowsalineonthe ground
The robot should follow a path given by a line. For the purpose, we need to use sensors with the
robot that can detect the line in the path. The robot should be able to make turns through following
the line.
Requiredcomponents:
1. Robotchassiswith motors
2. Battery
3. MotorDrivercircuit board
4. ESP32Dev Modulewith datacable
5. Connectingwires
6. PC(ArduinoIDE installed)
7. IRsensor
8. LDRandresistors
Sensors:
i) Infra Red (IR) Sensor can be used to detect the line on the ground (path). An IR LED-Sensor
pair can help to detect the line. The IR led (transmitter) emits the IR signals which are reflected by
any coloured path, except black. If there is a line in black colour in-front of the receiver, the
transmitted light will be not be reflected and not received by the sensor. In case the robot will
identify its path.
The IR sensor module used in this lab consist of three pins; Vcc, GND & Out. In order to interface
with the controller (Arduino/ESP), the sensor pin “Out” will be connected to one of the pins in
Arduino/ESP. In case of line in-front of the sensor, the “Out” pin will provide logic “High” and
“Low” if there is no line. You may need to use more than one IR sensors for proper detection. .
Make the connections of the IR sensor as per sample code below.
int Input_pin = 4;
inton_line=HIGH;
void setup() {
pinMode(Input_pin,INPUT);
Serial.begin(9600);
}
void loop(){
on_line=digitalRead(Input_pin); if
(on_line == LOW) {
Serial.println("Noline!!,Line!!");
}else {
Serial.println("Linefound");
}
}
ii) IR Array This is a Five IR Sensor Array with Obstacle and Bump Sensor. A 5 IR sensor array
has a compact construction where the emitting-light source and the detector are arranged in thesame
direction to sense the presence of an object by using the reflective IR-beam from the object. The
detector consists of a phototransistor. There are 5 sensors connected in an array which can be used
effectively in different robotic applications such as, Line following robot, Shaft encoder, Obstacle
avoidance use. Make the connections of the array as per sample code below.
intS2=18;
intS3=19;
int S4 =21;
int left = HIGH;
intcentre=HIGH;
int right = HIGH;
void setup() {
pinMode(S2,INPUT);
pinMode(S3,INPUT);
pinMode(S4,INPUT);
Serial.begin(9600);
}
void loop()
{
left = digitalRead(S2);
centre=digitalRead(S3);
right = digitalRead(S4);
if(left==HIGH &¢re== LOW&&right==HIGH)
{
Serial.println("ONLine,Gostraight!!");
}
elseif(left== LOW&¢re==HIGH &&right== HIGH)
{
Serial.println("Movingonrightside,turnleft");
}
elseif(left==HIGH&¢re==HIGH &&right== LOW)
{
Serial.println("Movingonlefttside,turn right");
}
else
{
Serial.println("NoLinefound");
}
}
Youmayneed to add moreif-then-else statements in order to makeproper linedetection.
iii) Light Dependent Resistor can also be used to design LFR. An LDR is a variable resistor that
changes its resistance with the light intensity that falls upon it. This allows it to be used in light
sensing circuits. A circuit can be designed to create logic “High” or “Low” depending upon the
resistance.
Make a sample circuit to read analogue values through ESP32. The circuit consist of a series
connection a resistor and an LDR. Outer terminals of the circuit are connected to 3.3V (Vs) and
GND pins. The middle (common, Vo) terminal is connected to an ADC channel pin.
constintAnalog_channel_pin=36; int
ADC_VALUE = 0;
intvoltage_value=0; void
setup()
{
Serial.begin(9600);
}
void loop()
{
ADC_VALUE= analogRead(Analog_channel_pin);
Serial.print("ADCVALUE=");
Serial.println(ADC_VALUE);
Serial.println("");
delay(1000);
voltage_value=(ADC_VALUE*3.3)/(4095);
Serial.print("Voltage = ");
Serial.print(voltage_value);
Serial.print("volts");
Serial.println("");
delay(1000);
}
Practice Task:Write the program forLFR, using IR sensormodule,which follows the line inany
given direction.
CIRCUIT DIAGRAM:
CODE:
const int S2 = 3;
const int S3 = 4;
const int S4 = 5;
const int motor1Forward = 6;
const int motor1Reverse = 9;
const int motor2Forward = 10;
const int motor2Reverse = 11;
int left = HIGH;
int centre = HIGH;
int right = HIGH;
void setup() {
pinMode(S2, INPUT);
pinMode(S3, INPUT);
pinMode(S4, INPUT);
pinMode(motor1Forward, OUTPUT);
pinMode(motor1Reverse, OUTPUT);
pinMode(motor2Forward, OUTPUT);
pinMode(motor2Reverse, OUTPUT);
Serial.begin(9600);
}
void loop() {
left = digitalRead(S2);
centre = digitalRead(S3);
right = digitalRead(S4);
if (left == HIGH && centre == LOW && right == HIGH) {
Serial.println("ON Line, Go straight!!");
moveForward();
}
else if (left == LOW && centre == HIGH && right == HIGH) {
Serial.println("Moving on right side, turn left");
turnLeft();
}
else if (left == HIGH && centre == HIGH && right == LOW) {
Serial.println("Moving on left side, turn right");
turnRight();
}
else if (left == LOW && centre == LOW && right == LOW){
Serial.println("No Line found, stopping");
stopMotors();
}
}
void moveForward() {
analogWrite(motor1Forward, 130);
analogWrite(motor1Reverse, 0);
analogWrite(motor2Forward, 130);
analogWrite(motor2Reverse, 0);
}
void turnLeft() {
analogWrite(motor1Forward, 0);
analogWrite(motor1Reverse, 130);
analogWrite(motor2Forward, 130);
analogWrite(motor2Reverse, 0);
}
void turnRight() {
analogWrite(motor1Forward, 130);
analogWrite(motor1Reverse, 0);
analogWrite(motor2Forward, 0);
analogWrite(motor2Reverse, 130);
}
void stopMotors() {
analogWrite(motor1Forward, 0);
analogWrite(motor1Reverse, 0);
analogWrite(motor2Forward, 0);
analogWrite(motor2Reverse, 0);
}
VIDEO LINK: https://drive.google.com/file/d/1qct25O1kmPD30cybcSJL4A-IxwZ3kKX3/view?
usp=sharing
EXPLAINATION:In following circuit 3 ir sensors are used named left center and right,connected to
digital pins of arduino board.if the center ir sensor detects the black tape then the motors move
straight,also if the robot deflects and moves in either left or right then it turns accordingly.for
example if the left sensor detects logic HIGH meaning it has deflected towrds right so the arduino
board turns the robot to left by reversing the left motor and forwarding the right motor to result in a
turn,same happens for right sensor,if it deflects to left and detects the black tape meaning it has
deflected to left and now the arduino will make the right turn to move the robot to center and move
straight back.
ExerciseTask:DesigntheLFR,usingLDRsensor,whichfollowsthelineinanygiven direction.
CIRCUIT DIAGRAM:
CODE:
const int motor1Forward = 6;
const int motor1Reverse = 9;
const int motor2Forward = 10;
const int motor2Reverse = 11;
const int motorSpeed = 70;
const int leftLDR = A3;
const int rightLDR = A0;
int leftADC_VALUE = 0;
int rightADC_VALUE = 0;
// Thresholds for detecting black tape(vary by environment’s light intensity)
const int leftThreshold = 1007;
const int rightThreshold = 1010;
void setup() {
pinMode(motor1Forward, OUTPUT);
pinMode(motor1Reverse, OUTPUT);
pinMode(motor2Forward, OUTPUT);
pinMode(motor2Reverse, OUTPUT);
Serial.begin(9600);
}
void loop() {
leftADC_VALUE = analogRead(leftLDR);
rightADC_VALUE = analogRead(rightLDR);
Serial.print("Left ADC VALUE = ");
Serial.println(leftADC_VALUE);
Serial.print("Right ADC VALUE = ");
Serial.println(rightADC_VALUE);
// If both sensors see white (robot centered), drive straight.
if (leftADC_VALUE < leftThreshold && rightADC_VALUE < rightThreshold) {
driveForward();
}
// If left sensor detects black (robot is drifting right), slow the left motor to steer left.
else if (leftADC_VALUE >= leftThreshold && rightADC_VALUE < rightThreshold) {
adjustLeft();
}
// If right sensor detects black (robot is drifting left), slow the right motor to steer right.
else if (rightADC_VALUE >= rightThreshold && leftADC_VALUE < leftThreshold) {
adjustRight();
}
else {
driveForward();
}
delay(100);
}
void driveForward() {
analogWrite(motor1Forward, motorSpeed);
analogWrite(motor1Reverse, 0);
analogWrite(motor2Forward, motorSpeed);
analogWrite(motor2Reverse, 0);
}
void adjustLeft() {
analogWrite(motor1Forward, motorSpeed / 2);
analogWrite(motor1Reverse, 0);
analogWrite(motor2Forward, motorSpeed);
analogWrite(motor2Reverse, 0);
}
void adjustRight() {
analogWrite(motor1Forward, motorSpeed);
analogWrite(motor1Reverse, 0);
analogWrite(motor2Forward, motorSpeed / 2);
analogWrite(motor2Reverse, 0);
}
VIDEO LINK: https://drive.google.com/file/d/1qEgrc7Gmj4C5WACZ6SFngTe1zhKEAdAO/view?
usp=sharing
EXPLAINATION:the following ciruit contains 2 Ldr sensor known as photoresistors wired to
analog pins of arduino which are used to detect whether the robot is following the line or
not.different environment light varies the threshold,in our case the left was 1007 meaning if it
detects greater than that means it has deflected to right detecting the black tape and if the right ldr
senses greater than 1010 meaning it has deflected to left detecting the black tape.if both thresholds
are less then arduino makes our robot move forward,if the left threshold is triggered then it moves to
left and same for right ldr,if the right threshold is triggered then arduino makes a right turn insuring
the robot follows the line.
ExerciseQuestion:Whichsensorwasabestchoicetodesign theLFR? Why?
Answer:The IR sensor was the best choice because it detects the line quite correctly as compared
to the LDR sensors which were not not detecting the line correctly.since the LDR is effected by sunlight,roomlight
etc so we have to adjust the thresholds accordingly for different environments to make the robot work.
ExerciseCode
RubricsforLabTask Assessment
RubricsforMarks Breakdown
TaskCompletio ManualSubmission Total (Out of
n 10)
80% 20 % 100 %
Criteria Accurately Partially None
Design (40%) Designedasper Designedasper Notdesignedasper
instructions instructions instructions
Demonstration(40%) Successfully Successfully Failedtodemonstrate
demonstrated and demonstratedbutfailed the project.
answered the asked to answer the asked
questionsduringthe questions during the
demonstration. demonstration.
Manual (20%) Themanualsubmitted Themanualsubmitted Themanualnot
on time with the on time without the submitted.
solution code. solution code.