# How to code automatic line-following programs
In the AI era, automatic line-following is a must for most robots. What is the principle of line following? This # How to tells you the answer. You can follow it to learn line-following programs.
# Principle of line following #
Block explanation:
Multiplying "kp" by the deviation value can reduce the value returned by the Quad RGB Sensor, achieving the goal of fine-tuning the speed.
During line following, when mBot2 deviates to the left, the deviation value is negative, and when it deviates to the right, the deviation value is positive.
Therefore, when the robot deviates to the right, the left wheel needs to slow down to lead the center of the robot back to the center of the line.
To achieve this, "left_power" needs to increase by subtracting the deviation value (which is negative) from "base_power", making the left wheel speed up and the center of the robot return to the center of the line.
As the deviation value approaches zero, the robot speed approaches "base_power".
Since the left and right wheels are symmetrically installed, their rotating directions are opposite and their speed values have opposite signs.
Assigning the speed values to the corresponding motors implements line following.