Arduino smart and fast line following Robot

Arduino Line following Robot

Arduino line following robot with complete code files

Line Following Robots are the most common icon for building any robot.Line followers are the stepping stone towards robotics.The most basic,easy and very first part in robotics starts with line following.the most common Robot all the time.
Well Making line follower for the first time is not really easy as we thought.some of the things could be turned to make simple stuff into complex.i mean some times sensor calibration, comparison and other related things could be more difficult.
For this we have to be more smart and more tricky.we have to choose those parts that can not create any sort of disturbance for us.I'm talking about those days when there were not much revolution in Robotics.we had to make robots using circuits with LDR'S IR'S and other messy and complex things.
Now we have been blessed with Arduino and bunch of good not just good but great sensors.
so be smart and don't waste your time in making complex circuits with separate sensors.
There are bunch of sensor modules available in the market with built in comparators.
so wisely choose what you are going to do and what you have to do.
We will see some of the very common and effective ways of making line following robot.We will start from basics and in further tutorials we will follow some complex algorithms.we will see PID implemented robots and some advance techniques in making smart robots.

Step # 1 : Gather all the Stuff at one place

Required Components:

  1. Arduino board/uno/nano/mega
  2. 3x IR sensor modules
  3. 2x Dc/servo motors
  4. Chassis 
  5. Wheels
  6. Battery 
  7. Caster wheel
  8. Jumper wires
complete guide of arduino line following robot with code files

These are the basic components you have to buy before making any further i will explain how to choose the right component and which one will be more suitable.

1-Arduino board:

As arduino is the main part so we have to be clear about which arduino board we are going to use.for making line followers the arduino uno is the best choice.we don't need a lot of pins or very few pins that's why we are not going for the nano and mega.
For arduino guidance and Example codes understanding go through these following posts.


2-IR Sensor Modules:

IR sensor module is the best choice for making line following robot.instead of using IR leds we have to buy IR modules.
The pin out Configuration for IR modules and arduino is very simple.

If you are using 3 sensors connect 3 in the same manner.The IR module has 1-pin for input and other 2 are vcc and ground.
So join all VCC and GRound seperatly at one place and then connect to the arduino.and the input pins are placed randomly on the digital pins of arduino.

3-DC/Servo motors:

The selection of motors depends upon your choice.if you are going to  but the LFR Kit then you got the servo motors with in the package.
but my recomendation is to use DC geared motors.As i have used in this project we can handle more wieght and current as compared to simple there are other several benefits of using these motors.
DC gear motors having high rpm and high
torque they are PMDC typed. The advantages of these motors are constant magnetic field which
is uncontrolled by external source.

Line follower KIT:

There are lots of LFR kits  are available in the market.buying a kit is the easiest solution for any mechanical work load or any sort of disturbance.


Chassis could be of any thing.I'm not going in the depth there are so many things already discussed over the internet.
The chassis could be of an acrylic sheet or use any home made things for robot base.


There are standard size wheels are available in the market.if you are using KIT then youo don't need to worry about these things.

Ball caster:

For making 2 wheel LFR you have to buy an extra wheel that is caster wheel the ball caster.For balancing the robot.


Battery is the most important part in this project.we need a good and an efficient timing battery.dry lithium ion 12 volt rechargeable battery is the best choice.we can also go for other choices use simple rechargeable  two 4-volts batteries in series.

Pin out Configuration and Assembling:

Assemble all the evrything on your kit.and now wire up.

Connect 3 IR modules with your arduino board.the vin pin of IR module will be connected on any of analog or digital pin of arduino remember these pin configurations you have to use in the if you are connecting on analog pins then you have to write analog in the code.

Motor Driver:

The motor drivr we are using here is L298N module. This is my recommendation to use this module instead of going for any shield or downloading libraries and other stuff this one is the simplest and easy.
For the working principle pin out configuration and why we are using this module you have to read this following post.

Code Construction:

There are three main steps in the construction of coding.
1-defining the pins according to the connecting / inputs and outputs
2-Logic for sensors / The switch case
3-functions for motor directions

The very first part Before coding is to calibrate the sensor.Set a threshold for a sensor.

For working principle of line follower you must have some knowledge about how we move motors according to the logic.

 Logic and Sensors placement:

We have to place all the sensor with equal distance lets say 1.5 in middle sensor must be at the center of the line;

logic of line following robot
guide of line following robot
complete guide of line following robot

This is the basic principle for line following robot.Now we will move towards arduino code.

Arduino Code:

For three sensors the logic will be :
010  Robot is on the line drive forward
100  turn medium Left
001  turn  medium Right
110  Sharp turn left
011 Sharp turn Right
000 stop

Complete Code:

#define LS 2      // left sensor
#define MS 3      // middle sensor
#define RS 4      // right sensor

#define LM1 5       // left motor
#define LM2 7       // left motor
#define LM3 6       // left motor Enable pin for controlling speed

#define RM1 8       // right motor
#define RM2 9       // right motor
#define RM3 10       // right motor Enable pin for controlling speed

void setup()
  pinMode(LS, INPUT);
  pinMode(MS, INPUT);
  pinMode(RS, INPUT);
  pinMode(LM1, OUTPUT);
  pinMode(LM2, OUTPUT);
  pinMode(LM3, OUTPUT);
  pinMode(RM1, OUTPUT);
  pinMode(RM2, OUTPUT);
  pinMode(RM3, OUTPUT);

void loop()
  if(!(digitalRead(LS)) && digitalRead(MS) && !(digitalRead(RS)))    // Move Forward (010)
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, LOW);
  if(!(digitalRead(LS)) && !(digitalRead(MS)) && digitalRead(RS))     // Turn medium right (001)
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, LOW);
  if(digitalRead(LS) && !(digitalRead(MS)) && !(digitalRead(RS)))     // turn medium left (100)
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, LOW);
  if(!(digitalRead(LS)) && !(digitalRead(MS)) && !(digitalRead(RS)))     // stop (000)
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, LOW);

  if(digitalRead(LS) && (digitalRead(MS) && !(digitalRead(RS))))     // Turn Sharp left 90 degree (110)
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, LOW);
  if(digitalRead(RS) && (digitalRead(MS) && !(digitalRead(LS))) )    // Turn Sharp Right 90 degree (011)
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, LOW);

Upload the code and test it.Before testing check your connection and compare with your code.
I have skipped most of the mechanical parts in this have to take them on your own.these were some common and unnecessary steps.
I hope you got the concept.If you have nay query comment below to notify.
Read the coming post about advance line following Robot and PID implemented line following Robot.
Follow to get the latest updates....

Next Post »


Click here for comments