Test the QTI Sensors Using Micro:bit’s LEDs
You will be adding a second module to your micro:bit project for the QTI
Hardware Setup
- Set the cyber:bot board's power (PWR) switch to Position 0.
- Make sure the battery holder is loaded with 5 AA batteries.
- Make sure the battery holder's barrel plug is firmly plugged into the cyber:bot board's barrel jack.
- Connect your micro:bit module to your computer with a USB cable.
Software Setup
- In a Google Chrome or Microsoft Edge browser, go to python.microbit.org to open the micro:bit Python Editor.
- Add three modules to the project: cyberbot.py, qti.py, and intbits.py.
- Go to Add modules to your micro:bit.
- Skip the Quick Start section, and instead start at the section titled Adding a Module to micro:bit Filesystem.
- Watch the video and then add the cyberbot.py module to the project by following the instructions from there to the end of the page.
- Next, follow the same steps to add the qti.py module to the project. It will be in the same folder with cyberbot.py.
- Repeat the steps one more time to add the qti.py module to the project. It will also be in the same folder with cyberbot.py.
Script: QTI_Display_Detections
- Set the project's name to QTI_Display_Detections, enter the script below, and then click Save.
(See Save & Edit Scripts and Flash Scripts with Python Editor.) - Click Send to micro:bit.
(See Flash Scripts with Python Editor.)
# QTI_Display_Detections # Test program uses micro:bit LEDs to display QTI states from cyberbot import * # import modules from qti import * from intbits import * # cyberbot library 0.7.0 or later bot(22).tone(500, 1000) # start beep while True: # main loop pattern = qti(7, 4).read() # store 1/0 detect states in pattern for x in range(0,4): # loop 4x count LED columns with x z = bit.get(pattern, x) # get 1s or 0s from pattern for y in range (0,5): # use y to visit each row display.set_pixel(x, y, z*9) # LED on 9 or off 0 in x-col y-row
- Set the cyber:bot board's PWR switch to position 2.
- Position your robot over the black line on your track. The QTI sensors directly over the line will cause the corresponding column of micro:bit LEDs to illuminate.
- Move the robot around slightly to test each QTI sensor Place each QTI sensor over black and then white to verify that the LEDs turn on and off, as shown in the video below.
If the sensors don't see the line, try the following adjustments:
- Double-check all of your wiring connections to make sure the sensors are properly connected to power, ground, and each I/O pin.
- Try replacing the jumper wire connecting to 5 V with a 220 ohm resistor. This makes the QTIs less bright, which might be overwhelming with a shiny line or shiny paper.
- Try removing the nylon spacers from one of the sensor's posts. If this improves performance, remove it from the rest of the posts.
IMPORTANT: Do not continue until the 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 5x5 LED display jumps by one column when the black stripe is moved from one QTI to the next. If it insead 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.
- Set the PWR switch to 0 until you are ready to rerun this script, or until you are ready to run the next script.
How It Works - Display QTI States with LEDs
This statement copies line detection information for all four QTI sensors into a single int variable:
pattern = qti(7, 4).read()
The qti(7, 4).read() 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.
for x in range(0,4): # loop 4 times through qti value z = qti.check_bit(pattern, x) # qti.check_bit is 1 or 0 for each x index
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:
for y in range (0,5): # set each x column of LEDs on when z is 1 display.set_pixel(x, y, z*9)
The third argument in display.set_pixel(x, y, z*9) 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 display.set_pixel 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.