Skip to main content

Distance Sensor

The LEGO Distance Sensor (45604) measures how far away the thing in front of it is, from about 4 centimetres to 2 metres. Its two eyes also have four lights around them that your program can turn on.

Add the Build HAT library first

BuildHat comes from a library, not from the language, so this code won't build without it. In IntelliJ, choose Tools | Nylon | Add Nylon Library… and pick build-hat. It brings the Raspberry Pi library with it, and adds one line to your project's build.markup.code:

(+dependencies : RepositoryDependency coordinate={"com.grandtheorem:build-hat:0.3.0"} /)

Measure a distance​

Plug the sensor into port B. Any port works, as long as the program names the same one.

Pi is the board, BuildHat is the HAT on top of it, and Port names the HAT's ports.

# Measure distances, then wait for a hand.
namespace distance

import grandtheorem.rpi.Pi
import grandtheorem.buildhat.BuildHat
import grandtheorem.buildhat.Port
import namespace nylon.time

BuildHat.connect starts the HAT, and distanceSensor gives the sensor on a port.

# The Distance Sensor in port B.
entrypoint.default main() {
let hat = BuildHat.connect(Pi)
let sensor = hat.distanceSensor(Port.B)

distance() is in millimetres. It gives nothing when nothing is in range, so if (let away? = …) takes the reading only when there is one.

# Twice a second, for five seconds.
var count = 0
while (count < 10) {
if (let away? = sensor.distance()) {
println("${away} mm")
} else {
println("nothing in range")
}
sleep(500.milliseconds())
count = count + 1
}

waitUntilCloserThan waits until something comes nearer than a distance, for as long as you give it. It answers whether it did, so the program knows which happened.

# Light its eyes when something comes
# within 10 cm, if it does in 10 seconds.
if (sensor.waitUntilCloserThan(100, 10.seconds())) {
sensor.lights(100, 100, 100, 100)
println("something is close")
sleep(2.seconds())
} else {
println("nothing came close")
}

lights(0, 0, 0, 0) turns its lights off again, so they don't stay on after the program ends.

# Turn its lights off before the program ends.
sensor.lights(0, 0, 0, 0)
}

For five seconds it prints a reading every half second, such as 152 mm, or nothing in range. Then it waits for your hand: bring it within 10 cm and the sensor's lights come on for two seconds, then go off.

Everything the sensor can do​

CallWhat it does
hat.distanceSensor(port)The Distance Sensor on a port
sensor.distance()How far away the thing in front of it is, in millimetres, as an i32?
sensor.waitUntilCloserThan(millimetres, timeout)Waits until something is closer, and answers whether it was in time
sensor.waitUntilFartherThan(millimetres, timeout)Waits until the thing in front of it is farther away, and answers whether it was in time
sensor.lights(upperRight, upperLeft, lowerRight, lowerLeft)Lights the four lights around its eyes, each 0 off to 100 full

Good to know​

Nothing in range is not far away. distance() gives nothing both when the nearest thing is closer than about 4 cm and when it's farther than about 2 m, and waitUntilFartherThan doesn't count either as farther.

A reading is taken when you ask for it. Each call asks the sensor once. A sensor that doesn't answer within half a second gives nothing, as an empty range does.

A wait checks every 50 milliseconds. Something that comes close and goes again between two checks can be missed.

The wrong part on the port ends the run. If port B has a motor instead, the run ends and says what's there.

Boards: every board on Boards.