Coding the V5 Distance Sensor using Math Inequalities

Using the V5 Distance Sensor on your robot during a VEX Robotics Competition (VRC) match can allow your robot to score more points using autonomous movements.

The game used as the example for this article is the 2021-2022 VRC game, Tipping Point. View this page for more information on the game and how it is played. For game definitions used throughout this article, an overview of the game rules, and scoring, view the Game Manual for Tipping Point.


The V5 Distance Sensor

Angled view of the V5 Distance Sensor.

The V5 Distance Sensor is one of the powerful V5 sensors which are designed for complete integration with the V5 robotics platform.

  • This sensor uses a pulse of classroom-safe laser light to measure the distance from the front of the sensor to an object.
  • The V5 Distance Sensor can be used to detect an object and determine the relative size of the object. The approximate size of an object is reported as small, medium, or large.
  • The sensor can also be used to calculate a robot’s approach speed. Approach speed measures the speed of the robot/sensor as it moves toward the object.

For more detailed information on the V5 Distance Sensor, how it works, and how to use it with VEXcode V5, view this article from the VEX Library.


V5 Distance Sensor Example

The goal of this project is to have the robot pick up and move a Mobile Goal using the V5 Distance Sensor to detect how far away the goal is.

VEXcode V5 Blocks project that drives forward until it detects a Mobile Goal, picks it up, and then moves it. The project reads When Started, drive forward, wait until DistanceCenter object distance in mm is less than 139, and then stop driving. Next, spin ForkMotors up for 600 degrees and then turn left for 90 degrees. Next, drive forward for 600 mm, spin ForkMotors down for 600 degrees, and then drive reverse for 300 mm.

This is the code example that will be covered in this article.

A breakdown of the behaviors in the example, and how to obtain the parameters used to code the V5 Distance Sensor, will be covered.

You can build the project in VEXcode V5 as you follow along with the article, or you can just read the article for inspiration as an example of how to include the V5 Distance Sensor when you code your own custom robot.

This video shows the code above being executed and the robot picking up and moving a Mobile Goal.

VEXcode V5 Devices menu is opened to show the device configuration in this example. The menu first lists a Distance Sensor titled DistanceCenter at port 19. Next there is a Drivetrain with motors on ports 1 and 10 and an inertial sensor on port 3. Lastly there is a Motor Group named ForkMotors on ports 2 and 9.

Here is the Robot Configuration used in VEXcode V5 to add the robot and the Distance Sensor to the configuration for this example, if you’d like to try the example yourself.

Robot used for this example

View of the Moby Hero Bot from above and with an added V5 Distance Sensor highlighted. The Distance Sensor is located behind and below the robot's fork, facing forward.

The robot used in this example is the 2021-2022 VRC Hero Bot, Moby. Moby’s design does not have any sensors, the V5 Distance Sensor was added to Moby for this example.

For this example, the V5 Distance Sensor is mounted near the center of Moby’s Forks.

For more information on Moby, view this article from the VEX Library.

You do not have to use Moby to code the V5 Distance Sensor or to compete in this year’s competition. You can mount the sensor on your custom robot wherever you see fit.

Note: ensure no structure on the robot is in front of the small laser window on the face of the sensor. There needs to be a clear path in front of the sensor between the goal and the sensor.

Breakdown of the behaviors to score

To pick up and move a Mobile Goal using feedback from the V5 Distance Sensor, first break down how the robot should move.

Moby robot is placed on a field with a Mobile Goal placed a few feet in front of it.

First, the robot should be placed to face the Mobile Goal.

Moby robot is shown after driving forward to sense the Mobile Goal. The Mobile Goal is now inside of the robot's fork arms.

Then, the robot should drive forward until the Distance Sensor detects that the Mobile Goal is inside the Forks and close to the sensor.

Moby robot is shown after detecting the Mobile Goal and raising its arms to pick it up.

Once the sensor detects that the Mobile Goal is inside the Forks or close to the sensor, the robot stops driving and raises the Forks to pick up the Mobile Goal.

Moby robot that is carrying the Mobile Goal and has turned 90 degres to the left.

Once the Mobile Goal has been picked up, the robot will turn left for 90 degrees and drive forward for 600 millimeters (mm).

Moby robot that has lowered its fork to place the Mobile Goal down and then driven backwards.

The robot will then lower the Forks to place the Mobile Goal down, and drive in reverse away from the Mobile Goal so that it does not accidentally knock it over during the next move.


Understanding parameters

In order to execute the above behaviors, it is important to understand how far away the Mobile Goal is from the V5 Distance Sensor.

Parameter to pick up the Mobile Goal using the V5 Distance Sensor

Moby robot on a field with a Mobile Goal placed in between its forks so the ideal distance can be determined.

In order for the robot to drive forward until the Distance Sensor detects that the Mobile Goal is inside the Forks and close to the sensor, the values from the sensor need to be recorded from the V5 Brain.

Place the Mobile Goal in between the robot’s Forks.

V5 Brain screen is shown in the Home menu with the Devices option highlighted.Brain screen is shown in the Device Info menu which has a list of all of the Brain's Smart Ports and connected devices. The Distance Sensor icon in the Device Info menu is highlighted to indicate that items can be selected to open their information menus.

Then, from the Brain’s screen, select ‘Devices’ and then the icon for the Distance Sensor.

Brain screen is shown in the Distance Sensor menu that lists the sensor's data. The menu reports data of an object's size, velocity, and distance. The menu also has a diagram that visualizes the distance and how it changes over time. In this screenshot, the object's size is small, its velocity is 0 meters per second, and its distance is 139 mm.

The distance in millimeters (mm) that the Mobile Goal is away from the sensor is reported on the Brain’s screen.

The same VEXcode V5 Blocks project from earlier that drives forward until it detects a Mobile Goal, picks it up, and then moves it. The value that the Distance Sensor's object distance is compared to in the project reads 139 and it is highlighted.

This is the value that will be used in the project when picking up the Mobile Goal using the Distance Sensor.

Parameter to raise and lower the Forks

Moby robot on a field with a Mobile Goal placed in between its forks.

Place the Mobile Goal inside the Forks again.

Brain screen is shown in the Device Info menu which has a list of all of the Brain's Smart Ports and connected devices. A Motor icon in the Device Info menu is highlighted to indicate that items can be selected to open their information menus.

Then, from the Brain’s screen, select ‘Devices’ and then the icon for Motor 2. Motor 2 controls the left Fork.

Moby robot on a field with a Mobile Goal placed in between its raised forks to pick it up.

Manually raise the Forks until the bottom part of the Mobile Goal is not blocking the view of the Distance sensor.

When manually raising the Forks, ensure to raise each Fork on the left and right side of Moby simultaneously, since each is controlled by their own motor.

Note: avoid pinch points when manually raising the Forks.

Brain screen is shown in the Smart Motor menu that lists the device's data. The menu has readings and graphs that report the device's data. In this screenshot, the motor's Degrees value is highlighted and reads 604.

The number of degrees that the Forks have been raised is reported on the Brain’s screen.

The same VEXcode V5 Blocks project from earlier that drives forward until it detects a Mobile Goal, picks it up, and then moves it. The amount of degrees that the Fork Motor group spins for in the project reads 600 and it is highlighted.

This is the value that will be used in the project when raising and lowering the Forks to pick up and place the Mobile Goal.

Using Operator blocks

VEXcode V5 Less than operator block is shown. This block is used to compare two values and report if the first value is lower than the second one.

In this project, the <Less than> operator block is used to code the Distance Sensor.

Brain screen is shown in the Distance Sensor menu that lists the sensor's data. The menu reports data of an object's size, velocity, and distance. The menu also has a diagram that visualizes the distance and how it changes over time. In this screenshot, the object's size is small, its velocity is 0 meters per second, and its distance is 139 mm.

This is because as the Distance Sensor gets closer and closer to an object, the distance in millimeters between the sensor and that object, decreases.

The same VEXcode V5 Blocks project from earlier that drives forward until it detects a Mobile Goal, picks it up, and then moves it. The value that the Distance Sensor's object distance is compared to in the project reads 139 and it is highlighted.

Threshold values can be selected to trigger behaviors of the robot once the distance is less than a predetermined amount.

For example, when the robot is driving toward the Mobile Goal, use the <Less than> operator block to trigger the behavior of the robot stopping once the distance is less than approximately 139 millimeters (mm).

Moby robot on a field with a Mobile Goal inside of its forks. The distance from the Distance Sensor to the Mobile Goal is highlighted with a red arrow.

If that value changed to 10 millimeters (mm) for example, the robot would actually run into the Mobile Goal and potentially damage itself, because 10 millimeters (mm) away from the sensor is not far enough to prevent the Forks of the robot from touching the Mobile Goal.

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

Last Updated: