Skip to content

Proximity Sensor

The Proximity sensor class provides access to a proximity sensor or range finder.

Creates a new Proximity object instance.

Proximity(options)

options

An object of properties used to construct the class.

sensor - The hardware connection definition.

interval (optional) - The sampling interval in milliseconds.

Returns a sample object containing the current proximity data.

sample()

An object with the following properties:

near - A boolean that indicates if a proximate object is detected.

distance (optional) - A number that represents the distance to the nearest sensed object in centimeters.

max (optional) - A number that represents the maximum sensing range of the sensor in centimeters.

const sensor = new device.sensor.Proximity({
sensor: {
io: device.io.I2C,
address: 0x39
}
});
const sample = sensor.sample();
if (sample.near) {
console.log(`Object detected at ${sample.distance} cm`);
}

Proximity