Inspiring Mindstorms EV3 Programming for FLL Part 8: Reusable EV3 Line Following My Block

In this post, we will show you how to create a generic EV3 Line Following My Block step-by-step. The flexible My Block allows a robot to smoothly and efficiently follow on the left or right side of a line for some distance. The My Block is a proportional line following program which works well for both straight and curved lines.

In the FLL Robot Game, it is important to move a robot from one point to another consistently. Line following is a very common method. We used the line following method in City Shaper and RePLAY seasons.

Besides Line Following, our post will describe another way of locating our robot through line detection.

The FLL Robot Game map has strips of white/black/white lines. We can use an EV3 light sensor to read the reflected light of those lines. A calibrated light sensor will read 0 for black line and 100 for white line.

We want to follow the edge where there is about 50% of white and 50% of black.

When following the left edge, a robot should turn left if the light sensor detects black and turn right if the light sensor detects white.

When following the right edge, a robot should turn right if the light sensor detects black and turn left if the light sensor detects white.

Left Side Line Following
Left Side Line Following
Right Side Line Following
Right Side Line Following

Proportional Line Following

Motor Power Adjustment = Kp * ( Light Sensor Reading – Target Light Reflection) = Kp * Error

When follow line on the left side,

Motor B Power = Base Power + Motor Power Adjustment
Motor C Power = Base Power – Motor Power Adjustment

That is, if the color sensor is on the white line, the Error will be bigger than 0. The robot will turn right since Motor B has bigger power than Motor C.

If the color sensor is on the black line, the Error will be smaller than 0. The robot will turn left since Motor C has bigger power than Motor B.

If the color sensor is in the middle, the Error will be 0. The robot will move straight since Motor B and Motor C have the same power.


Similarly, when follow line on the right side,

Motor B Power = Base Power – Motor Power Adjustment
Motor C Power = Base Power + Motor Power Adjustment

We can add side information to calculate the power adjustment:

Motor Power Adjustment = Kp * ( Light Sensor Reading – Target Light Reflection) * Side = Kp * Error * Side

When Side = 1, follow line on the left side. When Side = -1, follow line on the right side.


Where Kp is a constant that helps us tune the motor power adjustment. The bigger Kp is the higher will be the turning when there is an error. If Kp is too big, the robot may overreact.

You will need testing many times to find a suitable Kp for your robot. Start with Kp=0.1 and run your robot. If the robot can’t follow the line then increase Kp. If your robot turns violently on the line, decrease the Kp value.

The following is the Line Following program based on above description. The MyCheckDistance My Block is described in our previous post.

Unregulated Motor Block is used for driving the robot. The Unregulated Motor block does not include automatic motor control, like the normal Medium Motor block and the Large Motor block. This means that no automatic regulation on motor power will be included. The specified Power input is what is used to control the motor.

EV3 Line Following
EV3 Proportional Line Following

Creating EV3 Line Following My Block

To make the blocks in the Loop block reusable, we can create a EV3 Line Following My Block. First select those blocks, then select Tools My Block Builder menu item as shown below.

EV3 Line Following
EV3 Line Following a)

Now enter the My Block’s name, description, and select an icon.

EV3 Line Following
EV3 Line Following b)

Add the parameter for the light sensor port.

EV3 Line Following
EV3 Line Following c)

Add the parameter for TargetReflectionLight.

EV3 Line Following
EV3 Line Following d)

Add the parameter for the Base Power.

EV3 Line Following
EV3 Line Following e)

Add the parameter for the distance to follow the line

EV3 Line Following
EV3 Line Following f)

Add the parameter for the Side of line to follow and click Finish button.

EV3 Line Following
EV3 Line Following g)

At last, connect all the Parameters. Done!

EV3 Line Following
EV3 Line Following h)

Using the EV3 Line Following My Block.

The following program uses the Line Following My Block. This My Block will use port 2 Light Sensor to follow the line on the left side for 12 inches with Base Power of 25.

Using EV3 Line Following My Block
Using EV3 Line Following My Block

The following program will use port 1 Light Sensor to follow the line on the right side for 21 inches with Base Power of 25.

Using EV3 Line Following My Block
Using EV3 Line Following My Block

Note that to have consistent performance. Light sensors should be calibrated before using the Line Following My Block. Our post describes what the light sensor calibration is and how to do it.