Using the VEX IQ (1st gen) Distance Sensor

Description

This sensor reports the distance between it and the nearest solid object (50mm - 1m range). It calculates distance by using the time it takes for ultrasonic waves to bounce off of an object and return to the sensor.


How the Distance Sensor Works: Listening for Distance

The Distance Sensor uses the same principles as sonar devices to measure sound. It sends out a tiny pulse of ultrasound with its emitter at one side of the sensor, and then times how long it takes to hear the sound reflect back the receiver at the other side of the sensor. The sensor calculates the distance based on how long the sound pulse took to make the round trip. The Distance Sensor then makes a calculation based on the speed of sound to tell the robot brain how far the nearest object is. 

If the surface is not flat like a curved wall or a ball, the ultrasonic wave does not bounce off of all parts equally or at the same time. The closest surface large enough to reflect back a detectable amount of the wave determines the detected distance of the object.


Common Uses of the Distance Sensor:

This sensor can detect if there is an object in front of it.

This sensor can measure the distance between it and an object.

  • A robot can use this sensor to drive a certain distance away from an object.

  • A robot can use this sensor to drive up to an object without touching it.

  • The values provided by this sensor can be used to adjust the robot’s velocity depending on whether it is near or far away from an object or surface.

Uses of the Distance Sensor on a Competition Robot

  • The robot can use the Distance Sensor to initiate a sequence of behaviors when it is in the correct place to do so. For example, the robot can drive forward until it detects that it is 150mm away from a wall, then turn 90 degrees, and release an object into a scoring zone.  
  • The Distance Sensor can be used to trigger robot actions, such as picking up a game piece, when an object is detected within range.
  • The robot can use the Distance Sensor to drive toward an object or surface until it is within a specific distance, without touching it. This can be used to make sure that the robot does not knock over game pieces.
  • A robot can use the Distance Sensor to drive away from an object or surface until it has reached a specific distance. This can be used to avoid collisions with other robots on the field.

Using the Distance Sensor in VEXcode IQ

Adding the Distance Sensor as a Device in VEXcode IQ

Diagram illustrating the various sensors used in the VEX IQ Robotics platform, highlighting their functions and connections within the system for educational purposes.

To code the Distance Sensor in VEXcode IQ, you must first configure the Distance Sensor. View this article to learn more about configuring a sensor in VEXcode IQ.

Once the sensor is configured, commands will appear in the Toolbox that you can use in your project.

Coding the Distance Sensor in Blocks

Diagram illustrating the various sensors available for the VEX IQ Robotics platform, highlighting their functions and applications in educational robotics projects.

The <Object found> block is a Boolean reporter block that reports a condition as either true or false. Boolean blocks, like the <Object found> block fit inside blocks with hexagonal (six-sided) inputs for other blocks.

The <Object found> Boolean block reports 'true' if the sensor detects an object, and 'false' if the sensor does not. To learn more about Boolean blocks visit the Help or the Block Shapes and Meaning article.

 

Diagram illustrating various sensors used in the VEX IQ Robotics platform, highlighting their functions and connections within the system, designed for educational purposes and beginner robotics enthusiasts.

In this example, the <Object found> block is used with a [Wait until] block to make the robot drive forward until the Distance Sensor detects an object. When the sensor detects an object, the robot will stop driving, as shown in the image above. 

Diagram illustrating various sensors used in the VEX IQ Robotics platform, showcasing their placement and connections for educational robotics projects.

The (Distance from) block reports the distance of the nearest object from the Distance Sensor. It reports a range from 24mm to 1000mm, or 1 inch to 40 inches. The (Distance from) block is a reporter block that is used inside blocks with circular spaces.

 

Diagram illustrating the various sensors used in the VEX IQ Robotics platform, highlighting their functions and connections for educational purposes in robotics.

In this example, the (Distance from) block is used with a [Print] block to display on the Brain screen the distance between the Distance Sensor and an object. The (Distance from) block is also shown inside a <Less than> block which is inside a {Wait until} block. This will cause the robot to drive until the Distance Sensor detects an object less than 50 millimeters away, and then stop driving. This is shown in the first video above.

Coding the Distance Sensor in Python

Note: To code a VEX IQ (1st generation) Distance Sensor in Python, it must be connected to a VEX IQ (2nd generation) Brain. The VEX IQ (1st generation) Brain does not support Python.

Diagram illustrating the various sensors used in the VEX IQ Robotics platform, showcasing their placements and functions for educational robotics projects.

The  command reports a Boolean value of either true or false about the Bumper Switch. 

The distance.is_object_detected command reports 'true' if an object is detected, and 'false' if an object is not detected.

Note: The name of the Distance Sensor that appears in the command corresponds to the name it is given in the configuration. 

 

Diagram illustrating the various sensors used in the VEX IQ Robotics platform, highlighting their functions and connections for educational robotics projects.

In this example, a While loop with a not condition is used with the distance.is_object_detected command to make the robot drive forward until the Distance sensor detects an object. When the sensor detects an object, the robot will stop driving, as shown in the image above.

Diagram illustrating the various sensors available for the VEX IQ Robotics platform, highlighting their functions and connections in a robotics system, aimed at educational purposes for beginners and enthusiasts.

The distance.distance command reports the distance of the nearest object from the Distance Sensor. It reports a range from 24mm to 1000mm, or 1 inch to 40 inches.

Diagram illustrating the various sensors used in the VEX IQ Robotics platform, highlighting their placement and functionality for educational robotics projects.

In this example, a While loop with a not condition is used with the distance.is_object_detected command to make the robot drive forward until the Distance Sensor detects an object. When the sensor detects an object, the robot will stop driving.

In this example, the distance.distance command is used with a brain.screen.print command to display on the Brain screen the distance between the Distance Sensor and an object. The distance.distance command is also shown inside a While loop with a not condition. This will cause the robot to drive until the Distance sensor detects an object less than 50 millimeters away, and then stop driving. This is shown in the first video above.

To learn more about coding a Distance Sensor in Python, check out Unit 5 of the Computer Science Level 1 - Python course. 

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

Last Updated: