Line Following with the Light Sensor
In the last class we used the Light Sensor to detect when our robot encountered a dark line. In this class, we're going to use the Light Sensor to help our robots follow along a dark line. Specifically, we will follow the edge of the line.
Let's say we want our robot to follow a dark line on white paper. Remember, if we calibrated it, the light sensor will output a value from 0 to 100 depending on how much of the sensor is over the dark line. 0 means the sensor is totally over the line. 100 means the sensor is only over the white paper. Imagine what the sensor would see if it moved from left to right over a dark line (see picture below):
Before we work with the line sensor, we'll need to calibrate it for the lighting conditions in the class today. Last week you loaded a program onto your robot called "Calibrate.rbt". Run it by holding the light sensor completely over the black line, and pressing the orange button on the brick, then holding the robot over the white paper and pressing the orange button again. You can test the calibration by viewing the output of the light sensor in a Light Sensor Block in the NXT-G window. Ask your instructors for help if you don't remember how to do this.
Exercises
Exercise 1: Write a line following program
We're going to tell our robot to follow the left edge of the line. In the picture above, we see that the sensor reading is high (> 50) if the sensor is mostly over the paper, and the sensor reading is low (< 50) if the sensor is mostly over the line. We can use this fact to keep the sensor lined up with the left edge of the line while the robot moves forward. Let's write some pseudocode that does this:
Get output from the sensor
If the light sensor output is greater than ( > ) 50, we must be left of the line's edge
Move forward and turn slightly to the right
If the output is less than ( < ) 50, we must be right of the line's edge
Move forward and turn slightly to the left
- Repeat from the beginning
Now let's turn this pseudocode into an actual NXT-G program. Create a program called linefollow.rbt, and save it to your folder.
Since the program repeats, we'll need a loop

Now notice we have an "If" statement in our pseudocode. This means we have to make a decision based on the output of the light sensor. We'll use the "Switch" block (from the Common menu) for this. Select the settings on the switch block to use the light sensor output, and make a decision based on whether it is greater than or less than 50.


First fill in the top part of the switch statement. This is what will happen if the light sensor value is greater than 50 (see that the "light symbol" looks bright). Remember that a value greater than 50 means that the robot is positioned left of the line. We want to make the robot move forward, and slightly to the right to get closer to the line. Instead of using a Move Block, we are going to use two Motor Blocks to do this. The motor blocks give the robot a little more control over the turn, and make the movement smoother. Notice we have one motor block for motor B, and one for Motor C. Since Motor B is on the left and Motor C is on the right, if motor B spins faster (has more power) than Motor C, the robot will move to the right. Set the power for Motor B to 50 and the power for Motor C to 10.

Now let's fill in the bottom part of the switch statement. This is what we do if the light sensor returns a value less than 50. When the value is less than 50, the light sensor is mostly over the line. Since we want the robot to follow the left edge of the line, we'll want it to move forward, and slightly to the left. In this case, we'll want Motor C to have more power than Motor B. Add two motor blocks like we did in step 3, but set the power for Motor B to 10 and the power for Motor C to 50.

When all blocks of the program are combined it may something like this:

That's the whole program! Your instructor has made a course to test your program using Robotsquare's Mindstorm Tile Template (.pdf) similar to the one displayed below. Try placing the robot near the left edge of the line and seeing if it can follow the course.

Challenges for You
(1) The Need for Speed!: Using the course mapped out by your instructors, time how long it takes your robot to complete the course. Can you think of ways to make it go faster without losing its way?
Hint: Try completing Challenge 2 below for one method to make your robot a speedier line follower.
(2) Write a 3-State Line Following Program: The line following program we just wrote works best following a line where the curves aren't too sharp. If you try to get the robot to follow a sharply angled turn, it may lose the line entirely. The robot also moves pretty slowly, since it has to spend so much time turning left and right to find the edge of the line. Let's try a method to make the robot move faster and more accurately.
Get output from the sensor
If the output is less than ( < ) 50, we must be right of the line's edge
Move forward and turn slightly to the left
If the output is less than ( < ) 50, we must be right of the line's edge
Move forward and turn slightly to the left
Move forward and turn slightly to the left
Move forward and turn slightly to the left
Move forward and turn slightly to the left
- Repeat from the beginning
The code may look tricky because it has an "if" statement inside another "if" statement, but if you think carefully, you will see how to write the program. You will need to use more than one switch statement to get it to work. Ask your instructors if you need help.