0% found this document useful (0 votes)
2 views

Line Follower Robot using Arduino and IR Sensors

The document describes the design and implementation of a Line Follower Robot using Arduino and IR sensors, which autonomously follows a black line on a white surface. It includes a block diagram, component list, hardware connections, and a flowchart detailing the robot's operation and decision-making process based on sensor inputs. The document also provides a complete code example for controlling the robot's movements and behavior.

Uploaded by

adeivasundaram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Line Follower Robot using Arduino and IR Sensors

The document describes the design and implementation of a Line Follower Robot using Arduino and IR sensors, which autonomously follows a black line on a white surface. It includes a block diagram, component list, hardware connections, and a flowchart detailing the robot's operation and decision-making process based on sensor inputs. The document also provides a complete code example for controlling the robot's movements and behavior.

Uploaded by

adeivasundaram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Line Follower Robot using Arduino and IR Sensors

Introduction
A Line Follower Robot is an autonomous robot that can follow a path (usually a
black line on a white surface) without human intervention. It detects the line
using infrared (IR) sensors and makes decisions to move forward, turn left, or
right based on sensor inputs. This type of robot is widely used in industrial
automation, warehouse management, and educational projects.
Block Diagram
+------------------------+
| |
| IR Sensors |
| (Left & Right Sensors) |
+-----------+------------
+-------+--------+
| |
| Arduino |
| (Controller) |
+-------+--------+
+-----------+------------+
| |
| Adafruit Motor Shield |
| (Motor Shield) |
+-----------+------------+
+----------+-----------+

| DC Motors (L & R) |
| (Left and Right) |
+----------------------+
The block diagram consists of the following basic blocks:
 Arduino UNO: The main controller that reads the sensors and controls the
motors.
 IR Sensors (Left and Right): Used to detect the black line.
 Motor Driver (Adafruit Motor Shield): Controls the speed and direction of
two DC motors based on commands from the Arduino.
 DC Motors (Left and Right Motors): Move the robot forward, left, or right.
 Chassis: Physical body holding all components together.
Working:
 Sensors continuously monitor the surface.
 Arduino processes the sensor inputs:
o If both sensors detect white, robot moves forward.
o If left sensor detects black, robot turns left.
o If right sensor detects black, robot turns right.
o If both sensors detect black, robot stops.
Component List

S.No Component Quantity

1 Arduino UNO 1

Adafruit Motor Shield


2 1
V1

3 IR Sensor Modules 2

4 BO-type DC Motors 2

5 Robot Chassis 1

6 Wheels 2

7 Castor Wheel 1

Battery Pack
8 1
(6V/12V)

As
9 Jumper Wires
needed

Black Line Track


10 1 roll
(Tape)

Hardware Connections
 Left IR sensor output → Arduino analog pin A0.
 Right IR sensor output → Arduino analog pin A1.
 Motor Shield connections:
o Left Motor → M3 (Motor 3 pins)
o Right Motor → M4 (Motor 4 pins)
 Motor Shield mounted onto Arduino UNO.
 Power Supply:
o Motor shield powered through external battery (6V–12V).
o Arduino powered via USB (for testing) or external power supply.
Detailed Hardware Description
Arduino UNO
Arduino is the brain of the project. It processes inputs from IR sensors and
controls motor outputs through the motor shield.
IR Sensors
IR sensors consist of an IR LED and a photodiode. When placed over a white
surface, most infrared light is reflected, and the sensor reads a low value. When
placed over a black line, less light is reflected, and the sensor reads a higher
value.
Adafruit Motor Shield V1
The motor shield is used to easily drive two DC motors with independent speed
and direction control. It uses internal H-bridge circuits to manage the motors.
BO-type DC Motors
These motors convert electrical energy into mechanical motion. In this setup,
two motors are used — one for each wheel.
Chassis
The body structure that holds motors, sensors, Arduino, shield, and battery.
Flowchart
[Start]

[Initialize Motors to 0 Speed and RELEASE]

[Move Motors FORWARD at full speed for 40 ms]

[Loop Start]

[Read Left IR Sensor Value]
[Read Right IR Sensor Value]

[Right Sensor detects black (right_value ≥ DETECT_LIMIT) AND Left Sensor does
NOT?]

┌──────────────Yes──────────────┐
│ ↓
│ [turnRight()]
│ ↓
│ [Delay]
│ ↓
│ [Back to Loop]

No

[Left Sensor detects black (left_value ≥ DETECT_LIMIT) AND Right Sensor does
NOT?]

┌──────────────Yes──────────────┐
│ ↓
│ [turnLeft()]
│ ↓
│ [Delay]
│ ↓
│ [Back to Loop]

No

[Neither Sensor detects black?]

┌──────────────Yes──────────────┐
│ ↓
│ [moveForward()]
│ ↓
│ [Back to Loop]

No

[Both Sensors detect black?]

┌──────────────Yes──────────────┐
│ ↓
│ [stop()]
│ ↓
│ [Back to Loop]

No

[Back to Loop]
Code Explanation
The code continuously reads values from the left and right IR sensors. Based on
the readings:
 If both sensors detect no line (white surface) → move forward.
 If left sensor detects line and right does not → turn left.
 If right sensor detects line and left does not → turn right.
 If both sensors detect line (both black) → stop the robot.
Motor speed and directions are carefully controlled using the Adafruit Motor
Shield commands.
Full Code
#include <AFMotor.h>

// MACROS for Analog Input


#define LEFT_IR A0
#define RIGHT_IR A1
// MACROS to control the Robot
#define DETECT_LIMIT 300
#define FORWARD_SPEED 60
#define TURN_SHARP_SPEED 150
#define TURN_SLIGHT_SPEED 120
#define DELAY_AFTER_TURN 140
#define BEFORE_TURN_DELAY 10

// BO Motor control related data here


// Here motors are running using M3 and M4 of the shield and Left Motor is
connected to M3 and Right Motor is connected to M4 using IC2 of the shield
AF_DCMotor motorL(3); // Uses PWM0B pin of Arduino Pin 5 for Enable
AF_DCMotor motorR(4); // Uses PWM0A pin of Arduino Pin 6 for Enable

// variables to store the analog values


int left_value;
int right_value;

// Set the last direction to Stop


char lastDirection = 'S';

void setup() {
// Set the current speed of Left Motor to 0
motorL.setSpeed(0);
// turn off motor
motorL.run(RELEASE);
// Set the current speed of Right Motor to 0
motorR.setSpeed(0);
// turn off motor
motorR.run(RELEASE);

// To provide starting push to Robot these values are set


motorR.run(FORWARD);
motorL.run(FORWARD);
motorL.setSpeed(255);
motorR.setSpeed(255);
delay(40); // delay of 40 ms
}

void loop() {
left_value = analogRead(LEFT_IR);
right_value = analogRead(RIGHT_IR);

// Right Sensor detects black line and left does not detect
if (right_value >= DETECT_LIMIT && !(left_value >= DETECT_LIMIT)) {
turnRight();
}
// Left Sensor detects black line and right does not detect
else if ((left_value >= DETECT_LIMIT) && !(right_value >= DETECT_LIMIT)) {
turnLeft();
}
// both sensors doesn't detect black line
else if (!(left_value >= DETECT_LIMIT) && !(right_value >= DETECT_LIMIT)) {
moveForward();
}
// both sensors detect black line
else if ((left_value >= DETECT_LIMIT) && (right_value >= DETECT_LIMIT)) {
stop();
}
}

void moveForward() {
if (lastDirection != 'F') {
// To provide starting push to Robot when last direction was not forward
motorR.run(FORWARD);
motorL.run(FORWARD);
motorL.setSpeed(255);
motorR.setSpeed(255);
lastDirection = 'F';
delay(20);
} else {
// If the last direction was forward
motorR.run(FORWARD);
motorL.run(FORWARD);
motorL.setSpeed(FORWARD_SPEED);
motorR.setSpeed(FORWARD_SPEED);
}
}

void stop() {
if (lastDirection != 'S') {
// When stop is detected move further one time to check if its actual stop or
not, needed when the robot turns
motorR.run(FORWARD);
motorL.run(FORWARD);
motorL.setSpeed(255);
motorR.setSpeed(255);
lastDirection = 'S';
delay(40);
} else {
// When stop is detected next time then stop the Robot
motorL.setSpeed(0);
motorR.setSpeed(0);
motorL.run(RELEASE);
motorR.run(RELEASE);
lastDirection = 'S';
}
}

void turnRight(void) {
// If first time Right Turn is taken
if (lastDirection != 'R') {
lastDirection = 'R';
// Stop the motor for some time
motorL.setSpeed(0);
motorR.setSpeed(0);
delay(BEFORE_TURN_DELAY);
// take Slight Right turn
motorL.run(FORWARD);
motorR.run(BACKWARD);
motorL.setSpeed(TURN_SLIGHT_SPEED);
motorR.setSpeed(TURN_SLIGHT_SPEED);
} else {
// take sharp Right turn
motorL.run(FORWARD);
motorR.run(BACKWARD);
motorL.setSpeed(TURN_SHARP_SPEED);
motorR.setSpeed(TURN_SHARP_SPEED);
}
delay(DELAY_AFTER_TURN);
}

void turnLeft() {
// If first time Left Turn is taken
if (lastDirection != 'L') {
lastDirection = 'L';
// Stop the motor for some time
motorL.setSpeed(0);
motorR.setSpeed(0);
delay(BEFORE_TURN_DELAY);
// take slight Left turn
motorR.run(FORWARD);
motorL.run(BACKWARD);
motorL.setSpeed(TURN_SLIGHT_SPEED);
motorR.setSpeed(TURN_SLIGHT_SPEED);
} else {
// take sharp Left turn
motorR.run(FORWARD);
motorL.run(BACKWARD);
motorL.setSpeed(TURN_SHARP_SPEED);
motorR.setSpeed(TURN_SHARP_SPEED);
}
delay(DELAY_AFTER_TURN);
}

You might also like