Example Code for PID Control and Vehicle Speed Detection
Last revision 2026/01/20
This article demonstrates how Maqueen Plus V3 uses magnetic encoders for PID-controlled motion and real-time speed feedback, with example code for autonomous and sensor-driven behaviors.
Introduction
The Maqueen Plus V3 is equipped with magnetic induction encoders on both drive wheels, enabling precise measurement of real-time rotational speed and traveled distance. Leveraging this data, the robot supports closed-loop PID control for accurate motion execution—such as moving a specific distance or rotating a precise angle.
Key Features:
- Distance control accuracy: ±2 cm
- Angle control accuracy: ±5°
- Real-time speed output: in centimeters per second (cm/s) — functions like a digital speedometer
This capability enables reliable autonomous navigation, consistent turning, and performance monitoring—essential for competition robots or educational projects requiring repeatability.
Example 1: PID Control (Non-interruptible Mode)
Introduction
In non-interruptible mode, the program blocks further execution until the PID-controlled movement (distance or rotation) is fully completed. This simplifies logic but offers no mid-action flexibility.
Use Case: Simple sequences where actions must complete before proceeding (e.g., “move 50 cm, then turn 180°”).
Program Description
- Move forward 50 cm using PID distance control
- Turn 180° in place using PID angle control
Note: Due to mechanical tolerances, wheel slippage, and surface friction, the robot may not return exactly to the starting point. Repeated cycles will accumulate error.
Hardware Preparation
- Maqueen Plus V3
- micro:bit
- Flat, consistent surface for testing
Sample Code
Result
The robot executes the full forward-and-turn sequence without interruption, demonstrating basic closed-loop motion control.
Example 2: PID + Laser Ranging Stop (Interruptible Mode)
Introduction
Interruptible mode allows ongoing PID motion to be stopped early based on external conditions—in this case, obstacle detection from the Matrix Laser Ranging Sensor. A flag variable (x) ensures commands are only issued when the robot is not already in motion, preventing command conflicts.
Advantage: Enables responsive, sensor-driven behavior while maintaining PID precision when uninterrupted.
Program Description
- Start PID-controlled forward motion for 500 cm (effectively “drive until stopped”)
- Continuously check laser distance at x4, y6
- If obstacle < 20 cm → stop PID motion, set flag
x = 1 - When obstacle removed → resume forward motion, reset
x = 0
Critical Design Note:
Without the flag variable, re-triggering PID during active motion causes undefined behavior. The flag acts as a state guard.
Sample Code
Result
The robot halts immediately upon detecting a close obstacle and resumes when clear—ideal for dynamic environments.
Example 3: Read Real-Time Speed
Introduction
This example reads and outputs the instantaneous speed of each wheel via serial communication. Speed is reported in centimeters per second (cm/s), providing real-time feedback similar to a vehicle speedometer.
Program Description
- Continuously read left and right wheel speeds
- Print values to serial monitor every 100 ms
Sample Code
Result
Observed Speeds:
- Left wheel: 29.4 cm/s
- Right wheel: 14 cm/s

Application Ideas:
- Diagnose wheel imbalance or motor issues
- Implement speed synchronization for straight-line driving
- Log performance data for tuning PID parameters
Was this article helpful?



