Building a Line Follower Robot Using Arduino UNO

by Rachana Jain in Circuits > Arduino

871 Views, 4 Favorites, 0 Comments

Building a Line Follower Robot Using Arduino UNO

Line follower robot.jpg

A line follower robot or LFR is an autonomous guided robot designed to follow a path or line, typically a black line on white surface. These robots are used in industrial automation, warehouse logistics, and educational purposes. In this guide, we’ll show you how to build a line follower robot using Arduino and an L293D motor driver shield.

Supplies

Working of a Line Follower Robot

Untitled design (5).jpg

A line follower robot operates by detecting and following a black line on a surface. But how exactly does it recognize the line? The principle is based on the differing light properties of black and white surfaces. Black surfaces absorb most of the light, while white surfaces reflect nearly all of it. This difference is leveraged by sensors like LDRs (Light Dependent Resistors), IR sensors, or specialized line sensors. For our project, we use line sensors due to their superior accuracy and precision.

Understanding Line Sensors

Line sensors are composed of two key components: an IR emitter (LED) and an IR receiver (photodiode). The emitter continuously sends out infrared light, while the receiver detects the light reflected from the surface below.

  1. When the infrared light hits a white surface, it reflects back toward the receiver, which detects the light signal.
  2. When the light hits a black line, it is absorbed, and no light reaches the receiver.

This simple mechanism allows the robot to differentiate between the black line and the surrounding surface, enabling it to navigate along the path with precision.

This detection process forms the core functionality of the line follower robot and ensures reliable performance in line-tracking applications.

Circuit Diagram of the Line Follower Robot

FX8GL4PM3EGQ57I.jpg

The circuit consists of four main components: two IR sensors, an L293D motor driver shield, four 12V BO motors, and an Arduino board. A 12V power supply will be connected to the Arduino to power the system. The motor driver shield should be mounted on top of the Arduino board for proper connections.

The VCC and Ground pins of both IR sensors are connected to the respective VCC and Ground pins of the Arduino.

The analog output pin of the left line sensor is connected to the analog input pin A0 of the Arduino, while the analog output pin of the right Line sensor is connected to the analog input pin A1.

For motor connections, the left-side motors are wired in parallel and connected to port M3 of the motor driver shield. Similarly, the right-side motors are wired in parallel and connected to port M4 of the motor driver shield.

Assembling the Arduino-based Line Follower Robot

Line Follower Robot using Arduino And L293D Shield


  1. Set up the Chassis: Mount the wheels, attach the motors to the chassis, and secure the battery holder in place.
  2. Connect the Line Sensors: Position the IR sensors at the front of the chassis for optimal line-detection accuracy.
  3. Mount the Arduino and Shield: Place the Arduino Uno and the L293D Motor Driver Shield on the chassis, securing them firmly.
  4. Connect the Wiring: Connect the Line sensors to the Arduino’s analog pins, and connect the motors to the motor driver shield’s outputs. Finally, wire the power supply to the Arduino and motor shield.
  5. Now the robot is ready. Make a path using 50 mm black tape. The width of black tape should be between 4.8 to 5 cm

Code

Upload the following code into the Arduino:

/*
Library used: Adafruit Motor Shield library V1 version: 1.0.1
For this code to run as expected:
1. The centre to centre distance between the Line sensors should be 11 to 11.5 cm
2. The width of black tape should be 4.8 to 5 cm
3. The distance of the sensor LED from the flat ground surface should be 2 cm.
*/
#include <AFMotor.h>
// MACROS for Debug print, while calibrating set its value to 1 else keep it 0
#define DEBUG_PRINT 0
// 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() {
#if DEBUG_PRINT
Serial.begin(9600);
#endif
// Set the current speed of Left Motor to 0
motorL.setSpeed(0);
// turn on 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);
#if DEBUG_PRINT
// This is for debugging. To check the analog inputs the DETECT_LIMIT MACRO value 300 is set by analysing the debug prints
Serial.print(left_value);
Serial.print(",");
Serial.print(right_value);
Serial.print(",");
Serial.print(lastDirection);
Serial.write(10);
#endif
// 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)) {
moveBackward();
}
}
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 moveBackward() {
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);
}

Final Testing and Troubleshooting

Once assembled, place the robot on the line and turn on the power. Observe its movements and adjust sensor positions or motor speeds if necessary. Ensure the line path is clear and well-defined for best results.

To learn more refer to the tutorial: Arduino Line Follower Robot.