LEGO SPIKE Prime Finding the Best Color Sensor Position for Line Following Robots

by AfrelEdTech in Circuits > Robots

63 Views, 0 Favorites, 0 Comments

LEGO SPIKE Prime Finding the Best Color Sensor Position for Line Following Robots

スクリーンショット 2025-07-11 144327.png

What happens when the sensor is too close—or too far?


Line following is one of the fundamental skills in robot competitions, often performed using a color or light sensor to follow a black line on a white surface. But have you ever wondered:

Does the position of the color sensor affect how the robot performs?

To find out, we ran a series of tests, shifting the sensor forward one LEGO stud at a time. The goal was simple: Discover the best sensor position for smooth and stable line following.

Let’s dive into the experiment!

Supplies

1 PC with Windows 10 or 11 operating system.

Test Setup

スクリーンショット 2025-07-11 140646.png

Robot:

A custom-built line following robot using PD (Proportional-Derivative) control.

Program:

pup_motor_t *motorA; // Variable for motor C

pup_motor_t *motorB; // Variable for motor D

pup_device_t *ColorSensor; // Variable for color sensor


void Main(intptr_t exinf)

{

dly_tsk(3000000);


uint8_t in; // Light intensity

uint8_t r = 55; // Target value

int8_t e1; // Previous deviation

int8_t e2 = 0; // Current deviation

float mv; // Manipulated variable

float kp = 1.2; // Proportional gain

float kd = 1.0; // Derivative gain

int32_t left_speed, right_speed; // Motor speeds for left and right


// Motor and sensor initialization

motorA = pup_motor_init(PBIO_PORT_ID_A,PUP_DIRECTION_COUNTERCLOCKWISE);

motorB = pup_motor_init(PBIO_PORT_ID_B,PUP_DIRECTION_CLOCKWISE);

ColorSensor = pup_color_sensor_get_device(PBIO_PORT_ID_C);


while(1) {

in = pup_color_sensor_reflection(ColorSensor);

e1 = e2; // Store previous deviation

e2 = r - in; // Calculate current deviation


// Calculate manipulated variable (mv) using proportional and derivative control:

// mv = (Kp * deviation) + (Kd * change in deviation / measurement time)

mv = (kp * e2) + (kd * (e2 - e1) / 0.010);


// Calculate motor speeds from manipulated variable

if (mv > 0) {

left_speed = 400;

right_speed = 400 * ((100 - mv)/100);

} else {

left_speed = 400 * ((100 + mv)/100);

right_speed = 400;

}

pup_motor_set_speed(motorA, left_speed);

pup_motor_set_speed(motorB, right_speed);

}

exit(0);

}

Control Method:

  1. PD control(Kp = 1.2, Kd = 1.0)
  2. Program written in C language

Sensor Adjustment:

We moved the color sensor forward from 1 to 5 studs in 1-stud increments.

Track:

A simple course with straight lines and curves.


Test Results


🎥 Video Part 1 (1–3 studs):

https://youtu.be/amVXO-ElsdA?si=qCqgoCVUJ1aqJxJ_

🎥 Video Part 2 (4–6 studs):

https://youtu.be/SolJBl6R7WQ

Summary & Insights

🔸 Best Position?

From these tests, the most stable and consistent performance came when the sensor was 3–4 studs ahead of the wheel axle.

🔸 Too Close?

When the sensor is too close (1–2 studs), it’s like the robot is “looking at its own feet.”

It reacts too late to changes in the line, resulting in overcorrections and unstable movement.

Easy to go off track, especially in curves.

🔸 Too Far?

When the sensor is too far forward (5–6 studs), the robot sees too much “future.”

While it may seem like it has more time to react, the delay causes overcompensation and leads to shaky, unstable motion—especially in turns.

Conclusion

Sensor position has a huge impact on how your robot performs in line following tasks.

Want smooth, stable driving? Try placing your sensor around 3–4 studs in front of the wheels.

【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