DUE Distance


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 ReadDistance() function is used. The 2 needed arguments are the 2 pins, trigger and echo. The returned value is measured distance in centimeters.

d = Distance(1, 2) # Read sonar with trig connected to pin 1 and echo connected to pin 2

Single Pin Sensors

Some Distance sensors use a single pin for measuring distance. The code is same as before, except the second argument must be -1 to indicate that the sensor is a single pin sensor.

This code reads distance on a single wire sensor connected to pin 2.

d = Distance(2, -1)


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?

Content Licensing

Twitter Feed
Hot off the press!