PID Line Tracker
As part of the Mechatronics Design 1 Class from June to August 2024, I designed, built, and programmed a Lego Mindstorms robot that autonomously follows a line while detecting and navigating around obstacles. The project focused on implementing a PID controller to achieve precise and efficient line tracking, allowing the robot to adapt seamlessly to various course layouts. By integrating a color sensor for line detection and an ultrasonic sensor for obstacle avoidance, the robot was able to maintain smooth and accurate navigation, even in complex environments.
The robot’s logic was developed using both RobotC and Micropython, a class requirement by making the RobotC version without PID and then remaking the script in Python with PID. The PID controller played a crucial role in optimizing the robot’s performance, enabling it to adjust its motor speeds dynamically based on real-time feedback from the color sensor. When the sensor detected deviations from the line, the robot would correct its trajectory by adjusting the speed of its left and right motors, ensuring it stayed on course. Additionally, the ultrasonic sensor allowed the robot to detect obstacles and respond intelligently; either by stopping and removing obstacles on green lines or executing a 180-degree turn on blue lines to continue its path.
Rather than having the colour sensor track the center of the line. I had it track the right edge of the line. Having the left half of the sensor's view be colored and the right half be the ground reading. If the sensor read an RGB value too close to the pre-set RGB value of the ground, the right motor would spin quicker. If the sensor read too much of a green or blue RGB combination (the colors of the tape the robot is meant to follow) that means the robot was drifting left. Therefore the left motor would increase in speed.
[RobotC video on left w/o PID, Micropython video on right w/ PID]


The robot achieved the fastest completion time in the class for the line-following course, earning an A+ for its precision and adaptability. The robot’s ability to handle sharp 30-degree turns, avoid obstacles, and maintain consistent speed demonstrated the effectiveness of the PID tuning and sensor integration.
RobotC code: https://drive.google.com/file/d/1tLUIkST5p8ORrYxxLS0JtyjFHLwdw1p4/view?usp=sharing
Micropython code: https://drive.google.com/file/d/1ogpaGrvg2lX-Uznf0Di9-krZfr-1LqUc/view?usp=sharing