Fun Projects for your LEGO MINDSTORMS NXT!

Home     Projects     Help     Contacts

Line Follower
Building: Intermediate
Program: Intermediate

Building Instructions


Start by building the Five Minute Bot.  Click the picture for building instructions.

Building Instructions


Connect the Color Sensor to port 3 on the NXT.

Line Follower Programming
Download Programs (help)

There are two programs provided for the Line Follower.  Both programs use the Color Sensor in the "Light Sensor" mode, where the sensor shines an LED lamp of one color (red is used here) and measures the amount of light that is reflected back into the sensor, as a number from 0 to 100.  Although under good conditions it is possible to follow a line using the Color Sensor in Color Sensor mode, the Light Sensor mode is better for this task. 

  1. The LineFollow program is a simple "Two State" line following strategy without any sensor calibration.  It will repeatedly turn either left or right in a "zig-zag" pattern, alternating back and forth on either side of the right edge of the line.  Start the robot near the right edge of the line. 

To use this program, you will usually want to first determine a good brightness value to use as the "cut-off" between the two states (light and dark).  To do this, you can use the LightMeter program to measure the brightness over the center of the line (darkest), then over the open floor (brightest), then average them to find the cut-off.  For example, if the Light Meter reads 20 over the line and 40 over the floor, then 30 would be a good cut-off value.

  1. The SmoothFollow program is a much more complex "Proportional Line Follower" which continually adjusts the steering response of the robot depending on how far off the line the robot appears to be.  If the robot is only slightly off of the edge of the line, then only a small amount of steering is applied.  When the robot is exactly over the right edge of the line, the robot will drive straight ahead at the full power specified.  This allows the driving to be smoother and faster than the LineFollow program. 

In addition, the SmoothFollow program does an automatic "calibration" sequence at the beginning, where the robot scans the line and the surface to determine darkest and brightest light values to expect, so there is no need to manually determine a cut-off value.  To get a proper calibration, start the robot with the sensor directly over the center of the line.


  • Try designing your own course for the Line Follower, with a mixture of gradual turns and sharper turns.  Electrical tape works well because it is dark, flexible, and leaves no residue when removed.  Adjust the Power levels used in the programs as necessary to allow the robot to still follow the course.  The tighter the turns that the course contains, the slower the robot will have to drive to avoid missing the turns.

  • Add the Color Sensor to another robot and see if you can make it follow lines too.  Which robots follow lines better than others?  Among other things, it will depend on how the wheels are spaced and where the sensor is placed on the robot.  Try modifying the "Gain" variable in the SmoothFollow program to try to tune the steering response needed for your robot.


Home     Projects     Help     Contacts

2007-2011 by Dave Parker.  All rights reserved. 
All project designs, images, and programs are protected by copyright.  Please see the usage policy.