# How to follow a black line (EV3 version) ProPreview

In this tutorial we introduce the basic algorithm for following a line with one sensor and review the algorithm for quickly followwing a black line with one sensor, implementing both with the Mindstorms EV3 robotics kit.

• #49
• 10 Dec 2013
• 9:58

### Oscillating

We have several inquiries about following a black line with the EV3 Kit. In this episode we suggest two ways, both using only one EV3 color sensor. The first is the most basic way for following a black line - oscillating between the black line and the whitespace, advancing with each oscillation further forward. With this the robot actually follows the edge of the black line.

### Faster

The second way is the algorithm from Episode 31, where we have introduced a faster way for following a black line. In this case both motors are moving forward, so the robot advances faster forward. Again the aim is to follow the edge of the black line, but this time, if we get far from it, we just increase the power of the motor that is further away from the edge.

Note: In this tutorial we have used the EV3 Color Sensor, but both programs can be performed using the NXT Color Sensor or the NXT Light Sensor. More on the topic could be found in Episode 48.

### Follow a black line block

Download the whole Mindstorms EV3 project created in the tutorial with the myBlocks included:

### English

The First Lego League Competition is all about positioning and having a good orientation on the field. A common way to do this is by following the black lines. We have devoted previous tutorials on how to follow a black line using the NXT kit. In this tutorial I will focus on the same thing but this time I'll be using the EV3 kit.

First of all, we should start by putting the EV3 color sensor on the robot. In our case I have chosen to put it right here and now let's see what the consequences are. OK. We start the program and the robot will do something like this and then it will go away. However, the problem is not with the program but with the robot. As you can see, the red dot, this is the EV3 color sensor, is positioned exactly between the two wheels. When the robot turns, the sensor is almost static. That's because the sensor is on the same line as the point of the turn. If we want to detect the black line, we should make a huge turn which is very difficult to achieve. But if we position the sensor on the place of the gyro sensor, the robot will detect the black line only with a slight turn. Now I will change the position of the two sensors and see the difference. OK. Several minutes later the position of the two sensors is switched and the EV3 color sensor is in front of the robot. I will run the program and as you can see it's working just fine. In conclusion, the EV3 color sensor should not be on the same line as the two motors but it should be in front of the robot which means the sensor should follow the direction in which the robot is moving. So, if the robot is moving backwards, it'd be better if the sensor is at the back of the robot. Our strategy of following the black line will be to follow the line between the black line and the rest of the field. So, first we'll move the right motor until the sensor detects the black line and the left motor until the sensor detects a white space. Then the right motor again, etc. Now, let's take a look at the program. First, we have a loop that repeats itself forever. We have a motor block in it which turns on motor B. Motor B is on until the EV3 color sensor detects a black line or in our case detects a value less than 15. Then motor B stops and we turn on motor C which is on until the EV3 color sensor is no longer on a black line or the value is greater than 15 and finally motor C stops. 15 is a good threshold that works well in our case and will work well in most of the cases. Still, in some cases it may not be the correct value. So, we should go to Port View where we can see the value of the EV3 color sensor. As you can see, when the sensor is on white space, the value is 40, 42, 43. When we move the sensor on the black line, it's 4. So, 15 is a good threshold value. Let's run the program.

Now I will run the second one.