How to align to a wall with Ultrasonic Sensor from LEGO Mindstorms EV3 Set (part 1)

We have previously aligned to lines with the Color Sensors. In this series we are doing the same program, but with Ultrasonic Sensors that are aligning the robot to a Wall.

  • #163
  • 12 Nov 2015
  • 9:32

It is not very common to use two LEGO Mindstorms EV3 Ultrasonic sensor during competitions. We haven`t seen it a lot to be fair. But for almost every other STEM class and problem using two Ultrasonic sensors could be quite interesting. 



Most of the time during competitions you will use 2 color sensors to align to different lines and as you know where you are at the field and reach different mission models, take them to the mission. You might be in a situation where you have not 2 color sensor, but 2 ultrasonic sensors and you would like to align not to a line, but to a border. In today's video we'll build a program that aligns the robot to the border. The robot will be perfectly aligned to the border.

The Solution

First I moved the border here so you can see it on the camera. I remove the mission model. Now we need to add both sensors. Both sensors must be facing forward. I add the first sensor on the left of the robot. I'll take a second sensor with a beam on which we can mount it.

Then mount the sensor. Then we attach it. Because port 4 is already taken with the other sensor we'll add this one to port 1.

The program that I would like to implement is the following. The robot should align perfectly to the border at a distance of about 4 inches or 10 cm. If the robot is coming from here it will come and then turn until it aligns or if the robot is coming from here it will come, then turn and it will align. Going to the software and programming. Of course, before programming the robot we'll go in this problem step by step so let's first solve part of the problem and then we'll program the next. The first part of the problem is that we are moving in this direction so the sensor attached to port 4 is the first sensor to see the wall. It's the first sensor that reaches distance of about 10 cm. This would be our go and we'll program first this from here. Move and then align and then we'll add additional logic to program for the other direction.

Let's first start from here, this is port 4, this is motor D and this one is motor A. The left motor is D and the right is A. The left sensor is 4 and the right is 1.

Looking at the program we'll start with something very simple. First we'll just move forward, with both motors.

We are moving forward until we are waiting for the sensor on port 4, the ultrasonic sensor to reach a distance. We need this sensor to reach a distance that is less than 4 inches.

Then we stop with our robot.

Then because we have just stopped and we stop with motor A and D the left sensor is 4 inches away from the border and now we must move motor D, which was the right motor. We move motor D forward until the other sensor, the one attached to port 1, reaches a distance from the border of about 4 inches.

Less than or equal to 4 inches and then we stop with motor D.

I'll now try to connect the brick and run the program. Running the program.

What happened is that we are turning with motor D but we should be turning with motor A. Let's just change this is the program. Download and run again.

Here it is. So the robot moves, detects the border with the sensor attached to port 4, stops both motors and then, moves only motor A until the sensor detects the border at a distance of about 10 cm or 4 inches. As you can see that's not very precise.

What can we do to fix this. We have the distance here that is 10 cm, we must move one of the motors, motor D, the left one, a little back so that we align to the border. Let's do this in the program. In the program after we've detected the border with one of the sensors and then move left with only motor A. Then we move back with motor D. Turn on motor D until the sensor on port 4, the left one, detects a distance of exactly 4 inches, then we stop motor D.

This here is a very straight forward, not very elegant, but straight forward program for aligning to the border.

Position the robot, run, move. Again there is a problem with our program. I'm currently looking at it. What happened is that we configured port D, the motor on port D, the left motor, to move forward but we actually had to move it backward. I'll just add -10.

You can find the program below the video and try to run it yourself. Let's run the program now. Move and we have almost perfect align to the border.

You see this small movement backward that actually align the robot to the border and third last time.

That was not the correct program.

In the next video we'll solve the same problem but also adding logic for aligning from the other side, from the right side.