Lesson 9 – Line Tracking

Java Concepts


Teacher Materials

Get Access

The Finch contains two line sensors on the bottom of the Finch on either side of the on/off button. They are infrared sensors that emit infrared radiation and measure the amount reflected by the surface below the Finch. You can use these sensors to detect whether each sensor is over a white surface or a black surface.

Picture shows the positions of the two line tracking sensors. There is one to the right and to the left of the Finch power button.

Using the Line Sensors

For this lesson, you will need large sheets of white paper and black electrical tape about 2 cm wide. Use the tape to make a black line on a white background.

The getLine() method returns the value measured by a line sensor from 0 to 100 (no units). This method requires one parameter, a String that indicates whether you are interested in the right or left sensor.

Exercise 1

To use the line sensors in a Boolean expression, you need a threshold. Write a program to find this threshold. Your program should do the following:

  • Prompt the user to place the Finch over a white surface. 
  • Record the mean of the left and right line sensors in a variable named white
  • Prompt the user to place the Finch over a black surface.
  • Record the mean of the left and right line sensors in a variable named black.
  • Compute a threshold for the line sensors that is the mean of white and black.
  • Reports the threshold to the user.

Now you can use your threshold to create Boolean conditions in if-else statements and while loops. For example, this if-else nested inside a while loop will repeatedly print black if either sensor detects the black line and white otherwise.

Exercise 2

Write a program to make that robot drive forward until it detects a black line.

Line Tracking

The most common use of the line sensors is to track a line. Use black electrical tape to create a path on a large sheet of white paper. Avoid sharp turns; it is easier for the Finch to follow gradual curves.

To track the right side of a black line with the right line sensor, the robot should turn right when it is over the black line (less infrared radiation reflected) and left when it is over a light surface (more infrared radiation reflected). Example code for this algorithm is shown below.

Exercise 3

Try tracking a line with the code above. Then try modifying the setMotors() commands. How fast can you track the line before you lose it? 

Note: As you run a Java program, the BlueBird Connector relays commands over Bluetooth to the Finch. There is some delay in this process, and that limits how fast you can track the line.

Exercise 4

Write a program that tracks a line as long as there is no obstacle in the Finch’s path. If there is an obstacle, the Finch should stop, but it should start tracking again when the obstacle is removed. The Finch should continue tracking and stopping for obstacles until button A is pressed.

micro:bit V2 Extra Challenge

If your Finch contains a micro:bit V2, you can also use a sound sensor in the micro:bit to measure the volume of sound around the Finch from 0 – 100. The getSound() method returns the int value of this sensor. If you don’t have a micro:bit V2 in your Finch, you will see an error when you use this method.

Write a program that uses the sound sensor to start line tracking when you clap. Another clap should then stop the line tracking.

Exercise 5

Turning left or right based on a single line sensor is the simplest version of a line tracking algorithm, but there are many variations. Write a program to implement the alternate line tracking algorithm described below. Which works better for your path?

  • If the left line sensor is over white and the right is over black, turn right.
  • If the right line sensor is over white and the left is over black, turn left.
  • If both line sensors are over the same color, move forward in a straight line.

By using both line sensors, the Finch can also detect places where a path branches. When the Finch reaches the branch point in a path like the one below, both of the line sensors will be over black. Use black electrical tape to create a path on a large sheet of white paper. The path should contain a branching point where the Finch will need to decide between two different paths.

Exercise 6

In this exercise, you will modify your code from Exercise 5 so that when the Finch reaches the branching point, it decides which branch to take. You will place an obstacle on one branch, and the robot should take the branch without an obstacle. Your robot must do the following:

  • Stop when it reaches the branch point (both line sensors detect black).
  • Turn to face one path, pause, and check for an obstacle. If there is no obstacle, the robot should move forward slightly (to get past the branch point) and then begin to track this path.
  • If there is an obstacle, the robot should turn toward the other path, move forward slightly (to get past the branch point), and begin to track this path.
Back to Top