The Ultrasonic Sensor is digital sensor that can measure the distance to an Object in front of it. It does this by sending out hight frequency sound waves and measuring how long it takes the sound to reflect back to the sensor. The sound frequency is too high for you to hear.
Distance to an object is measured in millimeters (mm). This allows you to program your robot to stop at a certain distance from a wall.
#!/usr/bin/env pybricks-micropythonfrompybricks.hubsimportEV3Brickfrompybricks.ev3devicesimportMotor,UltrasonicSensorfrompybricks.parametersimportPortfrompybricks.toolsimportwaitfrompybricks.roboticsimportDriveBase# Initialize the EV3 Brick.ev3=EV3Brick()# Initialize the Ultrasonic Sensor. It is used to detect# obstacles as the robot drives around.obstacle_sensor=UltrasonicSensor(Port.S4)# Initialize two motors with default settings on Port B and Port C.# These will be the left and right motors of the drive base.left_motor=Motor(Port.B)right_motor=Motor(Port.C)# The DriveBase is composed of two motors, with a wheel on each motor.# The wheel_diameter and axle_track values are used to make the motors# move at the correct speed when you give a motor command.# The axle track is the distance between the points where the wheels# touch the ground.robot=DriveBase(left_motor,right_motor,wheel_diameter=55.5,axle_track=104)# Play a sound to tell us when we are ready to start movingev3.speaker.beep()# The following loop makes the robot drive forward until it detects an# obstacle. Then it backs up and turns around. It keeps on doing this# until you stop the program.whileTrue:# Begin driving forward at 200 millimeters per second.robot.drive(200,0)# Wait until an obstacle is detected. This is done by repeatedly# doing nothing (waiting for 10 milliseconds) while the measured# distance is still greater than 300 mm.whileobstacle_sensor.distance()>300:wait(10)# Drive backward for 300 millimeters.robot.straight(-300)# Turn around by 120 degreesrobot.turn(120)