Distance

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

Python

d = BrainPad.Distance.Read(1, 2)

C#

var d = BrainPad.Distance.Read(1, 2);

JS

let d = await BrainPad.Distance.Read(1, 2);

Single Pin Sensors

Some Distance sensors use a single pin for measuring distance. The code is the 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.

Python

d = BrainPad.Distance.Read(2, -1)

C#

var d = BrainPad.Distance.Read(2, -1);

JS

d = await BrainPad.Distance.Read(2, -1);

BrainStorm

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
Newsletter

Twitter Feed
Hot off the press!
January 30, 2024
December 14, 2023
December 11, 2023
December 8, 2023
November 24, 2023