Two-Color Sensor PID Line Following Robot
by AfrelEdTech in Circuits > Robots
17 Views, 0 Favorites, 0 Comments
Two-Color Sensor PID Line Following Robot

Line following is one of the most common beginner challenges in robotics—but doing it smoothly and accurately is not always easy.
Many simple robots use a single color sensor to detect whether they are on or off the line. However, this can lead to jerky motion and poor handling around curves.
In this project, we use two color sensors—one on each side of the line—and apply PID control (Proportional, Integral, Derivative) in C to build a stable and responsive robot.
Supplies
- LEGO SPIKE Prime
- 2 × LEGO Color Sensors
- 2 × Motors (left and right wheels)
- 1 PC with Windows 10 or 11 operating system
Build the Robot
- Right Motor : Port A
- Left Motor : Port B
- Right Color Sensor : Port C
- Left Color Sensor : Port D
Build a simple differential drive robot with two motors and two color sensors mounted at the front, slightly apart, so they can detect both sides of the black line.
Try to keep the sensors at the same height. The optimal sensor distance may vary depending on your track’s line thickness—test and adjust!
Why Two Sensors? and What Is PID Control?
With two sensors, you can compute an "error" that tells how far off-center the robot is:
- If the right sensor sees more white (higher value), and the left sees black, the robot is drifting right → error is positive
- If the left sensor sees more white → drifting left → error is negative
This error value is used in the PID calculation.
PID stands for:
- P: Proportional — corrects based on how far off the robot is (the error)
- I: Integral — corrects small errors that accumulate over time (long-term drift)
- D: Derivative — reacts to sudden changes in error
Each part of PID helps in a different way to make the robot follow the line more smoothly.
Implement PID Control
We started by using only P and D, and set I = 0. This makes the robot more stable during initial tuning.
Full code:
Tuning the Gains — Especially Ki
Once P and D are tuned, try adding a small I (Integral) term to correct for long-term drift.
Be careful:
In C, the control loop runs very fast—much faster than in block-based environments. This means the integral value accumulates error at each cycle, and with such a fast loop, it can grow too large very quickly, leading to unstable or erratic motion.
That’s why Ki needs to be very small. In our case, we settled on:
Tune gradually, test frequently.
Speed Up the Robot
Once your PID values are stable, try increasing the robot’s speed.
But be aware:
Simply increasing the speed without adjusting the PID values may cause the robot to wobble or lose the line—especially in curves.
You may need to reduce the PID gains slightly to keep things stable at higher speeds. In our case, we settled on:
See It in Action

【Free Trial】Start C Language Programming With Afrel's Educational Materials
Are you intrigued by the idea of programming your SPIKE™ Prime with C? Did you know that Afrel is a key information provider for SPIKE™ Prime?
To meet the demand of those who want to "learn more about SPIKE™ Prime " and "try programming in C," Afrel has developed and sells educational materials for learning C language programming with SPIKE™ Prime.
Stepping up from Python to C is an excellent opportunity to further enhance your programming skills. Learning C will give you a deeper understanding of how computers work and enable more advanced control.
Afrel's C language materials are designed for beginners, ensuring a smooth learning experience. Why not take this chance to discover the joy of controlling SPIKE™ Prime with C and expand your programming horizons?
If you're ready to tackle C language programming, click the link below for more details.
Learn more about SPIKE™ Prime C Language Programming Materials here