Programming Raspberry Pi Robot: Making it Follow the Lines (Part 2)

After we have defined the basic functions for our Raspberry Pi robot in the first part, we now want to use them by letting it follow a drawn line. To do this, we attach two optical infrared sensors (Line Follower: TCRT5000) to the body of the robot. If the line is lost, it is automatically searched for at an angle of 45° (adjustable) on both sides. If nothing is found, the robot stops.

This tutorial assumes that the functions of the first robot tutorial have already been implemented. If not, you should follow the previous tutorial first before proceeding here.


Required Hardware Parts

Raspberry Pi Line Follower

Raspberry Pi Line Follower Modul (TCRT5000)

As additional accessories, only two line follower modules are required in this tutorial (and of course female-female jumper cables). I also used hot glue to attach them. Black insulating tape is particularly suitable as a line.

It is also possible to use more than two IR Line Follower sensors, but the code must then also be edited accordingly. If you are recreating the project the way I did it, it is advisable to stick with two sensors.

Here you can already see a video of how the robot can later follow a line:


IR Line Follower Function

The TCRT500 (datasheet) as a module has 4 pins: VCC, GND and an analog and digital output. The analog output is unimportant in this case, as we can already see from the digital pin whether the sensor is above a line. In addition, there is also an LED which lights up if there is no line underneath (see the following images).

One of the two IR LEDs sends an invisible (infrared) signal. If this is now absorbed, the sensor is in all probability on a black line. Since the same effect is achieved as soon as the module is too far away from a surface, it should be ensured that the distance to the floor is approx. 1 to 1.5cm.

The choice of surface is also important: Smooth, slightly reflective surfaces are best (parquet, tiles, possibly also a floor covered with paper). The best contrast to this is the non-reflective black insulating tape, as I, therefore, used it.


Assembly and Connection

Before we attach the sensors, it is advisable to connect them: To do this, I attached both to each other on the sides with a little hot glue. After this has dried, I soldered the two VCC and GND pins to each other with a wire so that fewer cables are required. After attaching the jumper cables, I glued the assembled modules to the front wheel of the body with more hot glue. It is important that the distance to the ground is not too great. You should test the perfect height before attaching it (I fit exactly one finger underneath).

The further connection to the Raspberry Pi is very simple: VCC is connected as usual to the 3.3V pin of the Raspberry Pi, GND to GND and the digital output (D0) to a free GPIO pin. I chose GPIO 6 (right) and 19 (left) for this.

With the motors attached before and our motor driver IC, the schematic structure now looks like this:

Raspberry Pi Robot motor, line follower breadboard



Extended Code of the Raspberry Pi Robot

In order to follow the line, there are different modes that are checked: If both sensors detect the line, it is simply driven straight ahead, if only one of the two sensors detects the line, it is driven a little in the other direction. As soon as both sensors detect something again, the vehicle will drive straight ahead again. If neither of the two sensors detects something, an area (degrees_to_search) is searched for on both sides. If something is found, it continues as before. If not, the search ends and the robot stops.

So first we switch back to the previously created folder to create another file. This will later be responsible for all actions of the robot and control them.

sudo nano

The content is as follows:

And a first test can already be carried out. Create a test track (preferably not too sharp curves) and execute the following Python code:

The robot should now follow the line properly. Possibly, you can still adapt the angle (degrees_to_search) to your wishes. The robot can now e.g. already be used as a Pokémon Go egg breeder 😀


In the next Raspberry Pi Robot Tutorial, we will control the robot via infrared remote control.

Leave a Comment

Your email address will not be published.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!