Example Code for Regular Line Tracking Program
Last revision 2026/01/20
The article showcases example codes for Maqueen Plus V3's line tracking program using digital and analog sensors, explaining hardware and software setup, logic overview, and result interpretation for precise robot navigation.
Introduction
The Maqueen Plus V3 is equipped with 5 onboard line tracking sensors. Each sensor outputs a digital value:
- 1 when detecting a black line
- 0 when over a light (e.g., white) surface
This binary response enables precise line-following behavior by analyzing the pattern across multiple sensors. The following examples demonstrate both digital state-based tracking and analog value reading for advanced calibration and control.
Example 1: Line Tracking Robot
Introduction
This example uses the states of 3 central line tracking sensors (typically sensors 1, 2, and 3) to implement a basic line-following algorithm. Based on which sensors detect the black line, the robot adjusts its direction to stay centered on the track.
Hardware Preparation
- Maqueen Plus V3
- Printed black-line track on a white background
- micro:bit
- USB cable
Software Preparation
- MakeCode Editor
- Maqueen Plus V3 extension installed
Wiring Diagram
All sensors are integrated onboard—no external wiring required.
Sample Code
View on MakeCode

Logic Overview:
- Read digital states from left, center, and right line sensors
- If center sensor detects line → go straight
- If left sensor detects line → turn left
- If right sensor detects line → turn right
- If none or all detect line → stop or adjust accordingly
Result
The robot successfully follows a black line by continuously adjusting its direction based on real-time sensor feedback.
Example 2: Reading Analog Values of Line Tracking Sensors
Introduction
While digital mode gives binary (on/off) readings, the line sensors also provide analog output (0–4095), reflecting light reflectivity more precisely. This example reads analog values and sends them to the computer via serial communication, allowing users to observe the difference between black and white surfaces and calibrate thresholds for custom logic.
Hardware Preparation
- Maqueen Plus V3
- micro:bit
- USB cable (for serial data transmission)
Software Preparation
- MakeCode Editor with Maqueen Plus V3 extension
- Serial monitor (built into MakeCode or external tool like PuTTY)
Wiring Diagram
All components are onboard; connect micro:bit to computer via USB for data output.
Sample Code
Key Features:
- Reads analog values from line tracking sensors
- Sends data to serial output every 100 ms
- Enables observation of raw sensor response under different surface conditions
Result
Reading Interpretation:
- On white background: analog value ≈ 3800
- On black line: analog value ≈ 2700
- Difference: ~1100 units
This clear contrast allows reliable distinction between black and white surfaces. The analog data can be used to implement smoother or adaptive line-following algorithms.

Was this article helpful?

