In this lesson, we will add 2 black/white tracking sensors to the framework built in Lesson 1. If you have not completed installation in Lesson 1 and Lesson 2, please review 2WD Robot Car Starter Kit Lesson 1 and 2WD Robot Car Starter Kit Lesson 2

The software in this lesson will read data from these 2 black/white tracking sensors and automatically guide the 2WD Robot Car to move along the black track line in the white ground.

Parts and Devices

Device Picture Qty. Accessories
Tracking sensor module 2 M3*5 Screw x4

M3*12 Double Pass Copper Pillar x2

2WD Robot Car Chasis

(Installation please refer to Lesson 1 and Lesson 2)


Hardware Installation

1) Install the 2WD Robot Car Chasis

Install the smart car basic framework as per 2WD Robot Car Starter Kit Lesson 1 and Lesson 2. If you have already completed installation in Lesson 1 and Lesson 2, just keep it, no need to remove any module installed. You can assemble tracking sensor modules into empty holes at the front of the chassis.


2) Install the tracking sensor module

Install the tracking sensor module under the chassis with M3*12 Double Pass Copper Cylinders and M3*5 Screws. Make sure that every sensor head is over the gaps at the front side of the chassis so that the sensor can detect black lines in white ground.


The tracking sensor is used to recognize the black and white lines. When the sensor detected black line, it will output high level, the signal light off; when the sensor detected white line, it will output high level, the signal light on. The sensitivity of the module can be adjust by adjustable potentiometer.

3) Connect the tracking sensor with Arduino UNO Sensor Shield:

The Right Tracking Sensor
Tracking Sensor Arduino UNO Sensor Shield
AO Not Connected
The Left Tracking Sensor
Tracking Sensor Arduino UNO Sensor Shield
AO Not Connected

Software Installation:

Step 1: Install latest Arduino IDE (If you have Arduino IDE version after 1.1.16, please skip this step). Download Arduino IDE from , then install the software.

Step 2: Download and unzip , you will see a folder called lesson-3.

Step 3:  Connect UNO R3 board to PC with USB cable, open Arduino IDE, choose corresponding board/port for your project,upload the sketch to the board.

Step 4:  Arduino IDE: Click file -> click Open -> choose code “lesson-3.ino” in lesson-3 folder, load the code into arduino.

Code Explanation:

Port Definitions between tracking sensor and Arduino UNO:

  1. #define LFSensor_1 2 //line follow sensor1
  2. #define LFSensor_2 3 //line follow sensor2

Defines an array, stores the signals detected by the tracing module in an array:

  1. char sensor[2]={0,0};

The following two variables represent the speed of the car turning and executing:

Notice: Do not set the speed too large, when the speed is too big, the car may run out of track.

  1. #define M_SPEED1 150 //motor speed
  2. #define M_SPEED2 170 //motor speed

The following is the core code of the tracking function. The car will go straight when the black line is located between the left and right tracking sensor modules; the car will turn left when the left tracking sensor module above the black line; the car will turn right when the right tracking sensor module above the black line; the car will stop when the left and right tracking sensor module are both above the black line.

  1. void auto_tarcking(){
  2. read_sensor_values();
  3. if((sensor[0]==LOW)&&(sensor[1]==HIGH)){ //The right sensor is on the black line.The left sensor is on the white line
  4. set_motorspeed(M_SPEED1,M_SPEED1);
  5. turn_right(20);
  6. }
  7. else if((sensor[0]==HIGH)&&(sensor[1]==LOW)){//The right sensor is on the white line.The left sensor is on the black line
  8. set_motorspeed(M_SPEED1,M_SPEED1);
  9. turn_left(20);
  10. }
  11. else if((sensor[0]==LOW)&&(sensor[1]==LOW)){//The left an right sensor are on the white line.
  12. set_motorspeed(M_SPEED2,M_SPEED2);
  13. go_ahead();
  14. }
  15. else if((sensor[0]==HIGH)&&(sensor[1]==HIGH)){//The left an right sensor are on the black line.
  16. go_stop();
  17. }
  18. }


Step 1: Prepare a black track on white ground. (the width of the black track is more than 20mm and less than 30mm)

Please note, the bend angle of the track can’t be larger than 90 degree. If the angle is too large, the car will move out of the track.

Step 2: Adjust the sensitivity of tracking sensor modules.

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

Step 3: Turn on the car and put the middle of tracking sensor module facing over black track, and then the car will move along the black track.

Use the IR controller, press “OK”, the car will along the track; press “0”, the car will stop.

If the car can’t move, please check the following:

  1. If the battery can work;
  2. If the IR controller is too far away from the receiver;
  3. If the connection is right;
  4. If adjusted well the sensibility of the tracking sensor

1 Comment

Leave a Reply

WordPress spam blocked by CleanTalk.
Bombax Theme designed by itx