Ultrasonic Sensor. Simple follow a wall (part 1)

This tutorial is about how to follow a wall with an ultrasonic sensor. The example is with a border from a FIRST LEGO League (FLL) competition, but could also easily be applied to other competitions and problems.

  • #161
  • 20 Oct 2015
  • 8:27

Previous courses

The LEGO Mindstorms EV3 Ultrasonic Sensor detects objects. You can learn more about the sensor in the EV3 basics course. Ultrasonic Sensor that will introduce you to programming and using it.

This video

We start with a simple experiment for following a border by keeping a distance of about 10 centimeters/4 inches from the wall. The sensor is placed on the side of the LEGO robot. As a result the robot moves in Zig-Zag while following the wall.


During a competition you can also use the LEGO Mindstorms EV3 ultrasonic or infrared sensor to position yourself on the field. You can use this sensor to detect the distance to different objects, for example you can use the sensor to detect the distance to the border right here, here's the border of the field. You can place the robot at exactly, let's say 4-5 inches off the border about 10 cm. Then you can turn for 90 degrees and you know where you are compared to this competition module. Then you can take the loop for this competition module or you can use the sensor, that's not very common, but you can use the sensor to try to detect the object itself, so you can turn until you see the object, you see something that's about 10-15 cm or 5-6 inches from your robot and then you know how to approach the loop and how to take it you can also use the ultrasonic sensor to follow the border and to move near the border and to follow it and this is the program that we'll build today.

Again, I would like to implement a program that follows the border of a field. First, I'll have to make sure that the sensor is pointing to the border. I'll have to disassemble this and try to add this part here and then attach the sensor.

I'll move this whole construction to the other side of the robot.

Now the sensor is facing the border and I can implement a program that will follow the border and we'll keep a distance of about, let's say 10 cm or about 4 inches and to move the robot in this direction following the border. Let's attach the sensor to port 4, the default port for ultrasonic sensor. Let's check out the program.

First I'll start with a very simple program for following the border with the ultrasonic sensor.

The program is the following. I'll start motor, in my case, motor A

and I'll just move it forward. I'll move motor A forward until and now you can remember all the things that we discussed for the ultrasonic sensor in the previous videos in the EV3 basic course. We turn motor A until the ultrasonic sensor detects a distance of let's say 4 inches. We need distance of 4 inches or less than 4 inches.

Then stop with motor A.

Download and run. This here is the robot and I'll place it like this. We have some distance here between the border and the ultrasonic sensor when running the program the robot moves and it moves until the sensor detects a distance of about 4 inches. I can place the robot on another place.

It will be something like this. The next step is to move the robot with, so this is motor A - the right motor and this is motor D - the left motor, to move with the left motor until we detect a distance grater than 4 inches, about 10 cm and then stop, and then move again until we detect distance less than 4 inches, more than 4 inches, less than 4 inches, more than 4 inches or about 10 cm. This will make the robot follow the border. It's very simple it's called zig-zag following because it is following in a zig-zag movement. After we stop with motor A we'll start motor D.

Start motor A until we see something equal to or less than 10 cm then start motor D that will move the robot away from the border until

the ultrasonic sensor detects a distance in inches that is greater than 4 inches.

Then stop with motor D,

just turn it off.

You saw this zig-zag movement.

Now if we repeat this movement in a loop we would actually follow the border on the field.

I'll start the program again.

let's put them in a loop. I'll take a loop block and just add all the blocks inside the loop.

Just all the blocks inside the loop.

This would be the program for following the border. It's very similar to the program for following lines. Very simple program for following lines with the color sensor, but this time it's an ultrasonic sensor. It's not very advanced program, it's not even smart, but it's very simple and it's working. It's a zig-zag program. Place the robot like this and run the program.

You can see that the robot is actually following the border.

It might not be very useful most of the time because we have this zig-zag movement. in the next videos we'll look for ways to improve this program, so that we remove this zig-zag movements.