Using the Distance Sensor in VEXcode VR with Python

The Distance Sensor on the VR Robot reports the distance between the VR Robot and the nearest object. The sensor calculates distance by using the time it takes for laser light to bounce off of an object and return to the sensor.


Distance Sensor on the VR Robot

Screenshot of VEXcode VR interface showcasing the block-based coding environment, designed for teaching coding concepts through a virtual robot, as part of a tutorial for students and educators in STEM education.

The Distance Sensor is a sensor that reports the distance between the VR Robot and the nearest solid object.

Screenshot of VEXcode VR tutorial interface, showcasing the block-based coding environment for programming a virtual robot, designed to aid students in learning coding concepts and robotics principles.

The Distance Sensor uses Class 1 lasers similar to lasers modern cell phones use for head detection. It sends out a tiny pulse of safe laser light, then uses the time it takes for the pulse to reflect to calculate the distance of nearby objects. The sensor calculates the distance based on how long the laser pulse took to make the round trip.


VEXcode VR Commands used with the Distance Sensor

Screenshot of VEXcode VR interface showcasing the block-based coding environment, designed for teaching coding concepts through virtual robotics in educational settings.

The commands used with the Distance Sensor can be found in the Sensing category.

get_distance Command

Screenshot of VEXcode VR interface showcasing the block-based coding environment, designed for teaching coding concepts through a virtual robot, as part of the Tutorials section.

Using the get_distance command, the Distance Sensor can report the distance of the nearest object in millimeters (mm) or inches. The get_distance command reports numeric values in either millimeters (mm) or inches.

Screenshot of VEXcode VR tutorial interface showcasing block-based coding options for programming a virtual robot, designed to enhance coding skills and problem-solving in STEM education.

The distance will be displayed in millimeters (mm) and inches in the Monitor Console.

found object Command

Screenshot of the VEXcode VR programming interface, showcasing the block-based coding environment for users to learn coding concepts through a virtual robot, designed for educational purposes in STEM.

The Distance Sensor detects if there is an object in front of it using the Distance found object command. The Distance Sensor can detect an object or surface within 3000 mm (~118 inches) of the sensor. The Distance found object command returns a Boolean value as either True or False.

Screenshot of VEXcode VR interface showcasing the block-based coding environment, designed for teaching coding concepts through virtual robotics, featured in the Tutorials section for educators and students in STEM education.

True or false will be reported and appear in the Monitor Console.

The laser allows the sensor to have a very narrow field of view, so detection is always directly in front of the sensor. 


Common Uses of the Distance Sensor

Screenshot of the VEXcode VR programming interface, showcasing the block-based coding environment designed for teaching coding concepts through virtual robotics. Includes tools for creating, testing, and debugging code in a simulated setting.

The Distance Sensor can detect if there is an object in front of the VR Robot. This can be used to avoid an obstacle.

Screenshot of VEXcode VR interface showcasing the block-based coding environment for programming a virtual robot, highlighting features for beginners and advanced users in a STEM education context.

The Distance Sensor can measure the distance between the front of the Distance Sensor and the front of the object. This can be helpful to complete a maze without continuously bumping into the walls or to avoid an object.


Distance Sensor Example Project

Screenshot of VEXcode VR interface showing a block-based coding environment for programming a virtual robot, designed to facilitate learning coding concepts and robotics principles in educational settings.

In this example, the VR Robot will drive forward if the Distance Sensor finds an object. If it does not detect an object, the VR Robot will turn right and keep checking for an object.

Note: This example will find the first building, but needs improvements to avoid falling off the table.

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

Last Updated: