Using the VEX IQ (1st gen) Distance Sensor

Description

This sensor reports the distance between it and the nearest solid object (50mm - 1m range). It calculates distance by using the time it takes for ultrasonic waves to bounce off of an object and return to the sensor.

VEX IQ (1st gen) Distance Sensor piece.


How the Distance Sensor Works: Listening for Distance

Diagram of the Distance Sensor sending ultrasound out to hit a Green Cube and then recieving the reflected sound.

The Distance Sensor uses the same principles as sonar devices to measure sound. It sends out a tiny pulse of ultrasound with its emitter at one side of the sensor, and then times how long it takes to hear the sound reflect back the receiver at the other side of the sensor. The sensor calculates the distance based on how long the sound pulse took to make the round trip. The Distance Sensor then makes a calculation based on the speed of sound to tell the robot brain how far the nearest object is. 

If the surface is not flat like a curved wall or a ball, the ultrasonic wave does not bounce off of all parts equally or at the same time. The closest surface large enough to reflect back a detectable amount of the wave determines the detected distance of the object.


Common Uses of the Distance Sensor:

VEXcode IQ Object found block that reads Distance found an object?

Two diagrams. Above, the Distance Sensor is pointing towards a nearby Green Cube and is labeled with a checkmark. Below, the Distance Sensor is not pointing at anything nearby and it is labeled with an X symbol.

This sensor can detect if there is an object in front of it.

VEXcode IQ Distance from block that reads Distance distance in mm.

This sensor can measure the distance between it and an object.

  • A robot can use this sensor to drive a certain distance away from an object. View the animation below to see the distance value change as the robot drives until the distance is below 160mm.

  • A robot can use this sensor to drive up to an object without touching it. View this animation to see two robots using Distance Sensors to coordinate their driving.

  • The values provided by this sensor can be used to adjust the robot’s velocity depending on whether it is near or far away from an object or surface. View this animation to see a robot adjust based on the distance to a wall.


Uses of the Distance Sensor on a Competition Robot

  • The robot can use the Distance Sensor to initiate a sequence of behaviors when it is in the correct place to do so. For example, the robot can drive forward until it detects that it is 150mm away from a wall, then turn 90 degrees, and release an object into a scoring zone.  
  • The Distance Sensor can be used to trigger robot actions, such as picking up a game piece, when an object is detected within range.
  • The robot can use the Distance Sensor to drive toward an object or surface until it is within a specific distance, without touching it. This can be used to make sure that the robot does not knock over game pieces.
  • A robot can use the Distance Sensor to drive away from an object or surface until it has reached a specific distance. This can be used to avoid collisions with other robots on the field.

Using the Distance Sensor in VEXcode IQ

Adding the Distance Sensor as a Device in VEXcode IQ

VEXcode IQ Devices menu after the Add a device button has been selected. The Distance (1st gen) option is highlighted.

To code the Distance Sensor in VEXcode IQ, you must first configure the Distance Sensor. View this article to learn more about configuring a sensor in VEXcode IQ.

Once the sensor is configured, commands will appear in the Toolbox that you can use in your project.

Coding the Distance Sensor in Blocks

VEXcode IQ Object found block that reads Distance7 found an object?

The <Object found> block is a Boolean reporter block that reports a condition as either true or false. Boolean blocks, like the <Object found> block fit inside blocks with hexagonal (six-sided) inputs for other blocks.

The <Object found> Boolean block reports 'true' if the sensor detects an object, and 'false' if the sensor does not. To learn more about Boolean blocks visit the Help or the Block Shapes and Meaning article.

 

VEXcode IQ blocks project that uses a Distance Sensor to drive forwards until it finds an object. The project reads When started, drive forward, wait until Distance7 found an object, and then stop driving.

In this example, the <Object found> block is used with a [Wait until] block to make the robot drive forward until the Distance Sensor detects an object. When the sensor detects an object, the robot will stop driving, as shown in the image above. 

VEXcode IQ Distance from block that reads Distance7 distance in mm.

The (Distance from) block reports the distance of the nearest object from the Distance Sensor. It reports a range from 24mm to 1000mm, or 1 inch to 40 inches. The (Distance from) block is a reporter block that is used inside blocks with circular spaces.

 

VEXcode IQ blocks project that uses a Distance Sensor to drive forwards until its distance is less than 50mm. The project reads When started, drive forward, print Distance7 distance in mm on Brain, wait until Distance7 distance in mm is less than 50, and then stop driving.

In this example, the (Distance from) block is used with a [Print] block to display on the Brain screen the distance between the Distance Sensor and an object. The (Distance from) block is also shown inside a <Less than> block which is inside a {Wait until} block. This will cause the robot to drive until the Distance Sensor detects an object less than 50 millimeters away, and then stop driving. This is shown in the first video above.

Coding the Distance Sensor in Python

Note: To code a VEX IQ (1st generation) Distance Sensor in Python, it must be connected to a VEX IQ (2nd generation) Brain. The VEX IQ (1st generation) Brain does not support Python.

distance_7.is_object_detected()

The distance.is_object_detected command reports 'true' if an object is detected, and 'false' if an object is not detected.

Note: The name of the Distance Sensor that appears in the command corresponds to the name it is given in the configuration. 

drivetrain.drive(FORWARD)
while not distance_7.is_object_detected()
wait(20, MSEC)
drivetrain.stop()
In this example, a While loop with a not condition is used with the distance.is_object_detected command to make the robot drive forward until the Distance sensor detects an object. When the sensor detects an object, the robot will stop driving, as shown in the Python code above.
distance_7.distance(MM)

The distance.distance command reports the distance of the nearest object from the Distance Sensor. It reports a range from 24mm to 1000mm, or 1 inch to 40 inches.

drivetrain.drive(FORWARD)
brain.screen.print(distance_7.distance(MM)
while not distance_7.distance(MM) < 50:
wait(20, MSEC)
drivetrain.stop()

In this example, a While loop with a not condition is used with the distance.is_object_detected command to make the robot drive forward until the Distance Sensor detects an object. When the sensor detects an object, the robot will stop driving.

In this example, the distance.distance command is used with a brain.screen.print command to display on the Brain screen the distance between the Distance Sensor and an object. The distance.distance command is also shown inside a While loop with a not condition. This will cause the robot to drive until the Distance sensor detects an object less than 50 millimeters away, and then stop driving. This is shown in the first video above.

To learn more about coding a Distance Sensor in Python, check out Unit 5 of the Computer Science Level 1 - Python course. 

For more information, help, and tips, check out the many resources at VEX Professional Development Plus

Last Updated: