Distance Sensor#

Introduction#

The V5 Distance Sensor measures how far away an object is, estimates its size, and detects how quickly it is moving toward or away from the robot. It works at distances from 20 mm to 2,000 mm.

The sensor’s accuracy is approximately ±15 mm when an object is closer than 200 mm, and about ±5% for objects beyond that range.

The Distance Sensor uses a Class 1 laser, which provides a precise, straight-ahead detection path and is fully safe for classroom use.

The VEX V5 Distance Sensor.

This page uses distance_sensor as the example Distance Sensor name. Replace it with your own configured name as needed.

Below is a list of available methods:

  • object_distance – Returns the distance from the sensor to the nearest object.

  • object_velocity – Returns the relative speed of an object traveling toward or away from the Distance Sensor.

  • object_size – Returns whether the detected object is of a certain size.

  • is_object_detected – Returns whether the Distance Sensor is detecting an object.

  • changed – Registers a function to be called whenever the Distance Sensor’s value changes.

Constructor – Manually initialize a Distance Sensor.

  • Distance – Creates a Distance Sensor.

object_distance#

object_distance returns the distance from the sensor to the nearest object as a float.

Usage:
distance_sensor.object_distance(units)

Parameters

Description

units

Optional. The unit to represent the distance:

  • MM (default) – Millimeters, 20 to 2000
  • INCHES – 0.78 to 78.74

# Drive forward until the object is within 50 mm away
drivetrain.drive(FORWARD)
while not distance_sensor.object_distance < 50:
  pass
drivetrain.stop()

object_velocity#

object_velocity returns the relative speed of an object traveling toward or away from the Distance Sensor in m/s (meters per second).

Usage:
distance_sensor.object_velocity()

Parameters

Description

This method has no parameters.

object_size#

object_size returns the size of a detected object:

  • ObjectSizeType(0, “NONE”) – No object is detected

  • ObjectSizeType(1, “SMALL”)

  • ObjectSizeType(2, “MEDIUM”)

  • ObjectSizeType(3, “LARGE”)

The size is determined by how much of the Distance Sensor’s view is taken up by the detected object.

Usage:
distance_sensor.object_size()

Parameters

Description

This method has no parameters.

is_object_detected#

is_object_detected returns whether the Distance Sensor is detecting an object.

  • True – The Distance Sensor is detecting an object.

  • False – The Distance Sensor is not detecting an object.

Usage:
distance_sensor.is_object_detected()

Parameters

Description

This method has no parameters.

# Drive forward until the object is within 50 mm away
while True:
  drivetrain.drive(FORWARD)
  if distance_sensor.is_object_detected():
    if distance_sensor.object_distance < 50:
      drivetrain.stop()
      break

changed#

changed registers a function to be called whenever the Distance Sensor’s value changes.

Usage:
distance_sensor.changed(callback, arg)

Parameters

Description

callback

A previously defined function that executes when the Distance Sensor’s value changes.

arg

Optional. A tuple containing arguments to pass to the callback function. See Using Functions with Parameters for more information.

def my_function():
  brain.screen.print("Distance changed")

# Call my_function whenever distance_sensor's value changes
distance_sensor.changed(my_function)

Constructor#

Constructors are used to manually create Distance objects, which are necessary for configuring a Distance Sensor outside of VEXcode.

Distance#

Distance creates a Distance Sensor.

Usage:
Distance(smartport)

Parameter

Description

smartport

The Smart Port that the Distance Sensor is connected to, written as Ports.PORTx where x is the number of the port.

# Create a Distance Sensor in Port 10
distance_sensor = Distance(Ports.PORT10)