#Robotics Programming a Line Following Robot – Part 1

David Meego - Click for blog homepageWelcome to my first article in a series on robot design and programming techniques. I thought I would start with an article discussing how to make a robot follow a line with a single sensor. I will be discussing some of the different methods and explain the pros and cons of each.

Disclaimer: This article will use Robocup Junior Australia‘s Rescue (Line) Challenge with a Lego Mindstorms based robot as an example.

For the Rescue (Line) Challenge in Robocup Junior Australia, the teams must create a robot that can follow a line to a rescue zone and then complete a rescue. This article will discuss the line following aspect and will hopefully provide enough information for a team to create a robot that will follow a black line on a white background. This article will not cover avoiding obstacles, taking shortcuts (green squares) or what to do in the rescue zone.

Below is an example of a Robocup Junior Australia Rescue (Line) course:

Light Readings

Before we get started on any line following we need to understand light readings. Every sensor is different. Every course is different. Every room’s lighting is different. So it is vitally important that light readings are taken and the program adjusted to match the current conditions.

If you fail to take light readings and your robot works, you are just lucky. It is possible to have a robot take readings as part of its program and adjust its behaviour accordingly. It is also possible to have a robot auto calibrate by continuously monitoring light levels. For now we will just cover the basics with manual steps.

For each sensor, take a reading for white and a reading for black. The trigger point for that sensor will be the middle or average of the two readings.

Trigger = (White + Black) / 2

So, if the White reading was 50 and the Black reading was 10, the Trigger point is (50+10)/2 = 30.

Single Sensor Line Follower – Wiggle

The simplest line following robot you can create would be based on a controller with two motors and a single light or colour sensor.


  • I will recommend colour sensors over light sensors as you will need to be able read colour later. However, for line following it is best to use the colour sensor in light or brightness mode.
  • If you use a colour sensor in colour mode for line following, there are times when the sensor is half on the black line and half on the white, that it can produce incorrect readings of other colours.

The algorithm to make this robot work is as follows:

  • If the sensor sees black, power the motor on one side only
  • Else power the motor on the other side only

This method is a “Single Sensor Wiggle” and is usually the first method taught to students. Below are the examples for this type of robot if built from Lego Mindstorms with the motors plugged into ports B & C and the sensor plugged into port 3.



EV3 Basic

'  ** Wiggle Bot 1 **

'  By David Musgrave

'  http://WinthropDC.com

'  Last Modified: 03-Jul-2017

' ** Setup Starts Here *******************************************************************************************************

' Initialise Variables
Clicks = ""
Finished = "False"

' ** Main Program Starts Here *******************************************************************************************************

Sensor.SetMode(3, 0) '  Color to Reflected Light

' Program Main Loop
Finished = "False"
While Finished = "False"
  If Sensor.ReadPercent(3) >= 30 Then
    Motor.StartSync("BC", 50, 0)
    Motor.StartSync("BC", 0, 50)

    ' Check for Button
  Clicks = Buttons.GetClicks()
  If Text.IsSubText(Clicks, "E") Then
    Finished = "True"
EndWhile ' Finished

' End Program
Motor.Stop("BC", "True")


  • If the robot ignores the line and goes straight over it, then your light readings and sensor settings are incorrect. Retake your readings and calculate the trigger point and update the value used by the sensors.
  • If the robot goes backwards, change the motors to the opposite direction. On NXT, change the direction. On EV3 and EV3 Basic, change the motor values to negative values.
  • If the robot turns away from the line, start the robot with the sensor on the other side of the line.
  • If the robot loses the line on 90 degree corners, slow the motor speeds down on both motors until it works reliably.
  • If the robot cannot make a hairpin turn sharp enough, change the speed of the “stopped” motor to go backwards. On NXT, change the direction and set the speed. On EV3 and EV3 Basic, enter a value with the opposite sign of the other motor’s value.

Sadly while this method will follow a line, it has a number of drawbacks which is why I recommend not using it:

  1. It is very slow. The fastest path between two points is a straight line and the wiggling is just making the robot’s progress slow. There is a time limit in the Robocup Junior Australian challenge and you need the robot to reach the rescue zone as fast as possible.
  2. It will fail if there are any breaks in the line. A small gap between tiles or a broken line challenge tile will cause the robot to turn around.
  3. A single sensor cannot be used to read the green shortcut markers on either side of the line.

There are some other methods with a single sensor which will provide smoother line following, but do not solve the other issues. These methods include cross wiggle, stepped or steering.

Single Sensor Line Follower – Cross Wiggle

This is variation of previous Wiggle method. The idea is to alternate motor directions each time the sensor crosses the line and reads white, black and back to white. It will move forward a little faster than the previous wiggle but will wiggle more violently. It will also suffer from the other issues mentioned.

Single Sensor Line Follower – Stepped

The Stepped method changes the binary, on/off, true/false approach of the Wiggle method into three or five steps. We would break the range of readings between White and Black into an odd number of sections. So the algorithm for five steps based on the example readings would be something like:

  • Values below 20, Turn Sharp Left
  • Values 20 to 27, Turn Left
  • Values 27 to 33, Go Straight
  • Values 33 to 40, Turn Right
  • Values above 40, Turn Sharp Right

A three step version would just have Turn Left, Go Straight, Turn Right.

Single Sensor Line Follower – Steering

The Steering method is like the Stepped method but with many more steps. The idea is that use the NXT Move block or EV3 Move Steering blocks and feed it the value of Reading subtract Trigger (or Trigger subtract Reading if it turns the wrong way). If you need it to turn sharper, you can multiply the result of the previous calculation by a turn factor to increase the turning values.

Based on the example readings, the range of 10 to 50 with a trigger of 30 will give a range of steering values from -20 to 20. If we multiply this by a turn factor of 5, we have a range of -100 to 100. This final value can be used as the input to the block.


Please feel free to create programs for other methods mentioned and test them yourself.

The following conclusions can be made from what we know so far:

  • Single sensor can work without wiggling.
  • Single sensor cannot handle breaks in the line.
  • Single sensor cannot read other colour markers on either side of the line.
  • If any method loses the line on a sharp corner, increase the differential between motor speeds to make it turn faster.
  • If the robot still loses the line, decrease the overall speed as there will be limits caused by the speed of processing, reaction time for the motors and physical inertia that will limit the maximum reliable speed of the robot.
  • Reliability is more important than speed. In a competition, losing the line will cause penalties and force a restart which will also use up precious time.

More Information

For more information on robotics and the EV3 Basic extensions to Microsoft Small Basic, check out the following links:

In the next article, we will discuss methods for following a line with two sensors.



PS: I know I have not covered PID (Proportional, Integral, Derivative) algorithms. I have found that they are complex to program and still cannot follow a line as fast as some of the simpler methods.

This article was originally posted on http://www.winthropdc.com/blog.


4 thoughts on “#Robotics Programming a Line Following Robot – Part 1

Please post feedback or comments

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s