Project 2: Line Following


Line following is one of the basic tasks for professional and hobbyist robot builders alike. Solve a maze, move products around a warehouse (search about automatic guided vehicles), or automate an assembly line, these are all applications of line following. The heart of any line following algorithm is an IR sensor. To find our more about how an IR sensor works and how to connect it to and Arduino, we recommend checking out our IR sensor tutorial. For this project you will need a roll of black tape and a white surface (to make a path to follow)l in addition IdeaBot.


Assembled body with the IR bumper attached to it.

Before you can program IdeaBot to follow a line, you must first assemble the IR attachment.

Step by step assembly instructions are shown below.

1: Locate the IR holder.

2: Locate the 3 (or 1, 1 sensor is enough for line following) IR sensors.

3: Align the IR sensors with their corresponding holes, the extra holes give added flexibility to move sensors left and right.

4: Locate 3 M3x8 mm, nuts and bolts.

5: Tighten the nuts and bolts.

6: Locate the 2 M3x45 mm standoffs, and 2 M3 mm nuts.

7: Tighten the standoffs and nuts, the extra holes on the IR holder give added flexibility.

8: Locate 2 M3x8 mm bolts and align them with the chassis and the IR holder.

9: Tighten the bolts, the extra holes on the chassis give extra flexibility to move the IR holder forwards and backwards.

Connections and Coding

StartUp Shield makes connecting sensors to Arduino very easy, the picture below shows how to connect one IR sensor and two servos.

Connecting the servos to pins D9 & D11 which are servo 2 & 4, and the IR sensor to A5 since it is analog

The IR sensors provided with IdeaBot are analog sensors, meaning that they provide a continuous stream of measurement data, input from most analog sensors compatible with Arduino varies from 0 to 1023. For an IR sensor, 0 corresponds to completely dark surface and 1023 to a completely white, reflective surface. A simple code that reads and displays the output of the IR sensor to your laptop is shown below.

Note: it is better to read the values of any sensor before using it to detect the threshold value and to make sure that you have a good reading from your specific environment.

copy paste the code below in your arduino software, then download it and test the sensors reading as in the pictures below using serial monitor.

void setup()
  Serial.begin(9600);     // begin serial communication at 9600 which is the speed rate, a protocol used to transfer data between controllers
                                        // necessary to transfer data from Arduino to computer

int reading;          // defining an integer value called reading,  which is used to store the read data from the IR sensor.

void loop()
  reading = analogRead(A5);     // the sensor is connected to analog port 5
  Serial.println(reading);      // print the data on computer

In order to see the data output on your computer, you need to open the serial monitor, Ctrl-Shift-M in the Ardunio environment. Moving the sensor over a white background and then a black line will give you data like this :

Reading the white colored surface

Reading the black colored tape

Your numbers may be slightly different but in general, anything less than 400 can be considered white, anything else can be considered black. In this case, 400 is what is called a threshold value, it is used to split the data range of 0- 1023 into only 2 possibilities, black or white. The threshold value may have to be modified depending on the background type and color, and the line type and color.

Explaining the new commands above

int reading;
reading = analogRead(port number);

analogRead(port number); is the command used to order the arduino to take the input readings from the analog pins, no matter what analog sensor is connected to those pins the values read will vary from 0 to 1023. Those values represent a physical meaning depending on the sensor used, in our case the IR sensor values represent dark and light surfaces.

int reading; is a command used to declare and store an integer value named reading or whatever other name given in the arduino memory and can be used for many purposes like references or comparisons, you will see more examples below.


Follow the Line

We now have all the tools we need to program IdeaBot to follow a line. The simplest possible line following algorithm is shown below:

The video at the start of this tutorial shows this algorithm in action.

If, Else Functions !

The if else function is a truly useful and very essential tool in programming. The main concept of the these statements is to have conditions and execute them when they are true

This code below explains how we can write these functions which are so simple but powerful, and in general it goes as follows:

if ( statement is true )       // the statement could be a conditioned value like SensorReading>Threshold


   do something                 // Do the commands written here in case the condition is true.




    do something else   // if condition is not true keep doing these commands here.


To program IdeaBot to do what it does in the video, use the code below:

#include <Servo.h>

Servo left;    // create servo object to control a servo, here we called them right and left to make connections more easier.
Servo right;

int reading;        // variable to hold sensor readings

void setup()
  left.attach(9);    // left wheel, attach pin 9 to servo a
  right.attach(11);    // right wheel, attach pin 11 to servo b

void loop()
  reading = analogRead(A5);    // read sensor values and store them in the integer 'reading'

  if ( reading > 400)      // if reading is greater than threshold, on line
    // turn away from line
    left.write(60);          //move forward
    right.write(90);        // stop
    // if not on the line, turn towards it
    left.write(90);            // stop
    right.write(120);       // move forward.


The Challenge !

An algorithm that you will make, are you ready ?

Now that you have mastered the simple 1 sensor line following algorithm, try to think of more advanced algorithms using 2 or 3 sensors, we challenge you to write a code that solves the following maze.


  • Use two or three line following sensors.
  • One sensor could be to identify the right turn.
  • Could be solved by more than one approach.
  • Write down your solution (Algorithm) and think of it  before you program.
  • Grab a cup of coffee or tea and enjoy :)

If you solve it send us your code and a video of it in action to [email protected] and we may post it on our website.

Detecting obstacles

Time to watch out when you are moving!

  Project 3: Detecting obstacles

We ship to the GCC and MENA regions.

Reliable and fast shipping to your door, quality assured.

More about shipping

Find us on

Just type @idea_link

Visit our profile