A distance sensor works similar to how bats navigate and find bugs. They produce soundwaves that bounce off objects in their environment.

A distance sensor can be used to detect how far away an object is. These are often used in robots to detect objects and avoid obstacles.

The majority of distance sensors look like the image above. They use 2 pins to detect the distance, where a pulse is sent to the Trig pin and Echo is enabled when the sound is returned. Gnd is ground (negative) and Vcc is the voltage source, which must be 5V. Very few and rare distance sensors work at 3.3V. The sensor will not be damaged at 3.3V, but it will not work.


When using a distance sensor, the Distance() function element is used. The Distance() function requires 2 arguments: the first one is the trigger pin and the second is the echo pin.

Distance(triggerPin, echoPin)

The In() function is used to return the sensors value in centimeters.

var dist = Distance(P0,P1);
centimeters = In(dist);
dist = Distance(P0,P1)
centimeters = In(dist)


Can you think of an animal that uses sound to “see” in the dark? Did we use bats as an inspiration to create distance sensors?