5. case 03 Line Tracking

5.1. Our Goal


  • Make motor:bit smart car track lines.

5.2. Material


  • 1 x motor:bit smart car kit

5.3. Background Knowledge


Principle of Line Tracking

  • The line tracking module in this kit has adopted an infrared sensor. It consists of a transmitter and a receiver. The transmitter emits infrared light, while the receiver collects the infrared light reflected by the ground.

  • When contacting with black ground or other infrared light absorbing materials, the receiver can’t receive the infrared light, then the light tracking module returns to 0.

../../_images/UdHpeaS.jpg

5.4. Hardware Connection


Connect the left sensor(S1) on line tracking module to the 4th pin on motor:bit. And connect the right sensor(S2) to the 3rd pin. See picture below.

../../_images/BOpsVvF.jpg

../../_images/kzPngGo.jpg

5.5. Software


Microsoft MakeCode

  • If the line tracking module has detected a black line, its value will return to 0. If no black line detected, then it will return to 1.

5.6. Programming


Step 1

Click Advanced in the code drawer of MakeCode to see more options.

../../_images/LjMR5IU1.png

To program for motor:bit, we have to add a package. Find Add Package in the bottom of code drawer and click it. This will pop up a dialogue box. Search for “motorbit” and then click to download this package.

../../_images/XDlSfIS.png

Note: If you get a hint that some packages will be deleted due to the problem of incompatibility, you can either follow the prompts, or create a new project in the project menu.

Step 2

When start, set the motor speed to 60. At the same time, set a pull-up to both P3 and P4.

../../_images/Gsd06ub.png

Create two variable left and right to store the return value of the left or right infared sensor. Read the return value of the left or right infared sensor in digital way, i.e. digital read the return value of P3 and P4 port.

../../_images/sFbfJsT.png

If the right infrared sensor has detected derail, then set the speed of the left wheel to 30 and the right to 80. This will make the car turn left. Next, create a loop to check if the car has returned back to the black line. If not, the car will continue to turn left until it back to the track.

../../_images/bWmuV0v.png

That is same to the right wheel. If the left infrared sensor has detected a derail, then make the car turn right until it come back to the black line.

If both the left and right infrared sensor have detected derail, then set the motor speed to 60 and make the car continue to move forward.

../../_images/X10NnQL.png

5.7. Program


Link of the whole program: https://makecode.microbit.org/_XVADegLHX9dY

You can also download the program from the page below.

---

Note: Objects that can absorb infrared light can be regarded as black line.

5.8. Result


Motor:bit car moves along the scheduled black line.

5.9. Think


5.10. FAQ


5.11. Relative Readings