Raspberry Pi: Line Following Robot


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 demanding microcontroller at this moment.

I hope you already done LED blinkingRemote Desktop Connection and How to Control DC Motor tutorial before you jump to this tutorial. It is relating to each other in this tutorial.

Hardware Required

Software Required

1. Remote Desktop Connection

2. PuTTY

Refer to this tutorial on how to use it.

Step by Step (Hardware)

This tutorial is a continuation from the previous tutorial, Controlling DC Motor using MDDS10. I hope you already try it and let’s continue to do more exciting project!

1. Firstly, build a robot with Raspberry Pi and MDDS10 and the connection is same as previous tutorial.

2. In this tutorial, we will add Auto-Calibrating Line Sensor or known as LSS05 on the robot. This line sensor, is typically used for embedded system or robots in line following task.

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 distinct 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 do 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,

LSS05Raspberry Pi
5V5V (pin 4)
GNDGND (pin 25)
O1GPIO 11 (pin 23)
O2GPIO 9 (pin 21)
O3GPIO 10 (pin 19)
O4GPIO 22 (pin 15)
O5GPIO 27 (pin 13)
CalGPIO 17 (pin 11)

8. After finish all the connection, do some calibration process to LSS05 by refer to this link and User’s Manual.

9. If you don’t have any problem with it, we will proceed to programming part.

Step by Step (Programming)

1. In calibration process, I set the line sensor into dark line following mode which LSS05 will detect dark line. Sensor indicator LEDs will light up if it detects dark surface and the output is high. When no dark line is detect, the output will be low.

2. When we use 5 pair of IR sensor, there will be 32 condition or arrangement of sensor that we will have. We will take only a possible condition for the sensor.

3. This is the condition that I use in this tutorial:

(S1:S2:S3:S4:S5)DisplaySpeed M1Speed M2
10000left 1200
11000left 2205
11100left 3207.5
01000left 4209
01100left 52014
00001right 1020
00011right 2520
00111right 37.520
00010right 4920
00110right 51420
00100straight 12020
01110straight 22020

4. Next, let’s proceed to program. You can use either Remote Desktop Connection or PuTTY through SSH Client, along this process. 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 LineFollowRpi.py

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 LineFollowRpi.py

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.



, , , , , , , ,

Related Post

Lesson 2: Setting up a Raspberry Pi 3 LoRa Gateway with HAT-LRGW-915

Getting Started with Encoder Sensor Module (SN-ENC-MOD)

Installing Raspbian OS on Raspberry Pi for Windows

Peak Current vs Continuous/Rated Current

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 ?

Leave a Reply

Your email address will not be published. Required fields are marked *