Lesson: Exercise: Using an UltraSonic Distance Sensor

Overview: 
Explore the use of the EV3 UltraSonic distance sensor.
Objectives: 

Learn how to use the EV3 UltraSonic distance sensor to detect objects in  the robot's environment. Learn more about utility classes.

Content: 

In order for your robots to navigate in thier environment, you may need to detect obstacles or objects and maneuver in relation to these objects. One way to do that is with the EV3 UltraSonic distance sensor. This sensor uses sound waves to detect objects and measure  the distance to them.

Before we get to that, we will add an additional library class, Lcd. This class exposes simple methods to display text on the EV3 LCD screen. This will be useful in the sensor demo programs. Create a new class file in the library package called Lcd and copy this code into that class just as you did earlier with the Logging library class. You should look it over and see how it works and how it provides useful methods to make your programs easier to write.

Next, just as we did for the Touch Sensor, we are going to use a library class to "wrap" or simplify using the UltraSonic sensor. Create a new class file in the library package called UltaSonicSensor and copy this code into that class. You should study this class and see how it operates the EV3UltrasonicSensor class and exposes a simple set of methods that make using the sensor in your code easier.

With those library classes ready, we can move to a demo program showing the UltraSonic sensor in action. Create a new package called ev3.exercises.ultraSonicDemo. In that package create a new class called UltraSonicDemo and copy the following code into it:

When you run this program will see on the LCD a display of the distance to any object detected by the distance sensor. You can put your hand in front of the sensor and see the distance in meters between the sensor and your hand. When you bring your hand within a quarter of a meter the program will stop.

You can see how the Lcd class is used to display information on the Lcd in fixed positions and make it easy for you to observe what is happening in real time.

 

Navigation: