Example Code for micro:bit-Conventional Line Tracing Program
Last revision 2025/12/19
The article offers code examples for programming a Maqueen V5 robot car with a micro:bit to perform line tracing using built-in sensors. It demonstrates basic line following techniques and reading sensor values to determine map suitability, providing practical insights into robotics programming.
Hardware Preparation
- Maqueen V5 Robot Car: 1 (Purchase Link: https://www.dfrobot.com/product-2937.html)
- micro:bit Mainboard: 1
- 3 AA Batteries: 1 set
- Line Tracing Map: 1
Software Preparation
- MakeCode Platform: https://makecode.microbit.org/
- Maqueen Library: Load via MakeCode Extensions (search for "dfrobot" and select "Maqueen")
Other Preparation Work
- Ensure the micro:bit mainboard is properly plugged into Maqueen V5.
- Install the batteries correctly.
- Turn on the power switch.
- Place the car on the line tracing map.
Sample Code
Example 1: Line Tracing Robot
Program Description:
Realize the logic of line tracing by reading the states of the 3 line tracing sensors.
Program Link: https://makecode.microbit.org/_7WzMXpWXv7th

Example 2: Read the Analog Quantities of the Line Tracing Sensor
Program Description:
The function of the analog quantity value can be used to read different gray values on the map, and it is possible to judge whether the map material is suitable for line tracing through the ADC value.
This program demonstrates placing the car on a white background and a black background respectively, and checking the difference in gray values between the two.
Read the value of the analog quantity of the line tracing sensor and display it on the computer.

Result
- Example 1: Car follows the line using the 3 line tracing sensors.
- Example 2: Analog values of the line tracing sensors are displayed on the computer.
Data Results:
It can be seen from the data that on the black background, the gray value is about 2250, and on the white background, the gray value is about 3000. The numerical difference is about more than 700. Through the difference in values, it is possible to determine whether the car is currently on the white background or the black background, thereby realizing the function of line tracing.
You can also try to read the gray values on different backgrounds to see what kind of gray characteristics and differences there are between them. At the same time, it is also possible to judge whether the map material is suitable for line tracing through the ADC value.


Was this article helpful?
