PID Line Following at the Crossroad | SPIKE Prime in C
by AfrelEdTech in Circuits > Robots
71 Views, 0 Favorites, 0 Comments
PID Line Following at the Crossroad | SPIKE Prime in C

In my previous article, I tuned a PID-controlled line follower to handle sharp corners on a square course using LEGO SPIKE Prime and C. This time, I challenged the robot to handle a cross-shaped (plus-shaped) line course, which includes intersections requiring decision-making—such as turning.
We used the same robot as in the last article, with left and right motors and dual color sensors.
Supplies
- LEGO SPIKE Prime
- 2 × LEGO Color Sensors
- 2 × Motors (left and right wheels)
- 1 PC with Windows 10 or 11 operating system
Go Straight Through the Intersection
First, I tested the original PID line follower program without any changes.
The result: It handled straight crossings without any problem, continuing to follow the line even across the intersection.
This showed that the basic PID control is stable enough to pass through an intersection when no turn is needed.
Detect and Stop at the Intersection
Next, I tried to make the robot turn right at the intersection.
The first step was getting the robot to recognize the intersection and stop.
I modified the program so that when both left and right color sensors detect black, the robot treats it as a crossroads and stops.
This worked well—the robot consistently stopped at the correct spot.
Turn Right From the Stop
After stopping, I added code for the robot to rotate in place to the right.
Since the color sensors are placed in front of the wheels, I had to adjust the timing and rotation to align the robot properly after the turn.
With a little fine-tuning, the robot was able to turn right smoothly and rejoin the line on the new path.
Demo Video and Code

Conclusion
This experiment adds basic intersection handling to our line follower, showing how C programming on SPIKE Prime can be extended step-by-step for more complex tasks—useful for robotics competitions or education alike.
【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