How to Make a Line Following Robot Using Raspberry Pi


You all might remember the line-following robot I built using Arduino. Well today, we are going to do the same thing, but with a Raspberry Pi.

Special shout out to Matt Timmons-Brown for this project idea. He is the author of a really good book on Raspberry Pi robotics: (Learn Robotics with Raspberry Pi). Go check it out!


Here is a video of what we will build in this tutorial.


Here are the requirements:

  • Build a line-following robot using Raspberry Pi.

You Will Need


The following components are used in this project. You will need:


Connecting the Infrared Line Sensor

Make sure the Raspberry Pi is turned OFF.

Connect the VCC pin of the IR line sensor to pin 1 of the Raspberry Pi.

Connect the GND pin of the line sensor to the blue (negative) ground rail of the solderless breadboard.

Connect the OUT pin of the line sensor to pin 21 (GPIO 9) of the solderless breadboard.

Testing the Infrared Line Sensor

Power up your Raspberry Pi, and open IDLE.

Create a new program called

Save it in to your robot directory.

Here is the code:

import gpiozero
import time
# Description: Code for testing the
# TCRT5000 IR Line Track Follower Sensor
# Author: Addison Sears-Collins
# Date: 05/29/2019

# Initialize line sensor to GPIO9
line_sensor = gpiozero.DigitalInputDevice(9)

while True:
  if line_sensor.is_active == False:
    print("Line detected")
    print("Line not detected")

  time.sleep(0.2) # wait for 0.2 seconds

Create a line-following course using your black electrical tape and your poster board. It should look something like this.


I recommend leaving a 3-inch margin between the side of the poster board and the course. Don’t make curves that are too sharp.


Run your test program from a terminal window.

cd robot

Move your track in front of the sensor to see if the terminal prints out “Line detected. “


If you are running into issues, use a screwdriver to adjust the sensitivity of the sensor.

That white and blue potentiometer is what you should tweak.

Connect the other IR line sensor.

Connect the VCC pin of the IR line sensor to pin 17 of the Raspberry Pi using a female-to-female jumper wire.

Connect the GND pin of the IR line sensor to the blue (negative) ground rail of the solderless breadboard.

Connect the OUT pin of the IR line sensor to pin 23 (GPIO 11) of the Raspberry Pi.

Attaching the Sensors

Attach the first IR line sensor you wired up to the front, left side of the robot.

Attach the second IR line sensor to the right side of the robot.

Both sensors need to be just off the ground and can be mounted using 2×2 Lego blocks that extend downward from the body of the robot.

A piece of VELCRO is sufficient to attach both sensors.

Run the wires connected to the IR line sensors down through the gap in the robot body.


Create the Line-Following Program in Python

Open IDLE on your Raspberry Pi.

Create a new file inside your robot directory named

Here is the code:

import gpiozero

# File name:
# Code source (Matt-Timmons Brown):
# Date created: 5/29/2019
# Python version: 3.5.3
# Description: Follow a line using a TCRT5000 IR
# Line Following Sensor

robot = gpiozero.Robot(left=(22,27), right=(17,18))

left = gpiozero.DigitalInputDevice(9)
right = gpiozero.DigitalInputDevice(11)

while True:
  if (left.is_active == True) and (right.is_active == True):
  elif (left.is_active == False) and (right.is_active == True):
  elif (left.is_active == True) and (right.is_active == False):

Deploying Your Line-Following Robot

Place your robot on your track. Make sure the line-following sensors are directly above the black line.


Verify that your Raspberry Pi is connected to battery power, and your 4xAA battery pack is turned on.

Run your program from inside the robot directory.

cd robot

Watch your robot follow the line! Press CTRL-C anytime to stop the program.