Bukit Mertajam, Penang

Line-Following Robot Using LSA08 in Serial Mode with PID Controller


Good day,

In this tutorial, we are going to built an advanced line following robot. Why advanced? Because normal line followers only respond with specified/predetermined speed, however with a little trick, we can make the robot changes its speed depending on the line position. The trick here is called the PID controller – I hope you already understand the meaning of PID, if not, please google it ^_^

Before we start, you need to understand the concept of Advanced Auto-Calibrating Line Sensor (LSA08) operating in serial mode here, and also how to read the analog reading of LSA08 here.


Here is a list of materials that we will need.



Arduino IDE – an open source Integrated Development Environment used to program Arduino board. You can download it here.


Robot base:

  • From here, make yourself a robot base with at least 20cm x 20cm. For example…

The next thing is to mount Arduino board, MDD10A, and motors on the robot base, and connect them as shown in image at the left.

Notice that I did not connect 5V pin on MDD10A because there is no such pin on my real board, you have to connect it to 5V pin on your Arduino board if such pin exist.

A switch is highly recommended to avoid your robot running away once you connect the battery =P.

In the end, your connection should looks like the one at left.

(Click the image for larger view)

The connection of LSA08 and Arduino is based on the serial mode, so in case anyone missed it, I will explain it again.

We have the pin layout for PORTA which is used for serial and analog mode.

So connect it as below:

LSA08 pin – Arduino Uno board

1 – RX (pin 0)

2 – TX (pin 1)

3 – digital pin 2

4 – not used

5 – digital pin 4

6 – not used

7 – not used

8 – not used

9 – Vin

10 – GND

*NOTE: make sure this is PORTA, and input voltage ranging from 7.5V – 20V

Here are the settings that you need to configure your LSA08:





The other configurations will depend on your surrounding, so I can not give you the exact value. In case, you wondering the meaning of the setting and how to use it, please go through the user manual of LSA08 at Cytron’s product page.

PID controller

Now, let’s talk about PID controller where P stands for Proportional, I stands for Integral, and D stands for Derivative. Well, it is nothing but a piece of code that allows us to find the deviation of robot to the line, and correcting the position by changing the motors speed.

The concept is simple, we decide a set point on the sensor array (mostly the middle) and the robot will always try to adjust itself to centered at the set point. LSA08 support range from 0 – 70, so 35 will be our set point value, and the target of our PID controller is to make sure we achieve the position 35 in shortest time.

So, let’s see what we need to do.

First of all, we need to find the error of deviation using the formula

error = current position – set point value

this will yields us negative value when our robot is deviating to the right, or positive value when our robot is deviating to the left.

Next, applying the PID formula to find the required change in speed,

speed change = Kp * error + Kd * (error – previous error)

where Kp and Kd are the constants that we need to determine through endless of experiments, while the previous error is the error before this iteration.

Notice that I said PID controller, but in fact we are just applying PD controller, because adding Integral control will not affect the result much.

And here, the last step is to adjust your motors speed by how much it needs to change.

right motor speed = base speed – speed change
left motor speed = base speed + speed change

where, you can decide the base speed of your motors so that you can have your motors move faster or slower. You might need to interchange the + and – sign if your robot move in opposite direction.

You can download the source code at attachment below.

Finally, upload the sketch to Arduino board, and run the program. If your robot moving in a wrong way, try changing the polarities of your motor.

There are several ways on how to get the Kp and Kd value, and I found this is the most understandable:

First increasing the Kp value while maintain Kd as 0, until a condition where your robot move fast enough and without causing it to slip.

Then, divide the Kp value by half, and set the Kd value to the same as Kp, and from here, increasing the Kd value while maintain Kp value, until a condition where your robot move fast enough and without causing it to slip.

*NOTE: Kd > Kp always. And please don’t ask for the values, as there is no exact values for this, values from the others might not suit in your case.

**NOTE: You can analog read potentiometers instead of software tuning the Kp and Kd values, it saves you A LOT of time T^T.

Finally, if you have any question, feel free to ask in our technical forum as we seldom check the tutorial’s comment section.



Related Posts

Comments (2)

Is 11.1 v battery sufficient to power all components

Is this applicable for macanum wheels robot ??

Leave a comment