Using the IQ Distance Sensor (2nd gen)

The IQ Distance Sensor (2nd gen) is one of the powerful IQ sensors which are designed for complete integration with the IQ 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 IQ (2nd gen) Distance Sensor piece.


Description of the Sensor

The IQ Distance Sensor (2nd gen) 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 IQ.
  • 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 Beam piece with two Blue Pins.

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.

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 IQ Brain connected to a (2nd gen) Distance Sensor.

In order for the Distance Sensor (2nd gen) to be functional with the IQ Brain, the sensor’s Smart Port and an IQ Brain’s Smart Port need to be connected with a Smart Cable.

The sensor will work with any of the 12 Smart Ports on the IQ Brain.

When connecting an IQ 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 (2nd gen) Works

The IQ Distance Sensor (2nd gen) 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 (2nd gen) needs to be paired with VEXcode IQ to create a user program for the IQ Brain to utilize the sensor’s readings to control the robot’s behavior.

The IQ Brain in concert with a user project can be used to convert the Distance Sensor (2nd gen) 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 (2nd gen)

Placement

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

The placement of the Distance Sensor (2nd gen) 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 (2nd gen) 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 IQ Brain to see the information the Distance Sensor (2nd gen) is reporting.

From the Sensor Dashboard, the Distance Sensor (2nd gen) 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 (2nd gen) as a Device in VEXcode IQ

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

VEXcode IQ Devices menu after the Add a device button has been selected. The Distance (2nd gen) option is highlighted.

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

To add the Distance Sensor (2nd gen) to the configuration, follow the steps in this article. 

Screenshot of VEXcode IQ 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 (2nd gen) blocks are highlighted.

Once the Distance Sensor (2nd gen) 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 (2nd gen), view the Help information (Blocks project or C++ project).


Common Uses of the Distance Sensor (2nd gen)

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

Detect an object

VEXcode IQ 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 (2nd gen). The Distance Sensor (2nd gen) 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 (2nd gen) 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 IQ 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 (2nd gen) 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 IQ (2nd generation) Brain. See this article for more information about using the Sensor Dashboard.

Report object velocity

VEXcode IQ 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 (2nd gen) 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 IQ 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.


Uses of the Distance Sensor (2nd gen) on a Competition Robot

The Distance Sensor (2nd gen) will provide a superior competitive advantage for competition robots. The ability to detect distance to the perimeter wall and be able to measure a robot's velocity will provide a great deal of information when designing autonomous routines. Object detection and determining the object’s relative size will provide helpful information with the detection of game pieces and or goals.

Fling Hero Bot build that has an attached Distance Sensor that is highlighted and pointing behind the robot.

For example, in the 2021 - 2022 VEX IQ Challenge, Pitching In, a Distance Sensor (2nd gen) could be mounted on the back of your robot to detect walls when driving in reverse.

This can help automate movements, especially when retrieving a ball into the intake, driving in reverse, and then turning to launch the ball into the goal.

No matter what the application the VEX IQ Distance Sensor (2nd gen) is used for, there is no doubt it will be a welcomed addition for teams.

The function of the sensor’s values are open for the imagination of the user.

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

Last Updated: