Lego Line Follower

Playing with Legos again tonight! I made a little simple Lego Mindstorm line follower using the light sensor and two motors providing differential drive.

Simple project tonight because I haven’t posted in forever and this was fun. More code and video after the break.

My Lego Mindstorms are super fun and are great for prototyping out new robot projects. For the line follower, I made a simple robot platform using the instructions from for the design 9797 Simple Car. The little swivel castor shown below is a nice improvement and I mounted the light sensor at the front of the robot instead of behind to prevent it from interfering with the castor.

This version shown here tonight uses the default line following code bundled with the fantastic RobotC programming environment from I have reproduced pseudo-code below to illustrate their approach:

task main() {
  while (true) {
    // if you don't see the line turn left
    // otherwise turn right
    if (sensorValue(lightSensor) < 55 ) {
      motor(A) = 20;
      motor(B) =  5;
    } else {
      motor(A) =  5;
      motor(B) = 20;
  } // end while
} // end main

One thing I realized while testing out this code is that the difference between motor A and motor B determines the minimum line radius that can be followed.  This 4x difference works well for my small test pattern in my apartment. I could probably reduce this for a larger course.

I made my test matt using some grey plastic from the Art Supply store and the white line from hockey stick tape I had laying around.

Next steps:

  • Automatic sensor calibration (wave the sensor over the line and set threshold as average of the min/max)
  • Increase speed until the robot flies off the table
  • Adapt my Pololu 4-sensor line following sensor to the Mindstorms
  • Adapt my Pololu 6-axis gyroscope/accelerometer to the Mindstorms to do hill detection
  • Add a decision point with a branching path or maybe a figure-8
This entry was posted in Line Following, Projects. Bookmark the permalink.

Leave a Reply