Configuring Line Sensors on Your Robot

This blog post on line sensors contains excerpts from Lesson B-12 and Lesson D-13 of our Intro to Robotics program. Intro to Robotics consists of four levels that start at the beginning and systematically teach you to build circuits, use a Raspberry Pi, and write code in Python. In the final level, you will take all the skills you've learned and build a mobile robot of your own. The full Intro to Robotics course contains 72 detailed lessons including 225+ videos and 200+ projects and activities. Sample lessons and  full scope and sequences for all four levels can be found here.

 

Robot following line course

 

What Are Infrared Line Sensors?

An infrared line sensor can be used to detect the difference between dark colors and light colors using infrared waves. This can allow a robot to perform an activity referred to as “line following” where it can follow a black line, often created by using black electrical tape, on any light-colored surface. The robot can follow the path created by the tape without any intervention from the user, using only the IR line sensor input and a Python program.

 

Alignment of Infrared Line Sensors

Your robot could follow a black line using only one IR line sensor. The sensor would need to be positioned directly above the line, and your robot would have to be programmed to drive one motor at a time. If white was sensed by the line sensor, the current motor has driven you off the line, and the other motor needs to drive until you are back on the line.

While this would technically work, your robot would have to move extremely slowly to avoid getting away from the line too quickly and getting lost. Adding a second sensor allows you to watch both sensors, and if either sees black, then turn away from the line. This allows you to sense a larger area, so your robot can follow the line faster without getting lost.

Image dipicting two line sensors on either side of a black line 

Using two sensors is the most cost-effective way to make a robot reliably follow a line but continuing to add sensors can enhance the robot’s ability to follow a line quickly.

 

How Do You Configure and Program Motors to Allow Line Following?

The program to control a line following robot can be as simple as:

  • Drive both motors forward
  • When the left sensor detects the line then stop the left motor
  • When the right sensor detects the line then stop the right motor

By stopping the motor on the same side that sensed the line, the motor on the opposite side will continue to drive, pushing the front of the robot back to the center of the line. This same behavior happens over and over, and the robot will follow the line. The only problem with this behavior is that the robot will only drive in half circles to follow the line so it’s behavior may appear slightly erratic:

Erratic line following behavior by robot

The red lines above have been used to illustrate the path the robot’s wheels will take when driving only one motor at a time. You can correct for this slightly erratic behavior by accounting for a number of factors in robot construction and code including speed of the robot, tightness of the turns in the course to be followed, thickness of the line, distance between sensors, distance between drive wheels to sensors, and motor response time.

 

Creating a Course

The infrared line sensors are looking for reflections from the floor beneath the sensor, so the type of material used for the line will depend on your floor. If your floor is light colored, then black tape will likely work best as there will be good contrast for the line sensors between the floor and the line. If you have a very dark floor, then black tape might not give much contrast at all. In that case, white tape on the dark floor would give your line the best contrast given the surface.

Line width can seriously impact your robot’s ability to follow the line. If the line is too narrow, the robot can easily drive right over the line before the robot can take action to stay on the line. If the line is too wide, then the edges of the tape could be very close to both of your line sensors, resulting in the motors constantly switching back and forth to stay on the line:

Image depicting lines that are too wide, too narrow, and correct width for a line following robot

 

When laying out the course, keep turns larger than about a 6” radius, which is about the distance from the outside of one wheel to the outside of the other wheel. Tighter turns can cause the robot to drive over the tape before making the corrections for the turn:

Image shows the amount of curve a line can have for a line following robot

 

Make sure to keep the tape lines at least 4” apart. Closer together than 4” and you risk the robot seeing another part of the course, which would negatively affect it’s behavior on the current line. Also, do not allow the tape to cross in order to make figure-eight courses or other similar patterns. 

 Don't put course lines too close together or robot will get confused

 

Speed and Motor Drive Characteristics Matter

The speed of your robot is an important factor in line following performance. Too fast and the robot will drive over the line before it has a chance to make drive motor adjustments. Too slow and the drive motors may stall or become unable to move at the low speed you would like. The perfect speed will be somewhere in the middle, but it will likely take a little experimentation to find the best line following speed for your robot.

Luckily, through the use of PWM control of the enable line for each drive motor, you can very accurately control the speed of each motor. This will allow you to fine-tune the motors speed to determine the fastest speed that your robot can accurately follow the course that you have set up.

Another factor to keep in mind when trying to automate the control of a robot is coasting. Coasting is when a motor is being driven, the drive signal is turned off, but the robot continues to roll to a stop. The amount of coasting that occurs can be dependent on many factors, but the main factor is the weight of your robot. A heavier robot, once it’s moving in a certain direction, will try to continue moving in that direction, even once the motor drive signals have been removed. You may not think your robot is all that heavy, but the weight of the batteries and other electronics are affecting how quickly your robot will stop one the drive signal to a motor is removed.

 

Coasting Behavior

Coasting can negatively affect line following performance, since stopping the drive motor on the sensor side when the line is sensed is a key component to not overshooting the line. If that side continues to roll even after the drive signal is removed, then both sensors may travel over the line, and the robot will be unable to reacquire the line:

Robot coasting off line

There are different ways to lessen this coasting effect, but one simple software solution is to drive the motor backwards for a split second instead of allowing the motor to coast. This will rapidly bring the motor to a stop, but you have to be careful of the additional load this puts on the motor drive electronics. This is not an issue when using the small motors from your kit, but as motors get larger and more power is required to drive them, this option will become less feasible in those cases.

Line following robot

In the activities for Lesson D-13, we walk you through step-by-step as you configure the line sensors, motor, and write the Python code needed to account for the various factors and successfully have your robot follow a line course.

This blog post on line sensors contains excerpts from Lesson B-12 and Lesson D-13 of our Intro to Robotics program. Intro to Robotics consists of four levels that start at the beginning and systematically teach you to build circuits, use a Raspberry Pi, and write code in Python. In the final level, you will take all the skills you've learned and build a mobile robot of your own. The full Intro to Robotics course contains 72 detailed lessons including 225+ videos and 200+ projects and activities. Sample lessons and  full scope and sequences for all four levels can be found here.

Older Post
Newer Post
Close (esc)

Popup

Use this popup to embed a mailing list sign up form. Alternatively use it as a simple call to action with a link to a product or a page.

Age verification

By clicking enter you are verifying that you are old enough to consume alcohol.

Search

Your cart is currently empty.
Shop now