Introducing the Line Following Sensor
The line following sensor allows the mBot to detect edges and navigate along a thick black line (or a white surface). The sensor is comprised of two Infrared reflectance sensors that detect the reflectance of a surface near them. Each sensor is made up of an infrared transmitter to produce a beam of infrared light, and an infrared light detector mounted beside it to detect that same light if it reflects off a surface.
So, if one of the Infrared reflectance sensors is over a black surface there will be very little light bounced back into the infrared light detector which will report a zero value. If the Infrared reflectance sensor is over a white surface there will be a great deal of light bounced back into the infrared light detector which will report a positive number. Having two sets of these infrared reflectance sensors means the line detection sensor can determine;
- when both infrared reflectance sensors are over a black surface (or over an edge),
- when both infrared reflectance sensors are over a white or light coloured surface with some reflectance, and
- when one or other of the infrared reflectance sensors are over a black surface and the other over a light coloured surface – i.e. the edge of a line.
Here is what the line following sensor reports for the different conditions.
To use the line following sensor in our programming we just use this block.
The block is designed to fit into the standard Scratch Operator and Control blocks. The line follower block will output a number from 0 to 3 based on the state of the two sensors as shown above. So we can use this output where a number between 0 and 3 can influence particular events. Here are some examples.
In this case the output from the line following sensor will influence how many times the loop will run.
For instance if the line sensor is fully over a black line it will not do anything within the loop because the output from the sensor is 0.
In the case of the line sensor not being over the line, it will repeat the actions in the loop 3 times.
In this case the mBot will do the actions contained with the if block if the line following sensor is over a black line (or dark surface) or where the left side infrared sensor (sensor 1) is over something dark but the right side sensor (sensor2) is over something white(ish).
You can use the output from the line follower in mathematical operations.
In this case the output is being added to the number 4 to control the number of times a loop is executed. The loop will run between 4 and 7 times depending on whether the mBot line sensor is over a dark line or not.
Following a Line
Let’s recreate the line following behaviour of the default program. The program requires the following decisions to be made by the mBot.
- If the sensor has both Infrared Sensors over a dark surface, then it is on track and can run forward.
- If the sensor has only got the left infrared sensor over the line then it will need to turn left to get back onto the line.
- If the sensor has only got the right infrared sensor over the line then it will need to turn right to get back on track.
- If both infrared sensors are over a light coloured surface then it is off the line and will need to search for the line.
Logic for Line Following
Here is the flowchart of the logic being used.
Here is the Scratch program to do this.
You will notice we are using Less-Than operations rather than Equal To operations. While this is not important for this example, it is better practice because it allows for small errors in the sensor output data being compared against. For instance if the line follower sensor were to output a value of “0.999993” or “1.00002” this is not the same as “1” as far as the controller is concerned and so using a block like the following would not trigger the right actions even if that was what we wanted.
By using Greater-Than and Less-Than conditions makes our robot better able to handle the non-perfectness of the real world. After all Less-Than 2 is a lot bigger target than getting exactly Equal To 1. So the equivalent block that can handle a sensor output value of “0.999993” or “1.00002” would be:
For sensors that output finer resolution numbers we might set the comparison number to “1.3” say rather than “2”, but because our line Following Sensor outputs whole numbers we use the “<2” to pick up an output of “1”.
OK, now that we have a line following program in place, try out different paths for the mBot and see what is does at junctions. Use bits of black paper to make a course. If you create a curved path, how sharply can it turn before it gets confused?
If your mBot is lost and needs to find the line again create a method to help it search for the line. Try out different methods.
This document was adapted from “Building an mBot Program” by Hamish Trolove and is provided under a creative commons license – Attribution, Share Alike.