Site Overlay

Line Following Robot using Arduino

Line Following Robot using Arduino Prototype

What is a Line Following Robot ?

A Line Following Robot is basically an autonomous Robot that follows black line with normal width on a white surface area and keeps following it in some manner. This surface area is commonly known as line following map. In this topic, we’ll learn basic and advanced line following maps and how to program a Line Following Robot using Arduino.

Line Following Maps

There are two kinds of Line Following maps used generally in many events.

  • Single continuous lined Map, which is a single line that curls or twists in different manner to make a regular patterned shape
  • Straight lined cross-sectioned map, which is a multiple straight lined path but with line overlapping perpendicularly to each other

Approach to follow both maps

Basic Line Following Technique :

At first, we’ll learn technique to follow line with any of the sensors you’ll select that will be discussed in hardware section. Basic 4 sensors in lined are used to detect a line and keep a robot to maintain its path over a line.

Line Following Algorithm

Here from our four sensors, the two middle sensors play an important role in line detection, whereas other two sensors will be used when the robot tries to lose its path going astray so end sensors can detect it and returns the robot to its specified path.

Talking about line detection, the placement of middle two sensors can vary in two positions, either both sensors are placed on the edge of black line or can be placed on the edge of white surface like in the above example.


Now to follow the strategy or algorithm to detect a line keeping in mind our above example there can be followed three steps:

  • Both sensors are on white, we make both motors as of differential drive to move forward at same speed to keep on line
  • Right sensor is on black and other one on white, it means our robot is tilting towards left side so we slow down our right motor to til our robot towards right
  • Left sensor is on white and other one on black, it means our robot is tilting towards right side so we slow down our left motor to tilt our robot towards left

In worst case scenarios while our both middle sensors are on white we also keep a check on other edge to edge sensors. If they are also one white, it means robot is going straight. If one of them is on black it means our robot is going astray so we apply same strategy as above but really slowing down our one of the motor to turn its positing hard towards line. This simple technique is used in a single line continuous maps.

Single Continuous Line Map
Perpendicular Line Map

For cross-sectioned perpendicular lined map we can also use another 2 sensors on edge and one sensor to the middle front to detect different junctions more precisely.

  • If all sensors on black, it means there is a cross junction
  • All sensors on black except front, it means T-junction
  • Front and Left three sensors on black, it means forward as well as left turn
  • Front and Right three sensors on black, it means forward as well as right turn
  • Only Left three sensors on black means left turn
  • Only Right three sensors on black means right turn

Robot Movement techniques

As this robot works on differential drive term, we have to play with both motors differently to control basic movements of a robot. Basic control movements are as follows:

  • moving Straight, rotate motors forward at same speed
  • tilting Left, nominal speed of left motor is decreased and of right motor is increased
  • tilting Right, nominal speed of right motor is decreased and of left motor is increased
  • turning Left, rotate left motor in backward until front sensor reaches on line
  • turning Right, rotate right motor in back until front sensor reach on line
  • making a U-turn, rotate any one of the motor in backward until front sensor re-reaches on line
Perpendicular Line Following Map

Algorithm for Perpendicular Line Map

There are two strategies which you can select in order to reach to an end in a shortest way.

  • Left Hand Rule
  • Right Hand Rule

Either you select one of the rule to reach to an end. For instance, if you are selecting Left Hand Rule, for every left turn you should always make a left turn otherwise straight forward and vice versa. Applying this rule, your robot easily reach to an end.

Right Hand Rule Line following Robot
Right Hand Rule for Line following Robot


Parts you need for Line Following Robot are as follows:

  • Two DC Motors with tires – for differential Drive
  • Omni wheel – to support the Base
  • Base for Robot – which is normally plastic which can be bought online here
  • Arduino UNO
  • LEDs/IR and Photo Transistors
  • L293D – Differential motor Drive IC or H-Bridge IC
  • 10 K-ohm and 270 ohm resistors
  • Rechargeable Battery (Li-po Battery best for the purpose)
Sensors Circuit
Basic Sensor Circuit

Sensor Circuit is Basic and easy to fabricate. We can use IR/LED with 270 ohm resistor and photo-transistor with 10 K-ohm resistor. Taking output of sensor in between photo-transistor and resistor that goes to our analog pin of Arduino UNO. We opt transmitter and receiver close to each other facing towards ground so light from LED or IR reflect from ground back to photo-transistor. In our case, we are using 6 sensors so you can opt similar circuit technique placing sensors aligned as discussed above.

Motor Circuit

L293D H-Bridge IC

Here to control our differential motors, we’ll be using L293D IC to control the rotation and direction of both motors with Arduino UNO. One of the motor is connected at OUT1 and OUT2 and other one at OUT3 and OUT4.IN1 and IN2 goes to Arduino where IC get signals for motor 1, similar goes for IN3 and IN4.EN1 and EN2 are set to HIGH. +Vmotor is given voltage according to motor voltage which is normally +6V. +V is given 5V and 0V to GND.


Here comes the fun part of the project, the programming or coding of Arduino to control Line Following Robot using Arduino.

At first, we’ll test the analog value range of our sensors on Black and White surface. Following simple code and checking values in Serial Monitor and note down the readings.

pinMode(A0, INPUT);
int x = analogRead(A0); //Reads the analog value on pin A0 into x
Serial.print(“Analog value: “);

Here for our sensor connected to A0 of Arduino, we check its reading range in Serial Monitor. Similar goes for every 6 sensors connected from A0 to A5.

Now, for the main code at first you’ll set void setup for your inputs and outputs like,

pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
pinMode(5, OUTPUT); //PWM pin for motor IN1
pinMode(6, OUTPUT); //PWM pin for motor IN2
pinMode(9, OUTPUT); //PWM pin for motor IN3
pinMode(10, OUTPUT); //PWM pin for motor IN4

For Void Loop write your code according to the pattern discussed above to follow line, by keeping checks on the ranges of each sensors. Firstly, saving values from every sensor into an integer.

int s1 = analogRead(A0); //Sensor-1
int s2 = analogRead(A1); //Sensor-2
int s3 = analogRead(A2); //Sensor-3
int s4 = analogRead(A3); //Sensor-4
int s5 = analogRead(A4); //Sensor-5
int s6 = analogRead(A5); //Sensor-6

Then using for-loop for every possibility of sensor value discussed above, you control your motor speed by PWM in a delay for like delay(100) for every motor through IN1/IN2 or IN3/IN4. If both HIGH motor will go Forward, if opposite motor direction changes.

Good Luck and have fun building the Line Following Robot using Arduino.

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2019 DIY Projects Arduino Crafts. All Rights Reserved. | Foodoholic by Catch Themes