Introducing Line Following Robot with the PD controller. I’m sure most of you guys are familiar with line following robot. But for this tutorial, I will show you an interesting method that we can use with the robot, where we will apply a PD (Proportional and Differential) controller to it, making the robot faster and less wobbly. Now let’s go through this together to learn about how this robot was built. =)








PID controller is a short form for proportional-integral-derivative controller. This controller continuously calculates an error value using its own feedback loop that can consists of any type of sensors. For this case, we will be using IR sensors as our feedback to follows the line.

Note that I just said PD controller instead of PID controller. It is because the existence of Integral control will not affect the result much, so we decided to only use a PD controller.

Here is the calculation for our PD controller :

P = error
D = error – previous_error
previous_error = error

PD_value = (Kp*P) + (Kd*D)

Kp and Kd is the constant value for the Proportional and Derivative controller respectively.

Okay. So, how to calculate the error then? Let’s take a look at graph below.

This graph is a sinusoidal waveform but the signal is not continuous. Instead, it is discrete since it is digital, and we want to prove that by using the digital input, we still can apply the PD concept.

From graph above, we can see that each position will have its own value for error. We can decide what value of error we want but it must be in synchronized form. So, for this case, we will just use 1 to 5 as the errors’ values.





  1. You need to set up the robot first, that includes platform, motors, wheels, castor, Auto-Calibrating Line Sensor (LSS05) and PCB stands.
  2. Since I used another type of chassis, you may refer to this link for the guide to set up the Aluminium robot chassis.
  3. As for the battery and switch, you may refer to the circuit diagram for the connection.



4. Next, set up the CT-UNO to the platform. The Vin pin is connected to positive terminal of the battery while the GND pin is connected to the negative terminal. But, we will use Vin and GND pins on the SHIELD-XBEE since it will be located on the top of our robot.

5. Then, set up the SHIELD-2AMOTOR on the CT-UNO. Make sure all the jumpers are connected at the Signed Magnitude of the motor shield. For the connections of the motor to the shield, you may refer to the circuit diagram provided.

6. Also, connect the cable of the LSS05 to the motor shield.

7. You may start to set up the switch and potentiometer on a small breadboard. As for the connections, you also can refer to the circuit diagramprovided. Make sure to use A0 pin for the potentiometer since it is analog and the A1-A5 pins are already used for the LSS05.

8. For the last set up, connect the SHIELD-XBEE on the top of the robot. Make sure the jumper is connected to the D1 and D0 for the connection of TX and RX.

9. Use male-to-male jumper to connect the circuit on the breadboard with the SHIELD-XBEE. Use the circuit diagram as a guide to do the connection (p/s: CT-UNO and SHIELD-XBEE has same pinout)

10. Connect both USB cable of the CT-UNO and SKXBEE to the computer. Now, we are ready to upload our program. =)




You need to calibrate the LSS05 first after you upload your coding. Read the User Manual of the LSS05 to know how to calibrate this sensor. You may refer to the sample code for the full coding. You can start with Line Following Robot without PD if you want. =)

  1. From the Line Following without PD coding, you can adjust the speed of the robot. If it is too slow, you can increase the value up to what you prefer.

p/s : Also change all the speed for each position if you have decided to change the initial speed.

2. After you get the speed that you prefer, take the value as the initial_motor_speed for the next coding (with PD).

3. As for the maxSpeed, you can put whatever value that you want until you satisfied. It will indicate the maximum speed for your robot to move.

4. If your motors did not move straight in line, make sure to add the line as shown below. If both motors are already moving at the same speed (so the robot is moving straight), you can ignore this line or just comment out the line (“Hide” or comment lines by putting  ‘//’ sign before the line).

5. Next, switch on the robot and open the Serial Monitor to see the speed of each motor. Test the sensor to see if there is any change of the motors’ speed.

p/s: You need to change the PORT COM since we are using SKXBEE as our Serial Monitor now.

6. Now, we can test the robot with the PD value. Make sure to make Serial.print coding as the comment (put ‘/*’ before and ‘*/’ after the coding) as shown below. It is because we will use the Serial Monitor to read the Kp value.

7. To enable the Serial.print for the potentiometer, we need to make the Kp line is not commented out, so it is as shown below.

8. Open the Serial Monitor and make sure that the value is zero (0.0). If not, adjust the potentiometer until it is 0.

9. Put your robot on the field and observe the performance of the robot. Keep increasing the value of the potentiometer so that the robot can follow the line even if it is wobbly.

10. If you notice the best performance of the robot to follow the line, note down the value from the Serial Monitor.

11. Change the value of Kp with the value that you have note down before.

12. Lastly, we will find the suitable value for the Kd. Make sure the those Kp line as comment and those Kd line not as comment as shown below.

13. Open the Serial Monitor and make sure the value to be zero (0.0) first. If not, rotate the potentiometer until it is 0.

14. Put your robot on the field and observe the performance of the robot. Keep increasing the value of the potentiometer until you see the robot not so wobble.

15. If you notice the best performance of the robot to follow the line and not wobble too much, record the value on the Serial Monitor.

16. Change the value of Kd with the value that you have record before.

17. Make sure to make those Kd line as a comment like what we did before this since we do not want the value anymore.

18. Now, you can just play with the robot since you already get the constant value for Kp and Kd. Have fun. =)

p/s : For more understanding on how to set up the coding, you can refer to the video below. 





Sample Code

  1. Without PD controller
  2. With PD controller


6 thoughts on “How to Make your Line Following Robot Faster?”

  1. El Haddar Yasser

    Hey thanks for your help ..
    but i’m sorry cuz i didn’t undestand why u did choose those values of error ..
    i saw somewhere that this error should be calculated like this :

    sensors_average = 0;
    sensors_sum = 0;
    for (int i = 0; i < 5; i++)
    sensors[i] = analogRead(i);
    sensors_average += sensors[i] * i * 1000; //Calculating the weighted mean of the sensor
    sensors_sum += int(sensors[i]); //Calculating sum of sensor readings
    { position = int(sensors_average / sensors_sum);
    proportional = position – set_point; // Replace set_point by your set point
    integral = integral + proportional;
    derivative = proportional – last_proportional;
    last_proportional = proportional;

    they mean here by proportional the error that u're talking about ..
    i'm sorry if this doesn't make sense for you .. i'm new to this so i'll be thankfull for your reply !

  2. Hello . I want to know for the base speed, is there any limit for how fast I want to put, for example 0.75 of my full speed or I could go as high as 0.9 of my full or any certain limitation for the PID method to control my line follow ?

  3. Initially i was confuse as well with the values. It doesn’t work if i swap the value of 3 and 4. So my assumption is that either the pattern is correct or there was an error when initializing the sensor since the sensor itself has its own position.
    You can try to change the value and see what happens. Any findings you obtain, you may share here in the group. :’)

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

BLTouch Installation for Ender 3 with 32-bit V4.2.2 Board
Pick and Send Random Meal’s Option and Locations through Telegram Bot Using Grove WiFi 8266 on micro:bit
DIY Automated Vacuum Cleaner Using REKA:BIT With Micro:bit
Rainbow Spark in Mini House using Maker Uno.
TinyML on Arduino using Edge Impulse
Tutorials of Cytron Technologies Scroll to Top