Robotics Competition | Line Following Robot

Hey guys! Today we are going to share with you our experience in Robotics Competition held at BIT Patna. The competition was divided in two stages. Both the stages had two different tasks.

Today we are discussing about the first stage competition and we secured 1st position in it.

Line Following Robot

A line follower robot is a robot which follows a certain path controlled by a feed back mechanism from the line sensors and movement of the wheel. You can build a line following robot car using 2 sensors, 4 sensors or even upto 8 infrared sensors based on your need.

First Stage Task

The task on first stage was to run a robotic car on white line with black surface and next black line with white surface. Basically, the robot will follow a particular line hence, called an autonomous robot.

For example, let us assume in the below is a line following robot. It has two sensors on the front of the robot and moving on black line with white surface. If the left sensor moves away from the line, the left wheel moves towards right and if the right sensor moves away from the line, the right wheel moves towards left.

Thus, it makes the robot follow its assigned line. It is completely autonomous and pre-programmed and works without any external support.

If you want to know about autonomous and controlled robots you can read our previous blog.

One more thing all the participants were provided guidelines regarding the arena and how the competition will take place beforehand so that they can make their own robot for practice session. Now at first we will show you some clips from our practice session and next at the competition.

Please support us on YouTube also. Like Share and Subscribe to our channel : https://youtube.com/c/RatnasRoboLab


Programming Part

Void Setup()

We used 6 Line Sensors as Input Pin, 2 Motors as Output Pin and a Buzzer as Output pin also.

void setup() {
   Serial.begin(9600);
  pinMode(2, OUTPUT); //BUZZER

  pinMode(A0, INPUT);//EXTRM LEFT
  pinMode(A1, INPUT);//LEFT
  pinMode(A2, INPUT);//RIGHT
  pinMode(A3, INPUT);//EXTRM RIGHT

  pinMode(5, OUTPUT);  //left wheel positive
  pinMode(6, OUTPUT);  //left wheel negative
  pinMode(10, OUTPUT); //right wheel positive
  pinMode(11, OUTPUT); //right wheel negative

  pinMode(8, INPUT); // LEFT SENSOR
  pinMode(9, INPUT);  //RIGHT SENSOR
}
Functions ()
void straight()
{
  analogWrite(5,250);
    analogWrite(6,0);
  analogWrite(10,250);
  analogWrite(11,0);
}

void rightadjust()
{
  analogWrite(5,250);
    analogWrite(6,0);

  analogWrite(10,150);
  analogWrite(11,0);
}

void leftadjust()
{
  analogWrite(5,150);
    analogWrite(6,0);

  analogWrite(10,250);
  analogWrite(11,0);
}

We used some functions according to our need. Here in the below 3 functions are given, straight function is used for straight line following, rightadjust and leftadjust functions are used for wheel adjustment.

Here 4 functions are given below, we used these functions for right turn, left turn, slow movement and for stopping the bot.

void rightturn()
{
  analogWrite(5,180);
  analogWrite(6,0);
  analogWrite(10,0);
  analogWrite(11,10);
}
void leftturn()
{
  analogWrite(5,0);
  analogWrite(6,10);

  analogWrite(10,180);
  analogWrite(11,0);
}
void stop()
{
  analogWrite(5,0);
  analogWrite(6,0);

  analogWrite(10,0);
  analogWrite(11,0);
}
void slow()
{
  analogWrite(5,70);
  analogWrite(6,0);

  analogWrite(10,70);
  analogWrite(11,0);
}

First Robot runs on White Line on the Black Surface, for that we used this linefollowing2 function, given below.

// 1st Robot runs on WHITE LINE ON THE BLACK SURFACE
// Here,we named the function linefollowing2()

void linefollowing2()
{
  if((digitalRead(A1)==LOW) && (digitalRead(A2)==LOW))//straight
  {
    straight();
  }

  if((digitalRead(A1)==HIGH) && (digitalRead(A2)==LOW) )//adjust straight 4 right
  {
    rightadjust();
  }

  if((digitalRead(A1)==LOW) && (digitalRead(A2)==HIGH))//adjust straight 4 left
  {
    leftadjust();
  }

  if((digitalRead(9)==LOW))
  {
  rtturn2();
  }

   if((digitalRead(8)==LOW))
  {
    ltturn2();
  }
}

Second Robot runs on Black Line on the White Surface, for that we used this linefollowing1 function, given below.

// 2nd Robot runs on BLACK LINE ON THE WHITE SURFACE
// Here,we named the function linefollowing1()

void linefollowing1()
{
  if((digitalRead(A1)==HIGH) && (digitalRead(A2)==HIGH))//straight
  {
    straight();
  }

  if((digitalRead(A1)==LOW) && (digitalRead(A2)==HIGH) )//adjust straight 4 right
  {
    rightadjust();
  }

  if((digitalRead(A1)==HIGH) && (digitalRead(A2)==LOW))//adjust straight 4 left
  {
    leftadjust();
  }
    if((digitalRead(9)==HIGH))
  {
  rtturn1();
  }

   if((digitalRead(8)==HIGH))
  {
    ltturn1();
  }

  if( (digitalRead(8)==HIGH) && (digitalRead(9)==HIGH) && (digitalRead(A0)==HIGH) && (digitalRead(A3)==HIGH))
      {
      stop();
      delay(500);
      }
}

Whole Program

//Created by Ratnadeep Hore,Rahul Mourya and Ravi Kant
//Support Us YouTube.com/RatnasRoboLab

void setup() {
   Serial.begin(9600);
  pinMode(2, OUTPUT); //BUZZER

  pinMode(A0, INPUT);//EXTRM LEFT
  pinMode(A1, INPUT);//LEFT
  pinMode(A2, INPUT);//RIGHT
  pinMode(A3, INPUT);//EXTRM RIGHT

  pinMode(5, OUTPUT);  //left wheel positive
  pinMode(6, OUTPUT);  //left wheel negative
  pinMode(10, OUTPUT); //right wheel positive
  pinMode(11, OUTPUT); //right wheel negative


  pinMode(8, INPUT); // LEFT SENSOR
  pinMode(9, INPUT);  //RIGHT SENSOR

}
void straight()
{
  analogWrite(5,250);
    analogWrite(6,0);
  analogWrite(10,250);
  analogWrite(11,0);
}


void rightadjust()
{
  analogWrite(5,250);
    analogWrite(6,0);

  analogWrite(10,150);
  analogWrite(11,0);
}

void leftadjust()
{
  analogWrite(5,150);
    analogWrite(6,0);

  analogWrite(10,250);
  analogWrite(11,0);
}

void rightturn()
{
  analogWrite(5,180);
    analogWrite(6,0);
  analogWrite(10,0);
  analogWrite(11,10);
}

void leftturn()
{
  analogWrite(5,0);
    analogWrite(6,10);

  analogWrite(10,180);
  analogWrite(11,0);
}
void stop()
{
  analogWrite(5,0);
    analogWrite(6,0);

  analogWrite(10,0);
  analogWrite(11,0);
}
void slow()
{
  analogWrite(5,70);
    analogWrite(6,0);

  analogWrite(10,70);
  analogWrite(11,0);
}

// 1st Robot runs on WHITE LINE ON THE BLACK SURFACE
// Here,we named the function linefollowing2()

void rtturn2()
{
  rightturn();

   while(!((digitalRead(A3)==HIGH) && (digitalRead(9)==HIGH)));
}


void ltturn2()
{
  leftturn();

 while(!((digitalRead(A0)==HIGH) && (digitalRead(8)==HIGH)));
}


void linefollowing2()
{
  if((digitalRead(A1)==LOW) && (digitalRead(A2)==LOW))//straight
  {
    straight();
  }

  if((digitalRead(A1)==HIGH) && (digitalRead(A2)==LOW) )//adjust straight 4 right
  {
    rightadjust();
  }

  if((digitalRead(A1)==LOW) && (digitalRead(A2)==HIGH))//adjust straight 4 left
  {
    leftadjust();
  }

  if((digitalRead(9)==LOW))
  {
  rtturn2();
  }

   if((digitalRead(8)==LOW))
  {
    ltturn2();
  }
}


// 2nd Robot runs on BLACK LINE ON THE WHITE SURFACE
// Here,we named the function linefollowing1()

void rtturn1()
{
  rightturn();
   while(!((digitalRead(A3)==LOW) && (digitalRead(9)==LOW)))
   {
    if((digitalRead(8)==HIGH) && (digitalRead(9)==HIGH))
    break;
   }
}


void ltturn1()
{
  leftturn();
 while(!((digitalRead(A0)==LOW) && (digitalRead(8)==LOW)))
 {
     if((digitalRead(8)==HIGH) && (digitalRead(9)==HIGH))
    break;
 }
}

void linefollowing1()
{
  if((digitalRead(A1)==HIGH) && (digitalRead(A2)==HIGH))//straight
  {
    straight();
  }

  if((digitalRead(A1)==LOW) && (digitalRead(A2)==HIGH) )//adjust straight 4 right
  {
    rightadjust();
  }

  if((digitalRead(A1)==HIGH) && (digitalRead(A2)==LOW))//adjust straight 4 left
  {
    leftadjust();
  }
    if((digitalRead(9)==HIGH))
  {
  rtturn1();
  }

   if((digitalRead(8)==HIGH))
  {
    ltturn1();
  }

  if( (digitalRead(8)==HIGH) && (digitalRead(9)==HIGH) && (digitalRead(A0)==HIGH) && (digitalRead(A3)==HIGH))
      {
      stop();
      delay(500);
      }
}

  void linefollow()
{
     while( (digitalRead(A3)==LOW) && (digitalRead(A0)==LOW) && ((digitalRead(A1)==HIGH) || (digitalRead(A2)==HIGH) ) )
      {
        linefollowing1();
      }
    if((digitalRead(A0)==HIGH) && (digitalRead(A3)==HIGH))
    {
      linefollowing2();
    }
}

void loop()
{
  linefollow();
}

Do you want to support our videos ?
https://www.buymeacoffee.com/ratnasrobolab

Support Our Channel By Shopping parts from Amazon !

Product Links

1. UNO R3 board, USB Cable : https://amzn.to/3V70iGC
2. L293d motor driver shield : https://amzn.to/41YTtZJ
3. Screws and nuts set : https://amzn.to/422jDL5
4. Plyboard chasis with motors and wheels : https://amzn.to/3Hg1phf
5. Only motor with wheels : https://amzn.to/44853Ug
6. Infrared Sensor x 2 : https://amzn.to/3Nigtz2
7. 4 cell AA Battery holder : https://amzn.to/3n1vdrk
8. 6 cell AA Battery holder : https://amzn.to/3NfS1hp
9. Duracell Ultra Alkaline AA Battery, 8 Pieces : https://amzn.to/422kU4P
10. Jumper wires : https://amzn.to/3oFrq3f
11. Black Tape : https://amzn.to/3oESH5L
12. L-Clamp x4 : https://amzn.to/3HgqhFS

Further Readings

If you liked this article, then please subscribe to our YouTube Channel. You can also find us on InstagramFacebook and Twitter.

READ – CONNECT – BOOST – CREATE