Lesson: Exercise: Drive and Avoid Obstacles

Explore using two sensors, UltraSonic and Gryo, to detect and avoid obstacles while driving.

Understand how to use sensors to program your robot to avoid obstacles while driving.


We have looked at test programs for several sensors. Now lets use two sensors to create a practical example of using sensors. We will take the simple driving sample and use an UltraSonic sensor to detect obstacles in the path of the robot and then use the Gyro sensor to execute a 90 degree turn to avoid an obstacle and continue driving. A Touch sensor is used as a way to stop the program along with the escape key on the EV3.

Create a new package called ev3.exercises.driveAvoid. Create a new class in that package called DriveAvoid and copy the following code into that class:

This program will drive the robot and if it detects an obstacle in its path it will make a 90 degree right turn and continue driving. It will drive until the escape key or touch sensor is pressed.


Lesson navigation: