Example Code for Matrix Laser Ranging Sensor
Last revision 2026/01/20
This article demonstrates how to use the Matrix Laser Ranging Sensor on Maqueen Plus V3 for accurate obstacle detection and autonomous behaviors with example code.
Introduction
The Maqueen Plus V3 comes standard with a Matrix Laser Ranging Sensor (8×8 LiDAR array), offering superior accuracy, speed, and stability compared to traditional ultrasonic sensors. It emits 64 laser beams within a 60-degree field of view, enabling precise distance measurement and spatial awareness of obstacles.
- Measurement unit: millimeters (mm)
- Origin: Upper-left corner of the sensor matrix (x=0, y=0)
- Forward direction: Sensor faces directly ahead when mounted
Installation Note:
When using rows x6 and x7, distances greater than 250 mm may cause the laser to hit the ground due to the mounting angle on Maqueen Plus V3.

Schematic of 8×8 laser point layout

Sensor mounting orientation on Maqueen Plus V3


Example 1: 1-Beam Laser Obstacle Avoidance
Introduction
This example uses a single laser point (x4, y6)—centered horizontally and positioned lower vertically—to detect obstacles up to 400 mm ahead. This configuration avoids ground interference within 0.4 m while effectively sensing low-height obstacles.
Note: The LiDAR requires ~3 seconds to initialize after power-on. The robot will remain stationary during this period.
Hardware Preparation
- Maqueen Plus V3
- micro:bit
- USB cable
Software Preparation
- MakeCode with Maqueen Plus V3 extension
Wiring Diagram
Fully integrated—no external wiring.
Sample Code
View on MakeCode

Logic:
- Read distance from laser point
x4, y6 - If distance < 400 mm → turn right
- Otherwise → move forward
Result

- Use lower y-values (e.g., y=0) for longer-range detection (>600 mm) but reduced sensitivity to low obstacles.
- Use higher y-values (e.g., y=6–7) for near-ground obstacle detection.
Example 2: 2-Beam Laser Obstacle Avoidance
Introduction
Two laser points—left (x0, y6) and right (x7, y6)—enable directional obstacle detection. The robot steers away from the side with the closer obstacle.
Sample Code
View on MakeCode

Logic:
- Read left and right distances
- If left < 400 mm and right ≥ 400 mm → turn right
- If right < 400 mm and left ≥ 400 mm → turn left
- If both clear → go straight
- If both blocked → reverse or stop
Result

Example 3: 3-Beam Laser Obstacle Avoidance
Introduction
Three laser points—left (x0, y6), center (x3, y6), and right (x7, y6)—provide comprehensive front coverage for intelligent path selection.
Sample Code
View on MakeCode

Logic:
- Prioritize center clearance
- If center blocked, choose the clearer side
- If all blocked → stop or back up
Result

Example 4: Cliff Recognition and Avoidance
Introduction
By angling the LiDAR downward (~60°), the robot can detect cliffs or table edges. It compares real-time distance to an initial ground reference and triggers avoidance if a drop is detected.
Setup
- Place robot on a raised platform (e.g., paper box >5 cm high)
- Tilt LiDAR bracket to ~60° downward
- On startup, record ground distance at x3, y3
- If current distance > (ground + 50 mm) → turn right
Sample Code
Result


Example 5: Intelligent Obstacle Avoidance Mode (Test Function)
Introduction
This built-in test mode uses multiple upper-row laser beams (e.g., y=0–2) to compute an optimized avoidance path. However, it has limitations:
Limitations:
- Struggles with low-height obstacles
- Max reliable range: ≤250 mm at default height (else ground interference occurs)
- Requires higher mounting or angle adjustment for longer-range use
Sample Code
Available Blocks


Note: This is a beta feature—behavior may vary.
Example 6: Line-Following with Obstacle Turnaround
Introduction
Combines line-following with obstacle detection: when an obstacle is detected ahead during tracking, the robot turns around and continues following the line in reverse—ideal for shuttling between two endpoints.
Scenario
Robot moves back and forth between two obstacles on a looped track.
Sample Code
Result

Example 7: USB Output of Matrix Laser Data
Introduction
The sensor can output full 8×8 distance matrix data via USB in real time (unit: mm), enabling advanced analysis or visualization on a PC.
Tool Preparation
- Windows 10+ PC
- USB-C cable
- Serial terminal software (e.g., SSCOM)
Usage Steps
- Connect Maqueen Plus V3 to PC via USB
- Open serial software, select COM port
- Set baud rate to 115200, enable DTR
- Matrix data streams continuously
Result


Experience Sharing & Tips
After experimenting with the above examples, consider these practical insights:
-
Y-axis controls vertical angle:
- y5–y7: Better for low obstacles (near ground)
- y0–y2: Better for long-range detection (less ground interference)
-
Adjust bracket angle:
- Lower angle → detect potholes or cliffs
- Higher angle → avoid ground reflection at long range
-
Multi-point fusion: Combine multiple beams for robust obstacle localization.
-
Intelligent mode: Explore built-in algorithms—they include basic path-planning logic.
-
USB data streaming: Use serial output to log or visualize the full 64-point depth map for custom applications or debugging.
Was this article helpful?


