Main Content

Implement Line Tracking Using LEGO MINDSTORMSEV3 Hardware

This example shows how to implement a line tracking algorithm for a two-wheeled robot built with LEGO® MINDSTORMS® EV3 hardware.


Line tracking is a classic robotics application that integrates sensing, actuation, and control algorithms. Line tracking robots are relatively easy to build and can autonomously navigate a pre-marked path without human input.

This example implements a line tracking algorithm on a two-wheeled EV3 robot with a color sensor. The robot uses feedback from the Color sensor to track the line. It uses feedback from the motor encoders to maintain forward velocity. Two Proportional-Integral-Derivative(PID) controllers control the velocity and tracking.


We recommend completing Getting Started with LEGO MINDSTORMS EV3 Hardware and Communicating with LEGO MINDSTORMS EV3 Hardware examples.

Required Hardware


  • Two LEGO MINDSTORMS EV3 Large Motors

  • EV3 Color Sensor

  • EV3 Wi-Fi® Dongle or USB Ethernet Adaptor

Task 1 - Set up Robot and Track

1. Build a two-wheeled robot with a color sensor. You can build a robot similar to the one described in the printed building instructions in the education core set.

2. Set up a connection between EV3 brick and your host machine. Please refer to Task 1 and 2 in Getting Started with LEGO MINDSTORMS EV3 Hardware example.

3. Set up the track. For example, you can draw a thick black line on a flat white surface.

Task 2 - Configure Model and Calibrate Parameter

1. Open the ev3_linetracking Simulink® model.


Sensors and Motors contain blocks that interface with the EV3 hardware.

State Estimation uses the values from each motor encoder to calculate the position and velocity of the robot.

Desired Velocity takes the user-provided Velocity (m/s) and converts it into the desired state values for the velocity controller.

Desired Light takes the Color sensor's white and black values to choose an appropriate reference value for the light.

Velocity Control has the PI controller implementation to control the forward velocity.

Line Tracking Controller has the PD controller implementation to control the turning.

2. Configure the model as described in Task 5 of the Getting Started with LEGO MINDSTORMS EV3 Hardware example.

3. Open the Sensors and Motors subsystem, and set right port number for the Encoder, Motor and Color Sensor blocks.

4. Calibrate the black and white light intensity value. The values of White Value and Black Value blocks are the Light Sensor block's output value for white and black colors. You can calibrate them accordingly for your robot and track.

Task 3 - Run the Model on the Robot

1. In the Hardware tab of your Simulink model, click Build, Deploy & Start. The model will now run on LEGO MINDSTORMS EV3 hardware.

2. Place the robot on the ground on the right edge of the line to be tracked. Observe that the robot moves along the track and makes corrections as it loses the track.

Other Things to Try

  • Try changing the parameters of both the velocity controller and the line tracking controller to achieve smoother line tracking

  • Use external mode to tune the controller parameters in real-time.