Help programming line follower

Hi all
I worked in school with lego mindstorms and I wanted to do some Projects on my own but the lego mindstorms set was too expensive so I got some old sensors from school and ordered the bricktronics shield… i would like to do a line follower like we did in School with two light sensors and two Motors but I dont exactly know how to do it I used all examples and they are all working fine. can someone help me with the code I used only the lego Software till now.

thank you

Hello Amy, thanks for making a post here. Sounds like a very fun project! I’m sure we’ll be able to get it working.

Which kind of light sensors did you get?

In the image above, the sensor on the left is the NXT Color Sensor, and the sensor on the right is the Light Sensor. Both types work with the Bricktronics Shield software.

The first step might be to play around with the light sensors to see how well they can detect the line. Maybe make a little sketch that just reads out the raw value from the sensor and prints it to the serial console so you can see what values it reads when the sensor is on top of the line, and what values it reads when the sensor is NOT on top of the line. If the numbers change a lot between line and not-line, then you’ll be able to have the robot code know if it is over the line. You might want to build up your robot’s frame at this point to mount the two sensors just above the floor while it’s rolling around. It would be good to test out the motors and make sure you can easily drive it forward in a straight line, as well as do turns to the left and right.

For the actual line-following algorithm, it’ll be easier if you have two sensors with the line between the sensors. You can drive forward until one of the sensors detects the line, which means that you need to turn towards the sensor that detected the line. For example, if your line is curving to the right and you drive straight forward, eventually the line will move under the right-side sensor. Now you need to turn the robot a little bit to the right and resume driving forward. You probably want to start out with a slow speed at first, otherwise you might overshoot with your turns!

If you only have a single sensor, it is a little more tricky but still possible. With a single sensor you always want it to be detecting the line. If it “falls off” the line, then you want to turn in place left and right until you find the line again. It’ll probably be slower than the two-sensor line following robot, but still pretty cool!

I hope that helps, and do let us know if you have questions or need more tips.


Hi Matthew thank you for your nice help!
I programmed my Roboter so when he is driving over a black underground he writes the numbers 70-99 and when he is on a white one it is under 70.
I have 2 of the right sensors!

I build a Robot like the one in school with two sensors in the front pointing at the ground - the line fits exactly between them. I cant figure out how to for example turn the Robot off when he is driving from a white to a black underground. I have nobody in my family who could help me with that and in school they only can work with the lego mindstorms…

Hi Amy, sounds like you are on the right track to have an excellent robot.

The Bricktronics Motors can be used in a few different ways, but the easiest is to use the setFixedDrive function. This function tells the motor to run at a specific speed, either forwards or backwards.

m.setFixedDrive(75); // Go forward at speed 75 out of 255

m.setFixedDrive(255); // Go forward at max speed!

m.setFixedDrive(-100); // Go backwards at speed 100 out of 255

When you call that function, the motor will turn at that speed until you tell it to turn at a different speed or stop. You can use the brake() function to stop the motor.

m.brake(); // Stop the motor

This might be a good starting place:

// Include the Bricktronics libraries
#include <BricktronicsShield.h>
#include <BricktronicsLight.h>
#include <BricktronicsMotor.h>

BricktronicsMotor motorLeft(BricktronicsShield::MOTOR_1);
BricktronicsMotor motorRight(BricktronicsShield::MOTOR_2);
BricktronicsLight sensorLeft(BricktronicsShield::SENSOR_1);
BricktronicsLight sensorRight(BricktronicsShield::SENSOR_2);

void setup()
  // Be sure to set your serial console to 115200 baud

  // Initialize the Bricktronics Shield

  // Initialize the light sensor connections

  // Initialize the motor connections

#define SPEED_NORMAL 50
#define SENSOR_THRESH 70    

void loop() 
  // Drive forward

  if (sensorLeft.scaledValue() > SENSOR_THRESH ) {
    // Left sensor hit the line, turn left until it leaves the line.
    // Turn left by slowing down the left motor.
    motorLeft.setFixedDrive(SPEED_NORMAL / 2);

    while (sensorLeft.scaledValue() > SENSOR_THRESH ) {
      delay(1); // Wait until we have turned left enough that the left sensor leaves the line

    // Now that we are back to normal, reset the left speed so we go straight

  if (sensorRight.scaledValue() > SENSOR_THRESH ) {
    // Right sensor hit the line, turn right until it leaves the line.
    // Turn right by slowing down the right motor.
    motorRight.setFixedDrive(SPEED_NORMAL / 2);

    while (sensorRight.scaledValue() > SENSOR_THRESH ) {
      delay(1); // Wait until we have turned right enough that the right sensor leaves the line

    // Now that we are back to normal, reset the right speed so we go straight

I am just introducing Bricktronics to the school I am teaching at. We had started out with the NXT kits and this is the next step I would like to take with my classes. They had done the follow line with a single sensor and I am wondring how that would work with Bricktronics Shield.



Hi Lee, thanks for the question! And welcome to the W&L discussion board!

As I mentioned above, for a single-sensor line-following robot, you want to keep the sensor over the line at all times. If the sensor detects it went away from the line, you want to turn the robot body until the sensor hits the line again, then continue forward. You probably want to rotate the robot back and forth in increasingly-wide arcs until you find the line again, as a general strategy.

Hope that helps you get started! You can probably use some of the code from my post above to get started, but customize it for your single-sensor situation. Let us know how it goes!