# EV3 basics course. Ultrasonic Sensor. Detect object (part1)

We are introducing the LEGO Mindstorms EV3 Ultrasonic Sensor. The first program is actually to detect an object and stop near it, at about 4 inches/10 centimeters. The ultrasonic sensor could be used during robotics competitions, but most of its use is for experiments.

• #142
• 12 Oct 2015
• 6:53

The Ultrasonic Sensor was quite popular in the NXT version of the robot and is still used in the EV3 robotics sets. The sensor detects distance to objects with a minimum range of about 1 centimeter/0.4 inches to about 255 centimeter/100 inches.

### Ultrasonic sensor. Detect object

EV3-G program for using the ultrasonic sensor and detecting objects with it.

### English

In the next few videos we'll continue with a basic introduction to the EV3 ultrasonic sensor. It's functions and how we can use to accomplish different tasks.

This will again be a very basic introduction we'll do this in a couple of videos, probably 2 or 3 and then we'll leave you few tasks that you can solve and to learn more about the ultrasonic sensor. The first thing is that we should add the sensor to the robot so I'll remove the touch sensor and I'll add the ultrasonic sensor.

Now it's not mandatory but it is a good practice to have the different sensors attached to different ports and there are some ports by default, for example, for the touch sensor it was port 1and for the ultrasonic sensor is port 4 and it's always a good practice to follow these default values, of course there can be a situation where you want to attach to another port but as a starting point follow the default values. Let's see how we can program the sensor. The goal of this sensor is to detect if there's an object at a specific distance and this distance might be in cm or inches and the sensor will also give us the distance to this object for about let's say 5 inches, 10 inches, a foot or a meter and the maximum value I think was 100 inches or 255 cm and it basically gives us with the precision of about a cm and that's how the sensor works. How can we use this sensor? We can use it to find different objects, to find walls and stop and we can use it, of course for many other tasks. Let's start with the basic programming for this sensor. The first program that I would like to build is to move the robot until we reach an object. We start with the Tank block and we move forward with motors A and D

and we move forward until, as you may remember from the touch sensor it was until we touch something, now it'll be until the ultrasonic sensor detects a distance let's say in inches and I would like to have something that's at about 4 inches, that's about 10 cm. We move with the robot until we reach an object that is about 4 inches away, about 10 cm. Let's download and run this program.

Let's see if it works.

The robot should move forward until it reaches my hand at a distance of about 10 cm.

Let's try it.

Now it seems that the program might work but if I place my hand very close to the robot and I run again you see that the robot does not stop it tries to move forward and we've made the same mistake we did with the touch sensor and this is that we are moving the motors not for an unlimited amount of time, for undefined amount of time but we are moving them for 1 rotation. That's a very common mistake and that's why I would like to emphasize on this because it's very common for everybody to do this mistake.

The sensor is not behaving as we expect it. Let's change our program. Since we are moving always for 1 rotation with the motors and then we start waiting the robot will always move for 1 rotation, no matter if we have an object or we don't have an object it will always move for 1 rotation. The solution is to turn the motors to just On with some power of let's say 50. Just turning them on means move forward until we reach something that is 4 cm away or less than 4 inches away and then we must stop our motors.

That's our program. Move until we see something at less than 4 inches and stop. Let's run the program.

What will happen now is that the robot will move forward until it sees my hand at a distance of about 4 inches or 10 cm. Let's start the program.

Move, stop. Now if initially my hand is very close to the robot, the robot will not even start. It'll start the motors but it will stop them immediately.

The robot does not move. So that was a very basic introduction to the ultrasonic sensor. In the next video we'll continue with a few more complex and interesting programs.

## Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

• 38
• 193:26
• 30
• 5
• 0
• 3
• 3d_rotation 0
• 136
• 280:11
• 156
• 4
• 0
• 0
• 3d_rotation 0
• 136
• 280:11
• 156
• 4
• 0
• 3
• 3d_rotation 0