Fast & Furious Arduino Line Follower | PID Controller Basics

This is the complete guide about how to make a perfect PID Arduino based Line following Robot. We will explain pid controller basics step by step. This is the last post in the section of making a good and easy DIY Line follower. All the basic things about pid controller explained and we have already discussed.in this post we will discuss the final Arduino code for making a fast line following robot. This robotics projects for beginners with so many applications. But before anything Read these previous linked articles:

Also Read: Arduino smart and fast line following Robot

I called this Robot the smart and fast because the good and amazing thing in the arduino code is whenever the robot leaves the line where it forgets its path suddenly it will recall the last saved values that where it was. So the last sensor values tell whether it is on the left side or right. Now here we will not discuss each and every part for making line following robot. We will just recall few things and discuss the final code here.


Steps for making Line Following Robot

There are Five main steps for making Line following Robot. All the steps have been discussed in the posts mentioned above. We have discussed projects with arduino in detail.

  1. Objective
  2. Selection of Components
  3. Mechanical Parts assembling
  4. Electronic components Wiring
  5. Programming & Testing
  6. Tuning

Well the first four have been discussed in above mentioned two articles in detail about pid controller basics. Here we will discuss only the last part as we have pid controller explained. This is programming and testing part. We will also discuss about robotics projects for beginners.

1. Objective

Our objective is to follow the line either black or white. Defining the objective is very important before doing anything. Like define what kind of arena you are going to make a Robot for. Some arena’s have very complex structures and some have only or few checkpoints to pass and some have inclined surfaces. So it is necessary to define the objective for making a good Robot and what features it needs. Here we are going to make a simple line following robot that follows the line and never leaves the line ever using pid controller basics. It knows where it was and how to be back on the track again.

2. PID Arduino Code & Testing

This is the modified Arduino line following Robot code in the list of projects with arduino. We will discuss the each and every section of this code. As the Arduino programming is not difficult and easy enough to understand so you will understand each and every thing easily.

Define Motor Control Inputs:

Defining Sensors

The Very first line defines the sensors in an array. The binary values will be stored there.

The Threshold is a value defined for sensor that in the range of 700 the sensor will detect the line. The sensor we use is also works as an obstacle avoidance so this is the threshold that defines the analog values of the sensor. We will get the analog values from 0-700 when the sensor on the black or white line.

We define sensor in a binary for understanding the coming values. Reading analog values and working on the tuning of code is somehow difficult and not accurate. So for ease we define those analog in binary. We can now easily detect which sensor is on the line. in the form of 1 and 0.

3. Pid Controller Basics Tutorial:

Define Deviation:

Define Motor Outputs:

Code for Serial Monitor:

Reading Sensors:

Define Sensor Cases:

ErrorLast = error is the command that stores the last value of the sensor if sensor loses line.

error is the value that will be used for tuning of line following robot. This will increase or decrease the speed according to the case. We will discuss this in the tuning section of the post.

There are two basic functions left you can understand by yourself. Now we will move towards the final code:

4. PID Arduino Line following Robot Final code

Download Full Code

5. Robot Tuning- Robotics projects using Arduino

Here this is the part of code that you have to change according to your conditions. As you can see that the case one hare in binary represents that the 1 is for sensor on the line and 0 for nothing. So in first case there is only one sensor on the line the left most. The error is -150 and when this -150 goes into the following formula:

This formula calculates the accurate speed for the specified motor in this case the left motor. So by changing the values of error we can adjust the motors speed and we can smooth the way it follows the line. It is same as changing the values of kp kd and ki in PID. So this one is the easiest way to control.

Motor Speed By Error Values:

When we change the error values in the code we must know what speed our motor will give at the end. So here I calculated this speed when we change values of error function.

If  Error   :    Speed

120 = 180




Well these are the values that I have calculated as according to this code using pid controller basics rules. You can also calculate all other values by above given formula. If the Robot could not adjust on any of the values well change the correction values in conditions. I hope so the pid controller explained now.

The code is very easy and understandable. Adjust according to your connections and your Robot. Test and modify until you get the perfect results. Watch the video of my Robot in Action. It was a baby Arena but in lots of Robots mine got first position. Subscribe to our YouTube channel for more Tutorials and ideas. Stay connected and always have faith on yourself.

Exit mobile version