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

Diagram illustrating the setup and connection of V5 sensors, highlighting key components and their functions for effective usage in robotics projects.

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.

Diagram illustrating the setup and configuration of V5 sensors, including labeled components and connections, to assist users in understanding how to effectively use sensors in V5 robotics projects.

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.

Illustration of V5 sensors used in robotics, showcasing various sensor types and their placements for optimal functionality in projects.

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

Diagram illustrating the setup and connections for using sensors with V5 robotics system, featuring labeled components and clear visual guidance for proper installation.

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.

Diagram illustrating the setup and connections of V5 sensors, showing various sensor types and their placement for optimal functionality in robotics applications.

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

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

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

Once the sensor detects that the Mobile Goal is inside the Forks or close
      to the sensor

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.

Diagram illustrating the setup and connection of V5 sensors, including labels for each component and their respective functions, as part of the V5 Category Description in the Using Sensors section.

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

Diagram illustrating the setup and connection of V5 sensors, showing labeled components and wiring for proper integration in robotics projects.

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

 In order for the robot to drive forward

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.

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

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

Diagram illustrating the setup and use of V5 sensors, showing connections and configurations for optimal performance in robotics applications.

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

Diagram illustrating the setup and connections of V5 sensors, including labels for each component to aid in understanding their functions in a robotics project.

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

Diagram illustrating the setup and connection of V5 sensors for robotics, highlighting key components and their functions in the system.

Place the Mobile Goal inside the Forks again.

Diagram illustrating the setup and connection of V5 sensors, showing various sensor types and their placements on a robotics platform, relevant to the V5 Category Description in the Using Sensors section.

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

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

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.

Diagram illustrating the setup and connection of V5 sensors, including labeled components and wiring, to aid in understanding sensor integration for educational robotics projects.

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

Diagram illustrating the setup and connections of V5 sensors, including labels for each component to aid in understanding their functions in a robotics project.

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

operator block is used to code
      the Distance Sensor

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

Diagram illustrating the setup and connection of V5 sensors, showing various sensor types and their placement in a robotics project for enhanced functionality and data collection.

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.

Diagram illustrating the setup and connections for V5 sensors, highlighting key components and their functions in a robotics system.

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).

Diagram illustrating the setup and usage of sensors in VEX V5 robotics, showcasing connections and components for effective sensor integration.

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: