Tracking a Line

Approx. Time: 
90

A mobile robot is a robot that moves around its environment. It usually has wheels, though there are mobile robots that walk, fly, or swim. An autonomous mobile robot must have a way to navigate around the environment. One way that some robots navigate is to follow a line. This tutorial will explain how to use a Hummingbird LED and light sensor to make a mobile robot follow a line.

Since we are using this sensor with a mobile robot, we will want the robot to run in Arduino mode (not connected to the computer). You can use either the CREATE Lab Visual Programmer or ArduBlock/Arduino. We will demonstrate how to write a line tracking program in both the Visual Programmer and ArduBlock. To run in Arduino mode, you must have a Hummingbird Duo. If you have the original Hummingbird, you can use this tutorial with the CREATE Lab Visual Programmer and keep the robot tethered to the computer.

For this tutorial, you will need a wheeled robot that can move and turn. We used the rover shown below, but any wheeled rover should be fine. The robot shown below is made using parts fabricated with a 3D printer; further information is available in this tutorial. It has two gear motors that control the wheels, a distance sensor in the front to detect obstacles, and several LEDs. A battery pack is mounted underneath the robot to provide power to the robot.

  1. Making a Line Tracking Sensor
  2. Line Tracking with Visual Programmer
  3. Line Tracking with ArduBlock
  4. Creating Lines to Track

Making a Line Tracking Sensor

You will need a single color LED and a light sensor to create a line tracking sensor. To track a black line on a light-colored surface, you need to know whether the sensor is over a black surface or a white surface. You can determine this by measuring the amount of light reflected by the surface. A white (or light-colored) surface reflects a lot of light, while a black surface reflects very little light.

To create a light sensor, first wrap electrical tape around the sides of the LED and the light sensor. This will help to ensure that the light must reflect off the surface before it reaches the sensor. Be sure not to cover the end of the LED or sensor! Next, tape the two components together to fix the position of the sensor relative to the LED. Connect the LED and light sensor to your Hummingbird board.

Mount the line tracking sensor to the front of your rover. We have placed ours slightly to one side of the distance sensor. Make sure that the sensor is pointed directly downward and is very close to the ground (but not touching it).

Line Tracking with Visual Programmer

To begin, create three expressions. One expression (TrackingLEDon) should turn the LED in the line tracking sensor fully on. The second expression should make the robot turn left, and the third expression should make the robot turn right. Our turnLeft expression is shown below.

Tip: Turn at a slow speed. A robot that is moving quickly will lose the line more easily.

Next, create a sequence (trackLine) that starts with the expression that turns the sensor’s LED on. Next, use a sensor block to test the output from the light sensor. To track the left side of a black line, the robot should turn left when it is over the black line (less light reflected) and right when it is over a light surface (more light reflected). You may need to adjust the threshold of the light sensor to distinguish the line from the light surface.

If you want to track the line only if there is no obstacle in the way, you can place the trackLine sequence within a sensor block for the distance sensor. This sequence also includes a stopRover expression that sets both motors to 0 when an object is near the sensor.

Note: If you place the trackLine sequence within another sequence, you should change the mode at the bottom of each track of the light sensor block to plays once (down arrow) instead of loops (circular arrow).

Once you are happy with your program, export to Arduino to disconnect the rover from the computer! For more information, see here. Then skip down to Creating Lines to Track to test your robot.

Line Tracking with ArduBlock

Start by removing the loop block that appears by default when you open a new ArduBlock file. Instead, you need to use a program block for this project. This block is shown below; you can find it in the Control menu.

The main part of an Arduino program is a forever loop that runs continually on the microcontroller until it is turned off or a new program is uploaded. Sometimes, you need to execute a few commands before entering the forever loop. For instance, here we need to turn on the line tracking LED before entering the forever loop. In this situation, the program block enables you to specify commands in the setup portion that will only be executed when the program starts. The loop portion of the block contains the commands to be repeated in a forever loop. Place an LED block in the setup portion to turn on the line tracking LED.

The main loop of this program will decide whether the line tracking sensor is over a black line or a light surface. To do this, we need to find a threshold for the light sensor. Start by printing the value of the light sensor to your computer screen. You can do this by placing a serial println block in the loop portion of the program block. This block is found on the Communication menu. You will also need the glue block to turn the SENSOR block into a string. 

After you upload the program, click on the button labeled “Serial Monitor” at the top of the ArduBlock screen. This will open a window like the one shown below. The value of the light sensor is printed to this window. Examine the light sensor reading over the black line and over the light surface. Choose a threshold that is roughly halfway between these values.

To track the left side of a black line, the robot should turn left when it is over the black line (less light reflected) and right when it is over a light surface (more light reflected). To code this decision, use an if/else block in the loop section of the program block. The test for this block is whether the light sensor reading is less than the threshold, which is 265 in the code shown below. If the value of the light sensor is less than the threshold (over the black line), two MOTOR blocks turn the robot to the left. Otherwise, two different MOTOR blocks turn the robot to the right.

Tip: Turn at a slow speed. A robot that is moving quickly will lose the line more easily.

If you want to track the line only if there is no obstacle in the way, you can nest the if/else block shown above within an if/else block that uses the distance sensor to check whether an object is near the robot. If there is an obstacle, the robot should stop; otherwise, it should track the line.

After you have uploaded your final program to the microcontroller, you can disconnect the Hummingbird from the computer. Now you are ready to experiment with tracking different lines!

Creating Lines to Track

You can use a thick black marker to draw a line on a large sheet of white or light-colored paper. Make sure that the line is at least ½” thick; it is very hard to track a thin line. You can also use black electrical tape to create a line for the robot to track. An example is shown below. Start with a line with gradual curves because it is harder to track sharp curves. Experiment with different lines to see what you can track! How can you modify your program to change how the robot tracks the line?