Line Following robot is one of the most popular project for hobbyist nowadays. However, instead of using Arduino, I’m using Raspberry Pi for this project since it is also one of the most highly demanded microcontrollers at this moment.

I hope you have already done LED blinkingRemote Desktop Connection and How to Control DC Motor before you jump into this tutorial as they are all related






1. Remote Desktop Connection

2. PuTTY

Refer to this tutorial on how to use it.



This tutorial is a continuation from the previous tutorial, Controlling DC Motor using MDDS10.

1. First, build a robot with Raspberry Pi and MDDS10 with the same connections as the previous tutorial.

2. In this tutorial, we will add the LSS05 Auto-Calibrating Line Sensor on the robot. This line sensor is typically used in embedded systems and robots in line following tasks.

3. It consist of 5 IR transmitter and IR receiver pairs. LSS05 can be used for either dark or bright line following. Any color with distinction or brightness difference is suitable for LSS05.

4. LSS05 will come with 2020 PCB Connector. So it will be difficult to connect with Raspberry Pi. To connect LSS05 to GPIO pin on Raspberry, you need to cut the end of connector and replace it with another connector.

5. In this tutorial, I use 2561 PCB Connector to replace the original connector. You can refer to User’s Manual to work this step.

6. Place LSS05 on the your robot. Make sure the center sensor is perpendicular to the castor.

7. The connection from LSS05 to Raspberry Pi is like below,

LSS05 Raspberry Pi
5V 5V (pin 4)
GND GND (pin 25)
O1 GPIO 11 (pin 23)
O2 GPIO 9 (pin 21)
O3 GPIO 10 (pin 19)
O4 GPIO 22 (pin 15)
O5 GPIO 27 (pin 13)
Cal GPIO 17 (pin 11)

8. After finish all the connections are done, calibrate LSS05 by referring to this link and the User’s Manual.

9. If there are no problems, proceed to programming the robot.




1. In the calibration process, I’ve set the LSS05 sensor to the Dark Line Following Mode. When the sensor detects a dark surface, the indicator LEDs will light up, and it will output high. When no dark line is detected, the output will be low.

2. Since we use 5 pairs of IR sensors, there will be 32 possible conditions (5 bits of permutations) for the incoming sensors. But in this tutorial, we will only consider the more possible conditions for the sensor.

3. These are the conditions that I used in this tutorial:

(S1:S2:S3:S4:S5) Display Speed M1 Speed M2
10000 left 1 20 0
11000 left 2 20 5
11100 left 3 20 7.5
01000 left 4 20 9
01100 left 5 20 14
00001 right 1 0 20
00011 right 2 5 20
00111 right 3 7.5 20
00010 right 4 9 20
00110 right 5 14 20
00100 straight 1 20 20
01110 straight 2 20 20

4. Next, let’s proceed to program. You can use either Remote Desktop Connection or PuTTY through SSH Client. You can refer to Remote Desktop Connection‘s tutorial to use it.

5. For this tutorial, I already build the code, and place it in Github. Using Raspberry Pi, you will get my code by following this command.

6. First, install the Git services on your Raspberry Pi. Open LX terminal of your Remote Desktop Connection, type:

sudo apt-get install git

7. Open this sample code. Copy the URL of the link.

8. On LX Terminal, write the URL of the link after this command.
git clone
python is case sensitive, so make sure to write exactly from the link.

9. To see the list of item that we already have in our Raspberry Pi, type “ls”

As you can see, we will have Raspberry-Pi-Line Following’s file. To open the file, type:

cd Line-Following-Robot-Raspberry-Pi-

Same as before, type “ls” to see what we have in file.

10. Now, we already enter the file. To open the code, type:

sudo nano

11. Now you can edit my code by changing the speed, GPIO’s pin or other necessary change based on your condition of game field.

12. Let’s try it. Place your robot on game field. Type this command on your LX Terminal,

sudo python

13. To stop the program, press CTRL+C.

14. Finish. Hope you enjoy play with your robot.

Ok, that’s all. If you have any question, feel free to ask in our technical forum as we seldom check the tutorial’s comment section. Thank you.



2 thoughts on “Raspberry Pi: Line Following Robot”

  1. sir how to pt code in python ? when i upload code in pi3 its working when i remove from system when i have to keep on robot side then its not working code so will u tell me the how to dump the code permenently into pi3 to work ?

  2. Hi can i get the circuit diagram and circuit description for the Raspberry Pi : Line Following Robot

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