#Robotics Programming a Spike Prime Line Following Robot – Part 3

At the end of the second article in this series, I mentioned some additional line following methods. In this article I will explain the theory and initial code to get a Lego Spike Prime robot following a line.
Disclaimer: This article will use Robocup Junior Australia‘s Rescue (Line) Challenge with a Lego Spike Prime robot as an example.
If you have not read the previous articles, please do so before continuing:
- #Robotics Programming a Line Following Robot – Part 1
- #Robotics Programming a Line Following Robot – Part 2
This article is based on a Spike Prime robot with two colour sensors in ports A and B and two motors in ports C and D. You will also need an Ultrasonic Sensor and Touch Sensor (for Primary) or additional motor (for Secondary claw mechanism). It is recommended to have 3 stud distance between the colour sensors to make the robot less reactive and jittery. An example design is shown in the article below:
Read on for the steps to get your robot following a line.
The Theory
Let’s make an assumption that black has a reading of 0 and white has a reading of 100. We can then create a differential value for a left and right colour sensor by subtracting one sensor’s reflection value from the other sensor.
Example 1: Both Sensors on White
Differential = Left Sensor – Right Sensor = 100 – 100 = 0
Example 2: Left Sensor on Black and Right Sensor on White
Differential = Left Sensor – Right Sensor = 0 – 100 = -100
Example 3: Left Sensor on White and Right Sensor on Black
Differential = Left Sensor – Right Sensor = 100 – 0 = 100
This Differential value can then be used to control the steering of the robot as it provides a value from -100 (left) through 0 (straight) to 100 (right).
In reality, the sensor values are not 0 and 100 and the sensor values will slowly change value as they see more black and less white as the robot starts seeing the line. If, in reality, black is around 40 and white is 90, then the range of the differential will be -50 to 0 to 50. If we multiply the differential by a fixed turn factor we can control how sensitive the robot is. A higher factor will make the robot more sensitive but jittery and jerky. A lower factor will smooth out the turns, but might not allow the robot to make a sharp turn. A good starting point is a value of 2. Testing can then see whether it should be changed.
The Program
I have seen programs created with lots of Events blocks and want to highly recommend against this approach. The issue is two fold; having lots of events divides the processing power of the controller by splitting the program into multiple flows all running at the same time, and you have no control as events could fire in the middle of other code you are running. Having a single When Program Starts event is all that is needed to create a single program flow that is easier to write and debug.
After the program starts you first want to define settings and initialize variables, then create a main loop for your program. In that loop you can then create My Blocks for each of the different challenges: Line Follow, Shortcuts, Obstacles, and Rescue.
For the Line Follow all you need is to have the Differential multiplied by the Turn Factor to get the Steering value.
Steer = ((Left Sensor – Right Sensor) * Turn Factor)
Putting that all together gets you this program as a starting point:
You can experiment with speed and Turn Factor to get the best results for your robot. But remember that reliability is more important than speed. The faster you go, the higher the chance of losing the line.
Also, as you add more code for the other My Blocks, the line follow will get less sensitive, and you might need to slow down the robot’s speed.
Note: If the robot goes backwards, change the sign on the speed setting. If the robot turns away from the line rather than follow the line, swap the ports on the colour sensors to B – A.
Next Steps
The next steps will be to code the My Blocks to look for and then handle the intersection shortcut markers, water tower or obstacles and then the Rescue Zone. See the other articles on the portal to help you.
Note: Make sure you turn on the More Sensors option as you will need to use the Raw Red colour sensor mode to be able to differentiate between white and silver.
More Information
For more information on robotics, check out the following links:
- David’s Robotics Portal
- #Robotics Handling intersection shortcut markers
- #Robotics Handling water tower or obstacles
- #Robotics Finding objects with an Ultrasonic sensor
- #Robotics Line Rescue Completing the rescue
- #Robotics Demo Video and Hints: Primary and Secondary Line Rescue
#Robotics Identifying Silver with Lego Spike Prime - #Robotics Better Colour Detection by Converting RGB to HSI Values
- Robocup Junior Australia – Rescue Line Resources
Hope this information is useful.
David
29-Jun-2026: Added links to article on enabling “More Sensors” for Spike Prime.
This article was originally posted on https://www.winthropdc.com/blog.

