Cannot figure out how to simply program Lego Mindstorm NXT robot to follow another moving robot that has a bright LED light attached to it.

Currently my programme looks like this:

Follow Light

  • It sounds like you are unfamiliar with how the light sensor works. First off, it's not directional. Secondly, it detects all light, including ambient light, so your LED may not be bright enough to even register on the light sensor.
    – Nick2253
    Commented Nov 12, 2014 at 16:20
  • A few things: The light sensor has to be pretty close to something in order to be very functional. How close will the other robot be? How much ambient light would there be in the room? Do you have more than one light sensor? Also, you would probably get much more reliable/feasible results using an ultrasonic sensor. Is there a reason why you must use light?
    – PGmath
    Commented Nov 13, 2014 at 2:35

1 Answer 1


If I were you, I'd make it this way:

The leader robot has a very bright light shining backwards, but not in a tight beam, rather a wide cone, so it can be seen from the back easily.

The follower robot has the light sensor facing forward, with a cowl around and in front of the sensor in order to narrow its sensing angle. In other words, the sensor is modified in such a way that it only picks up anything if the light source is directly in front of it, not to the sides. This will help filtering out background "noises" (ambient light and other light sources).

Then the follower robot runs this pseudocode:

while true // enter stopping condition here if required
  while light is visible // current light value is over a threshold - experiment with this value
    go forward some amount
  // if light is not visible the while loop exits and we need to find the light
  store current light level to variable FRONT
  rotate 10 degrees to the right // experiment with various values, maybe less is enough, maybe more is needed
  store current light level to variable RIGHT
  if RIGHT < FRONT // the light probably moved to the left
    rotate 20 degrees to the left // twice the amount in the previous turn
  // else the light probably moved to the right, where we are currently headed
  // so there is no need to rotate further, just continue with the next iteration, 
  // checking if we see the light now

This way the follower will go after the leader while it is headed in the right way, but when the follower loses the leader, it stops and turns around to look for the light.

This method however has a drawback. If the follower is slower than the leader, it will be left behind during the "stop and look around" phases, but if the follower is faster, it will run into the leader from the back.

You could remedy this by giving the leader a bumper on the back connected to a push button (pressure sensor), and program the leader to only move forward when something (probably the follower) has pushed into its back bumper.

Let me know if you need additional help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.