Using Distance Sensor with VEX EXP

The Distance Sensor is one of the powerful sensors which are designed for complete integration with the EXP 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.

VEX Distance Sensor piece.


Description of the Sensor

The Distance Sensor has the following capabilities:

  • Measuring distance: The sensor uses a pulse of classroom-safe laser light to measure the distance from the front of the sensor to an object. The distance is reported in inches or centimeters on the Brain's Sensor Dashboard, and in inches or millimeters in VEXcode EXP.
  • Detect object: The sensor can also be used to detect when it is near an object.
  • Determine object relative size: The sensor can also be used to determine the relative size of an object that has been detected. The approximate size of an object is reported as small, medium, or large.
  • Report object velocity: The sensor can be used to calculate and report the velocity in meters per second for an object approaching the sensor, or the sensor approaching an object.

Diagram of a Distance Sensor being attached to a Support piece with two screws.

The back of the sensor's housing has five holes to provide flexibility when mounting the sensor to a robot.

Distance Sensor piece with its sensing window highlighted. The window is set in slightly into the front side of the sensor. A label reads Window for Laser.

On the face of the sensor there is a small window where the laser beam is sent out and then received in order to measure distance.

Diagram of an EXP Brain connected to a VEX Distance Sensor.

In order for the Distance Sensor to be functional with the EXP Brain, the sensor’s Smart Port and an EXP Brain’s Smart Port need to be connected with a Smart Cable.

The sensor will work with any of the 10 Smart Ports on the EXP Brain.

When connecting an EXP Smart Cable to the ports, be sure the cable’s connector is fully inserted into the port and the connector’s locking tab is fully engaged.


How the Distance Sensor Works

The Distance Sensor sends out a pulse of classroom-safe laser light and measures the amount of time it takes for the pulse to be reflected. This allows for a calculation of distance.

The sensor’s Class 1 Laser is similar to the lasers used on modern cell phones for head detection. The laser allows the sensor to have a very narrow field of view, so detection is always directly in front of the sensor.

The sensor’s measurement range is 20 millimeters (mm) to 2,000 millimeters (mm) (0.79 inch to 78.74 inches). Below 200 millimeters (mm) the accuracy is approximately +/‐15 millimeters (mm); above 200 millimeters (mm) the accuracy is approximately 5%.

The Distance Sensor needs to be paired with VEXcode EXP to create a user program for the EXP Brain to utilize the sensor’s readings to control the robot’s behavior.

The EXP Brain in concert with a user project can be used to convert the Distance Sensor readings into:

  • Distance to an object measured in centimeters, millimeters, or inches.
  • Object velocity in meters per second.
  • Object size is small, medium, or large.
  • Object found.

Setup of the Distance Sensor

Placement

BaseBot build that has an attached Distance Sensor that is highlighted and pointing in front of the robot.

The placement of the Distance Sensor is very important to obtaining accurate readings.

Be sure no structure on the robot is in front of the small sensor's window on the face of the sensor.

There needs to be a clear path in front of the sensor between any object being measured and the sensor.

Reading Distance Sensor Values

Brain screen is shown in the Devices menu with a Distance Sensor device selected.

It is helpful to use the Devices screen on the EXP Brain to see the information the Distance Sensor is reporting.

From the Sensor Dashboard, the Distance Sensor dashboard reports the distance of the nearest object in either inches or centimeters.

The units can be changed by selecting the Check button on the Brain to toggle between inches and centimeters.

To use the Sensor Dashboard, follow the steps in this article. 


Adding the Distance Sensor as a Device in VEXcode EXP

Whenever a sensor is used with a programming language, it needs to be configured within that language. 

VEXcode EXP Devices menu after the Add a device button has been selected. The Distance option is highlighted.

With VEXcode EXP, this is accomplished by using the ‘Add a device’ feature from the Devices window.

To add the Distance Sensor to the configuration, follow the steps in this article. 

Screenshot of VEXcode EXP with the Devices menu open. A connected Distance Sensor is listed and highlighted in the Devices menu. The blocks Toolbox is open and the Distance Sensing cateogory of blocks is highlighted.

Once the Distance Sensor has been added to your project, a new set of sensor blocks will become available.

For more information on the blocks from the ‘Sensing’ category pertaining to the Distance Sensor, view the Help information (Blocks project).


Common Uses of the Distance Sensor

The Distance Sensor can produce several measurements which can be used to change the behavior of the robot. These features include:

Detect an object

VEXcode EXP blocks project that uses a Distance Sensor to find an object and then drive towards it. The project reads When started, turn right, wait until Distance7 found an object, and then drive forward for 400mm.

This feature allows your robot to detect an object when it becomes within the range of the Distance Sensor. The Distance Sensor will report a found object when it is approximately less than 1000mm away.

The example project shown on the left is used to code a robot with a Distance Sensor mounted on the front to turn until it detects an object, such as a cube, and then drive forward once the object is detected by the sensor.

Distance to an object

VEXcode EXP blocks project that uses a Distance Sensor to drive the robot towards an object and then uses the claw to grab it. The project reads When started, drive forward and then wait until Distance7 object distance in mm is less than 75. Lastly, stop driving and spin ClawMotor close for 25 degrees.

This provides a measurement between the front of the sensor and an object or a barrier/wall.

The example project shown on the left is used to code a robot with a Distance Sensor mounted on the front and a claw attached. The robot will drive until it detects an object is less than 75mm away from the sensor, then close the Claw around the object. This example would be helpful if it is known that there is an object in front of the robot and you want the robot to drive forward to collect that object. 

To determine how far away an object is from the sensor and use that parameter in the project, use the Sensor Dashboard on the EXP Brain. See this article for more information about using the Sensor Dashboard.

Report object velocity

VEXcode EXP blocks project that uses a Distance Sensor and Print blocks to report an object's velocity live. The project reads When started, set print precision to 0.1 on Brain. Next is a Forever loop with 5 blocks in it. The 5 blocks read Clear all rows on Brain, set cursor to row 1 column 1 on Brain, print Distance7 found an object on Brain and set cursor to next row, print Distance7 object velocity in m/s on Brain, and lastly wait 0.25 seconds.

This feature provides a measurement of the velocity in meters per second for an object approaching the sensor, or the sensor approaching an object.

In order to observe the velocity changing with an approaching object, the example to the left can be used. In this project, information will be printed to the Brain's screen. The Brain will print when an object is detected by the Distance Sensor and the velocity of that object in meters/second. To more accurately see those changing numbers, the print precision is set to 0.1. 

Test this project by moving a cube closer and further away from the sensor. When the object and/or sensor are moving away from one another, the velocity values will be negative.

Determine object relative size

VEXcode EXP blocks project that uses a Distance Sensor and Print blocks to report an object's size live. The project consists of a When started block followed by a Forever loop that contains 13 commands. The first three commands in the Forever loop read Clear all rows on Brain, set cursor to row 1 column 1 on Brain, and print Object Size on Brain and set cursor to next row. Next is an If block that reads If Distance7 found an Object? Inside this first If block there is a second one connecteed to an Else if statement and an Else statement. These inner If statements together read If Distance7 object size is small then print Small on Brain, else if Distance7 object size is medium then print Medium on Brain, and else print Large on Brain. The first If block is followed by an Else statement reading else print No object found on Brain. Lastly, outside of both If statements there is a Wait block reading Wait 0.25 seconds.

This feature allows your robot to identify an object as being small, medium, or large depending on the sensor’s reading.

This example to the left uses [If then else] and [Print] blocks to print the relative size of an object on the Brain. Place various objects in front of the sensor and look at the readings on the Brain's screen to see the size identification in real time.

To determine the relative size of an object, the sensor uses information about the amount of light being reflected back at the sensor. Objects should be placed between 100mm and 300mm (about 4-12 inches) away from the sensor to get the most accurate representation of the size.

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

Last Updated: