Challenge 24 – Edge Following – Line Following.

Summary: Following a line is a classic challenge in robot engineering. Robots are often equipped with one, two, three or more light sensors to enable them to follow a line. In Challenge 24 we look at the task of following a line with a robot that uses only one light sensor. If you look at the Challenge 24 student videos, you will notice that the robots shown using one light sensor are not actually following a line, they are actually following a light/dark edge.

The way the robot follows a light/dark edge is to oscillate between the dark and light sides of the edge of the line, advancing a little further forward with each oscillation.

In this note we look at NXT-G programs that will allow a robot to follow the edge of a line, and a way of changing the programme so that it follows the opposite side/edge of the line.

Following An Edge Using NXT-G.

The following NXT-G program will allow a one-light sensor robot like ClareBot to follow the edge of a line. If you are using DomaBot with the light sensor attached at the end of the robot that has the swivel wheel, the directions of all four motor blocks (shown in the diagrams below) should be reversed.

The calibration values for light and the motor speeds shown in the diagrams below worked quite well on my version of ClareBot in a typical classroom. However these light and motor values will almost certainly have to be adjusted so that the motor speeds fit your robot, and the light values are appropriate for the light in your classroom.


The video “Reminder” between Challenges 24 and 26 demonstrates how to calculate your substitute for the “50” value shown in the diagram above. If your room is very brightly lit, the value to you calculate will probably be above 50. If your room is relatively dim, a value lower than 50 is likely to be appropriate.



In the two diagrams above, and the two diagrams below, the motor speed of 20 backwards, and 50 forwards, allowed my version of ClareBot to follow edges with quite sharp bends. You should experiment with these speeds, as it is likely the your robot will need different motor speeds, depending on the size of the wheels attached to your robot, and the sharpness of the turns of the edge of the line that is being followed by your robot.

If the curves of the line are very gentle, a forward speed of 70, and a “backwards” speed of 0 (zero) may work. If your robot has to navigate the end of a line, returning on the other side of the line, values such as 40 forwards and 30 backwards may be necessary to prevent the robot losing the end of the line. Experiment!



Keep things simple - set the loop to “Forever”.


You can now download the program into your robot, and try the program out.


  • If your robot is going backwards, reverse the directions of all four of the motor commands 2,3,4,5.
  • If the robot has problems following the edge of the line, try starting it on the other side of the line.
  • If the robot spins around in circles, the calibration of your light sensor is probably wrong. Go to the “Reminder” in between Challenges 24 and 26 to find out how to calibrate your light sensor.
  • If the calibration of your light sensor is correct and there are still problems, check to see if your robot’s light sensor can move or wobble. Moving, pivoting, wobbling or insecure light sensors will be impossible to calibrate accurately – re-build your robot so that your robot’s light sensor is secure and does not wobble.
  • The gap between the bottom of your robot’s LEGO light sensor and the floor, should be about half to three-quarters of a centimetre (about 3/16 to ¼ of an inch). Too close, and the light sensor will have trouble telling the dark and light portions of the line apart. Too far away, and the robot will be easily distracted by stray light from around the room.

Following the other side of the line.

There are times when you may prefer your robot to follow the opposite edge or side of the line. You can achieve this by swapping the top and bottom lines of the code (see the code below, and compare it to the code at the top of this web page).

Following the line using more than one light sensor.

If your robot is entered in a competition, the speed at which the robot follows the edge of the line may be important. Some additional speed may be gained by adjusting the motor speeds. However, more speed can be gained by using two light sensors and a more complicated NXT-G program. Check out Challenge 102 in for an outline of a method for using two light sensors when following a line. 

Edge Following. - Tutorial 24 - NXT-G - Mentor Teacher notes - LEGO MindStorms NXT -