Line Following

Learn how to get Sparki to follow a black line on white paper



Sparki can use the sensors below the hands to sense edges and lines. In this tutorial you’ll have Sparki follow a line.

What You’ll Need


Note: If you make your own line path and the lines aren’t dark enough, or the material is too reflective in general, Sparki may become confused. Try something with more contrast and less reflectivity, or change the threshold in the code.


How It Works

Sparki has an array of infrared sensors underneath that it uses to detect if there are lines below it. We’re going to program Sparki to follow the dark lines in our path, since they are printed over a more reflective background (typically, the path is printed in dark black, over a white background).

Infrared reflectance sensors can be thought of as an infrared light (which humans can not see), and some special eyes that look at that light. The eyes are made out of a special photo sensible material and an infrared emitter, typically a LED (or Light Emitting Diode):

Infrared Sensor Diagram

Here are the parts again, but with pictures showing what you’d normally think of them as!

Infrared Sensor Diagram  

Converting the Sparki in a line following robot

Following a Line

So, let’s use these sensors to make the Sparki to follow printed lines in the floor. Warning_Triangle 

Please remember to check that the batteries are properly connected (and charged!). And as we are going to use the motors here, please check that the On/Off Switch is on. Another important thing to take care of when playing with the robot’s motors is to be careful not to be working over a table. A fall from that table could permanently damage your Sparki.

In this example code, Sparki continues to move forward as long as it detects a line underneath the center sensor. If there is no line underneath it, it will look under the left and right line sensors to see if it has started to move off course. If there is a line underneath the left sensor, it will move left to move the robot back to the line (by accelerating the right wheel, as explained in this previous lesson). If there is a line underneath the right sensor, it will move right to move the robot back to the line.

The following image shows the possible states of these sensors, as seen by our program. The black dot means that the sensor is reading a line below it, and a white dot means that the white background is below the sensor. With the green dots we represented that the value for that “greened” sensor does not really care, since the state of the robot is really determined by the other two sensors:

Here is the complete miniBloq code that makes Sparki move according to its sensor states, as shown in the previous image:

Congrats! You have programmed Sparki to follow a line!

Just a note about the blocks used to rotate and move the robot forward: as you can see in the code above, they receive a 0 (zero) number as the parameter. This means that they will rotate or move indefinitely until they receive a new moving command. So, if we have Sparki rotate left with its degrees parameter set at 0, it will continue rotating in that direction until a move forward, backward or rotate right block is executed in the program.


Extra Activities

  • Of course, there are other techniques to follow lines with a robot, such as with two sensors instead of three, or even with one! Why not take a look out there in the Internet to see if you can find new ways of doing this?
  • If the line forks, which way will Sparki turn? Is there a way you can give it directions with the remote as it comes to a split in the road? Are there ways you could have Sparki tell you when it has reached a branching path?
  • What about following a line that is wider than the distance between the left and the right sensor?