Vex Ultrasonic Sensor

Today we were given ultrasonic sensors and given the tasks of understanding how it works, how to use it, and document it.



The Ultrasonic Sensor has two 3-pin cables, both connected to the analog pins on the Vex Cortex. Similarly to the shaft encoders, we use “SensorValue()” to figure out the value the sensor measures. However, instead of degrees, it measures the distance an object in front of it is, and is able to measure objects 3 centimeters to 3 meters away, otherwise the sensor returns a value of -1. The sensor uses sound waves at a frequency of 40kHz, higher than the human hearing range of 20 Hz to 20 kHz, thus giving the illusion of working “silently”. 

We could use the sensor to warn a robot we use about when it is close to a wall, to avoid any collisions. For example, if we were to use an “if” statement:


if(SensorValue(sonarCM) < 20)





then the robot executes “EAT” when it finds an object less than 20 cm away from the sensor.


As said above, it is able to detect objects 3cm to 3m away, and sends signals at 40kHz. It has precision great enough that it is able to detect a pole of 3cm diameter at a range of 2 meters, using a piece similar to a microphone to read the sound waves that bounce back to calculate distance. However, a flaw with this system is that due to its dependency on sound waves, if the surface it is trying to detect is a material that partially or completely absorbs/refracts sound waves, it either: Weakens the signal to where it may not be readable, or results in no signal at all, making the Ultrasonic Sensor not completely reliable.


Here is an example of a code I made that utilizes the ultrasonic sensor on the premade Vex Squarebots.

The first program uses the sensor very simply. It will travel forward until it sees an object 20 cm away, then turns 180 degrees and drives forward. Meanwhile, the second one is slightly more complicated. It travels until it sees a wall/object in close proximity, then looks left and right and determines the side of a greater distance, then travels in that direction.

Ultrasonic Sensor Example Code

Ultrasonic Sensor Example Code 2