Arduino PID advance line following Robot

pid robot

Modified PID Algorithm Implementation

Hey, Welcome to the second Tutorial of Line following Robot. As In the previous Tutorial we have just covered the basics and important things for making Line following Robot. As In the previous tutorial I have just explained the best ways to make a line follower instead of choosing wrong components and wasting your time in extra activities we have to make it to the best effective and simplest. So always choose those things that are less time consuming but Effective. So before moving towards the next must Read the previous Tutorial about Line follower. I’m not going to explain the components of the basic concepts for line following. First Read this Tutorial:


Well in this Tutorial we will discuss the PID concept for Line following ROBOT. And I named it the modified PID algorithm. Before this intention, I was planning to write a  tutorial about complete and complex line following Robots. But now I’m not going to do this. I have spent some hours surfing the internet about the PID line following Robots. And What I found is that there are already too many guides not just guides but a complex PID guides available for making Arduino code. They are not easy to modify according to the robot requirement.

Recently I have won the First prize in line following Robot competition. Later I will upload the video of the event in coming posts. My Robot Wins the Final Round and the amazing part is that I haven’t used any complex code. I just modify some points and skipped the complex part. Now my intentions for this tutorial is that I will only explain what I experienced and what is simple and effective. So we will just see the PID concept and adopt the best part in that for Line Follower.
So here are the contents:

00: Contents

  1. Necessary things for making line following Robot
  2. Introduction PID algorithm
  3. How to implement PID algorithm
  4. PID Algorithm Parameters
  5. Modify PID for line following Robot

02: Necessary things for making line following Robot

As I have already explained how to make a smart line following Robot so we will not discuss each and everything again. And We have discussed the components list, What sensor do we need to use for smart and fast line following robot and How motor and H-bridges work. Read these previous tutorials for Component selection and how the code and sensor work in the line following Robot case.




I have recommended IR modules sensor for making a line following Robots. The QTR Pololu sensor array will work perfectly good but again I will recommend the separate modules sensor for making line followers. By this, We have more choices according to line width when we use separate sensors. But if you feel more comfortable with QTR sensor array you can use it. The sensor array is expensive but the results will be good.

03: Introduction to PID algorithm

The Proportional Integral and Derivative (PID) controller is the control algorithm and mostly used in industrial control.  PID  algorithm allows engineers to operate them in a simple and straightforward way. The name shows PID, The three words  Proportional, Integral, and Derivative.

The Proportional Response:

The proportional component in PID algorithm is the difference between the set point of the system and the process variable. Now, this difference is called Error. The proportional gain (Kp) is the ratio between output response and the error signal.

The Integral Response

Integral in PID is the sum of the error term over the time. If the result is a small error the integral component will increase slowly. Now the integral term will continually increase over the time unless and until the error is going to be zero, so the ultimate effect is driving the Steady-State error towards zero. The difference between the process variable and the set point is the Steady state error.

The Derivative Response:

The function of the derivative component is to decrease the output of the process variable is going to increase. The derivative is the rate of change of the process variable. The Increase in the derivative time (Td)  will make the system to react strongly and to make changes in the error term. This will increase the overall speed. The very small (Td) the derivative time is used in most practical systems. Derivative Response is sensitive and highly sensitive to the noise and in the process variable. If the feedback is noisy or the control loop rate is very much slower, The control system will be unstable due to derivative response.

04: PID Control Algorithm for Line Following Robot

  1. In a case when robot is deviated to left it then pushes to move towards right
  2. In a case of deviation towards right, it is forced to move towards left

The accomplishment of this task gives a robot to follow the zigzag path. By this method, the speed we were giving was much less as error getting by using PID. And this technique was also not reliable for the turns. And To apply this algorithm it was better to use the error calculation method after calculating error giving motor it’s proportional value and then it changes speed and in turn, it gets back to the line.

e.g.-the deviation ranges between -3 to +3.

Kp is the proportionality constant it will give negative value when the robot is towards left of the line then the right motors gets slow down. Now, this is a good practical approach for many cases. But not suitable because of these reasons:
  • It affects the accuracy of the robot when the robot is not much effective it gets back to mean position very slowly.
  • To solve this problem we have to increase its proportionality contact but it will then overshoot when the robot comes to mean position due to inertia-cross to another side then again overshoot results in deviating result.

05: PID Algorithm Parameters

PID is a feedback mechanism. The three components that are:
  • : error
  • : time of deviation
  • : overshoots

The efficiency of the PID algorithm is totally based on these three parameters these helps to reach at a mean position much faster.

These three constants have been set by hit and trial method experimentally.
    1. Kp
    2. Ki
    3. Kd

Kp: Using only kp parameter the robot will be applied and give the results as the classical Line following algorithm.

Ki: Because of ki robot is forced to follow mean position.
Kd: kd does not allow the robot to perform any deviation.

06: Modify PID for Line Following Robot

To implement PID algorithm for Arduino code there are few important things. Well, I’m going to highlight some points.
The first thing after initializing pinout configurations:
    1. Define your sensors in an array:

const int irPins[5] = {A1, A2, A3, A4 ,A5};

2. Further, define your sensors as a binary function:

int irSensors = B00000;

3. Define

int errorLast = 0;
errorLast = error;

This is the point where an array stored the last error value and when ever the robot leaves the line it then comes back to the line by last stored value.

The last point is that use the error calculating equation to control motor speeds;

if (correction >= 0) {
motorRSpeed = maxSpeed – correction;
motorLSpeed = maxSpeed;

else if (correction < 0) {
motorRSpeed = maxSpeed;

motorLSpeed = maxSpeed + correction;

I know I just give some code snippets that will be really difficult to understand the whole concept.


Use this equation to control the speed of motors. Well here is just the PID understanding.
Read this complete post about PID that I have written for PIC controller. See the post the code will help you to understand more about the concept.


07: Conclusion

Well, this tutorial is going to be more about PID. My intentions were to explain each and everything but the tutorial is going to be little lengthy so I will explain my code in the Next post. There are very few steps left to explain for a perfectly smart and fast line following Robot.
Stay connected and Subscribe to our YouTube Channel for video tutorials.
Stay happy and stay motivated.
Exit mobile version