A line follower robot built using LEGO Mindstorms NXT using 2 colour sensors and 1 light sensor

Line Follower Robot Algorithm & Optimizations for Better Line Following

Jay Gupta
5 min readMay 16, 2019

In this tutorial, I’ll be teaching you a Line Following algorithm for robots. I’ll be demonstrating the algorithm using pseudo-codes, thereby, it can be implemented in any language, be it Python, C or a LEGO Mindstorms Kit. I’ve posted techniques for better line following in the latter as well.

We’ll be using a robot with 2 colour sensors on each side. Alternatively, Light / Ultraviolet sensors can also work.

A robot with 2 colour/ light sensors on each side

The Algorithm

Case 1

Steer the robot right (Left Image), Steer the robot left (Right Image)

In this case, only the right sensor (Sensor 2) or the left sensor (Sensor 1) detects a line a.k.a they detect black colour.

If only the right sensor detects a black line, then the robot must steer right to follow the line. Alternatively, if only the left sensor detects a black line, then the robot must steer left to follow the line.

if right_sensor detects black 
// Move Right
set left motor ON
set right motor OFF
else if left_sensor detects black
// Move Left
set left motor OFF
set right motor ON
endif

Case 2

Keep the robot moving forward in the same direction

In this case, both the sensors do not detect a black line, so the robot should keep moving forward in the same direction and maintain its current journey path.

if right_sensor and left sensor do not detect black
set left motor ON
set right motor ON

Case 3

Stop the robot (Left Image), Move forward (Right Image)

This case is a bit tricky. Depending on your game scenario, both the sensors detecting a black line could mean either it is a finish line (left image) or it is just a loop which is part of a bigger line-following problem (right image).

If it signifies a finish line, then the robot must stop and terminate the program.

Otherwise, if it signifies a loop, then:
1. The robot should stop itself for a duration of 1–2 seconds to stabilise itself,
2. Move the robot a bit forward (approximately 1/4th of rotation) to move past the black line and then,
3. Continue the normal line following algorithm.

The significance of both the sensors detecting a black line has to be pre-determined before the start of the program.

// Black line is the finish line
if left_sensor and right_sensor detect black
set left motor OFF
set right motor OFF
// Black line is a part of a loop
if left_sensor and right_sensor detect black
set both motors OFF for 2 seconds (Stabalise)
set both motors ON for 1/4th rotation (Move past the black line)
continue to normal line-following algorithm

Complete Algorithm

if right_sensor detects black
// Move Right
set left motor ON
set right motor OFF
else if left_sensor detects black
// Move Left
set left motor OFF
set right motor ON
else if right_sensor and left sensor do not detect black
set left motor ON
set right motor ON
else if left_sensor and right_sensor detect black
set left motor OFF
set right motor OFF
endif

Optimizations for better Line Following

Adjust the distance between the sensors

Distance between the sensors substantially alters the motion of the robot

Through my observations, I conclude the following:

Generally, the robots start to tumble more as we try to increase the distance or the gap between the two sensors due to the harsh steering of the robots left and right as soon as one of the sensors encounter a black line.

Furthermore, if the sensors are placed too close to each other, then the movement of the robot becomes very stiff and rigid as the robot is continuously taking minute left and right turns and trying very bitterly to follow the line.

So, through thorough hit and trial, we should be able to find the right distance between the sensors to maintain the balance between the overall stability of the robot and the rigidity in movement.

Robot Steering Technique

Robot steering generally has two techniques — Either turn one motor OFF and turn the other motor ON to steer the robot left or right, or, run both the motors in opposite directions to take a turn. The latter usually results in more steeper turns.

Sharp turns can cause the robot to overshoot the line

As a general rule of thumb, if the expected line following path is smooth and does not contain a lot of steep turns, then it is better to turn only using one motor. But rough line paths demand more precise turning, so, both the motors should be used to turn the robot in such cases.

I hope you find the mentioned tips useful. Line Following is immensely useful, be it projects or competitions such as RoboCup and others. If I missed anything, let me know!

--

--