This tutorial shows you how to add an array of four QTI sensors to your cyber:bot. These sensors can differentiate between dark and light surfaces. With a Block program, your cyber:bot robot can use the QTI sensors to follow a black electrical tape line on a light background. This tutorial provides a basic program you can use on a simple track with the cyber:bot’s standard Continuous Rotation Servos. The same code example could also be used with the High Speed Continuous Rotation Servo.
This project assumes you already have some experience with the cyber:bot tutorial series. At a minimum, work through these first:
Track Options
You will be ready to combine QTI line following with other sensors for more advanced projects which can be done individually or in the classroom. Some of the ideas include:
Each QTI sensor is connected to a post that is mounted on the underside of the chassis. This positions the sensors right above the ground surface.
Each QTI sensor needs to connect to 5 V power, a cyber:bot I/O pin, and ground. For this, you will use the four 3-pin headers from the kit to build ports on the breadboard for each sensor.
You will be adding a second module to your micro:bit project for the QTI
If the sensors don’t see the line, try the following adjustments:
IMPORTANT: Do not continue until all four QTIs are working properly as shown above. This might take some troubleshooting of errors made while connecting the QTIs as well as some adjusting of the spacers. It will also be important that the 5×5 LED display jumps by one column when the black stripe is moved from one QTI to the next. If it instead skips back and forth, it might mean that the QTI cables are swapped -not plugged into the correct QTIs. Set the cyber:bot board’s PWR switch to 0 if you adjust the circuit.
This statement copies line detection information for all four QTI sensors into a single int variable:
The qti read start (Pin7) end (Pin4) call returns the states of QTI sensors connected to sockets P7 through P4. These states are stored as binary 1 and 0 digits in the int variable named pattern. If a digit is 1, it indicates a black or non-reflective surface. If it’s 0, it indicates a white or reflective surface.
Suppose the two middle QTI sensors are over a black line so that they are both 1’s. The pattern variable’s value would be 0b0110.
After storing the QTI states, a loop uses the qti module’s check_bit method to successively check binary digits in pattern and store them in the z variable. The for x in range(0, 4) loop starts at the rightmost binary digit in pattern and works its way left by increasing the x variable’s value with each repetition: 0, 1, 2, 3. As an argument in the check_bit method, x selects which binary digit in pattern to return, and that binary 1/0 return value gets stored in the z int variable.
With the example 0b0110, this is how z evaluates at each loop repetition:
A nested loop turns all micro:bit LEDs in certain columns on or off to indicate which QTIs are over a black or white surface:
The third argument in plot x (x) y (y) brightness (z * 255) sets the brightness (from 0 to 9). If z is 0, then the brightness is z * 9 = 0 (off). If z is 1 then the brightness of z * 9 = 9 (max brightness). Instead of just one LED, the for y in range (0,5) loop makes plot set all LEDs in a given x column to on or off in response to a z value of 1 or 0. For more info on display, try the examples in the LED Matrix tutorial.
How many possible combinations of QTI sensor states exist from pattern? The values could range from 0b0000 (0) to 0b1111 (8+4+2+1=15) for a total of 16 different number combinations!
Is it likely that pattern would be 0b1001 where the two outside QTI sensors are seeing black? Not likely since they’re 1-½” apart and electrical tape is less than ¾” wide. Rather than check for every possible 16 cases, choose the ones which are most likely. This saves memory and creates a faster-running program. The example program has eight possibilities shown and described in the following 8 images.
0b1000, Sharp left turn
0b1100, Medium left turn
0b0100, Gentle left turn
0b0110, Straight ahead full speed
0b0010, Gentle right turn
0b0011, Medium right turn
0b0001, Sharp right turn
0b0000, Backup, turn, rotate, make a sound – or do the same command previously executed?
The cyber:bot Forward and Backward tutorial showed how the right motor must turn the opposite direction for the robot to go forward, using negative values. For example:
Note that the example QTI_Follow_Line code uses all positive values. How is that possible? At the end of the program, the wR values are made opposite, allowing the use of all positive numbers as arguments.
This makes it a little bit easier to come up with drive speed values in the example program. The values work well with a gentle, curvy line formed with electrical tape. The combinations of motor speeds you use will vary based on the line you create.
If you are competing in a line following contest, you can speed up your cyber:bot two different ways.