In this lesson, we use C++ to program our Raspberry Pi robot car to automatically drive along  a black line in white ground . We will use five IR tracking sensors to detect the line.

You will learn how C++ program read digital data from GPIO pins.

If you are interest in Python programming, we have a Python solution for this project in

No. Picture Device Qty. Accessories Link
1 Tracking sensor module 1 M2.5 Plastic Screw x 2
M2.5 Plastic Nut x2
M2.5 Plastic Pillar x 2
Click here to buy
2 7pin 25cm Female to Female Cable 1 Click here to buy
3 Philips screwdriver 1 Click here to buy

Step 1: You must complete Lesson 1 basic frame work

Step 2: Install tracking sensor modules under lower car chassis with 2pcs M2.5 plasctic screws, M2.5 plastic pillars and M2.5 plastic nuts.

Step1: Connect GND-VCC pin of tracking sensor module to GND-5V of PCA9685 compatible module; connect IR1, IR2, IR3, IR4, IR5 pins to GPIO5, GPIO6,  GPIO13, GPIO19, GPIO26 of Raspberry pi with 7pin 25cm female to female cable as the following photo shows (Remember : DO NOT remove any existing wires installed in Lesson 1 ):

Step 2: Adjust the sensitivity of tracking sensor modules.

Turn on and hold the car and adjust the potentiometer on the tracking sensor with Philips screwdriver until you get the best sensitivity status: the signal indicate LED light will turn on when sensor is above black track, and the signal LED will turn off when the sensor is above white ground

Step 1) If you have installed PCA9685 C library in Lesson 1, you can skip this step. Otherwise you need install the library by typing following command:

tar -zxvf pca9685.tar.gz

Step 2) Download line tracking C code by typing following command:

Step 3)Compile Lesson 2 code by typing following command:

gcc -o lesson2 lesson2.c pca9685/pca9685.c -lwiringPi

Step 4) Type following command to run the program:


our car will move along the black track line.