Distance Sensor

The Adafruit VL53L0X is a time-of-flight laser ranging sensor capable of measuring distances up to 2 m. It has a fixed I2C address. See the Adafruit learn guide for wiring and hardware details.

Connection

Call startDistanceSensor() inside preload(). The device is ready to use by the time setup() runs.

let sensor;

function preload() {
  sensor = startDistanceSensor(name?);
}

name string | false | null "distance"

Controls automatic event callback registration. With the default name:

function distanceChanged(event) { /* new reading */ }

Pass false or null to disable and use addEventListener instead.

Methods

getDistance() number

Returns the last measured distance in mm. Returns 8190 when the target is out of range.

Methods (BaseDevice)

connect() Promise<Response>

Opens the USB connection and sends the start command to the firmware. Calling it multiple times is safe — subsequent calls return the same promise. In p5.js you do not need to call this manually; startXxx() calls it for you inside preload().

Properties

Properties (BaseDevice)

id { type, address }

Identifies the device. type is the DeviceType enum value, address is the I2C address the device was started with.

isConnected boolean

true once the USB connection has been established and the firmware has acknowledged the device. In p5.js this is always true by the time setup() runs, because startXxx() waits for the connection inside preload().

Events

changed(event) changed

Fires on every sensor poll with an updated distance reading.

Event Argument

All event callbacks receive a single event argument. The event detail is available at event.detail and contains:

event.detail.distance number — Distance in mm. 8190 means out of range.