Over all the things you can do with a mobile robot, line following is one of the most common task. In this article, we will see how to do exactly that with an Arduino based mobile robot. We will see how to choose the chassis for your mobile robot, how to program it, and finally how to test the line follower robot. Let’s dive in!

Hardware & Software Requirements

There are many Arduino-compatible robot chassis out there that we could use for a line following project, but for this project I decided to simplify things and use the latest version of the DFRobot MiniQ 2 wheels robot chassis. This chassis already comes with an onboard Arduino system, so out of the box you can make the robot move around without any other components required.

This chassis also has many sensors on board, especially 5 infrared sensors on the bottom of the robot, which is perfect for our project as we won’t have any other components to add. We will use these sensors to know if we are following the line correctly, and adjust the speed of the motors if needed.

This is a closeup picture of these sensors:

sensors

The only other thing that you will need is a set of 4 AA batteries to power the robot. I chose a set of 4 AA rechargeable batteries for this project.

This is the list of all the components that were used in this article:

Hardware Configuration

The hardware configuration for this project is very simple, thanks to the sensors that are already installed on the robot chassis. All you need to do is to install the AA batteries on the robot. You will end up with a similar result:

robot

Building the Line Following Sketch

We are now going to build the sketch for this project. It starts by declaring on which pins the motors are connected:

#define EN1 5 // Pin for run the left motor 
#define IN1 12 // Pin for control left motor direction
#define EN2 6 // Pin for run the right motor 
#define IN2 7 // Pin for control right motor direction

Then, we define two constants for the forward & backward directions, so we can directly use these words in this sketch:

#define Forward 0
#define Back 1

We create a data array to store the measurements from the infrared sensors:

int data[5] = {};

After that, in the setup() function of the sketch, we set the motor pins as outputs, and stop the motors:

// Declare motor pins as outputs
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(12,OUTPUT); 
  
// Stop motors by default
Motor(Forward,0,Forward,0);

Then, in the loop() function of the sketch, we continuously read the values from the sensors, and then we act on the motors to adjust the position of the robot:

// Read data from the sensors
Read_Value();
  
// Follow the line 
line_follow();

Let’s first see the details of the read_value() function. It basically reads data from all the infrared sensors, and store these measurements into the data array:

char i;
for(i=0;i<5;i++)  {
    
  // Store data in data array
  data[i]=analogRead(i);
}

We can now see the details of the line_follow() function. It basically tests the different measurements of the sensors, and adjust the speed of the motors if needed. For example, this is the case where the robot is going too much on the left. Therefore, we should make the robot turn right:

// Detect black line on the left
if((data[0]<650 || data[1] <650) && (data[3]>800 && data[4]>800)) { 

  // Therefore turn right
  Motor(Forward,0,Forward,110);
  delay(10);
}

The same is done if we are going too much on the right. If the robot is going straight already, we continue going straight:

else if(data[2]<700)  {
    
  // Just go forward
  Motor(Forward,90,Forward,90);
  delay(20);
}

You can find the complete code inside the GitHub repository of the book:

https://github.com/openhardwaredrones/arduino-robot-line-follower

Testing the Line Follower Robot

It’s now time to test our line follower robot. First, make sure it is in safe place, like sitting on top of a little box so the wheels don’t touch the ground. Otherwise you can have some issues as the robot will start moving forward immediately.

Also prepare some line on the ground that the robot will have to follow. Note that depending on the surface you are using, you might need to adjust the values inside the code a bit. For example, I use a black tape put on a wooden surface, so I had to adjust the detection thresholds in the code to work with a non-white surface.

You can now upload the code to the robot via the Arduino IDE. Then, remove the USB cable, and put the robot on the line. You should see that the robot is following the line and adjusting its course if needed, as you can see on this picture:

robot_following

How to Go Further

There are many things you can do now to improve this project. You can for example refine the code to have a more complex behavior with the readings coming from the sensors. We used a very basic behavior here, but you could for example use a PID controlled to adjust more precisely the speed of the robot.

Have you ever built a line follower robot based on Arduino? Thinking about building one? Please share in the comments!