Laser Ranging Sensor - VL53L0X

This sensor can be used to accurately measure the distance to an object using a laser and photon detector.

For circuit, pin and interface specification see this datasheet

Usage
For basic ranging the sensor is put into ranging mode by writing to the ranging address and then waiting for the ranging to be completed. The measured distance can then be read from the ranging result address as a

Extra information
Reference code for communicating with the sensor can be found at the following links

Sample code
As default the sensor will be accessible on i2c address 0x29. This means that multiple sensor can not coexist on the i2c bus without changing their addresses. In order to change the addresses, it is necessary to hook up the SHDN pin of the sensor to a gpio pin so that it can be controlled. The shutdown pin turns the sensor off when it is pulled low. The address of the sensor can be changed by writing a new address value (should be between 0x29 and 0x7F, an easy way to do it is to start from 0x30 and count upwards) to the internal address 0x8A.

pseudo code

  • Shutdown all sensors by driving their SHDN pins low
  • for each sensor
    • drive its SHDN pin high
    • write 0x30 + i to address 0x8A

Javascript code
//get the gpio module so that we can control SHDN pins
var gpio = context.getModule("gpio"); 

//array holding the names of the sensors SHDN pins
var sensors = ["GPIO 0", "GPIO 4", "GPIO 5", "GPIO 21"];

//get a single device reference that we can always use to
//write to a sensor with the default address
var sensor = context.getModule("i2c").getDevice(1, 0x29);

//turn off all the sensors by driving their SHDN pins low
for (var i = 0; i < sensors.length; i++) {
    gpio.setValue(sensors[i], false);
}

//one by on turn one the sensors and assign them a new
//address starting from 0x30
for (var i = 0; i < sensors.length; i++) {
    gpio.setValue(sensors[i], true);
    sensor.write(0x8A, 0x30 + i);
}

For doing a simple single measurement with the sensor, it is enough to just write a 1 to the sensors internal register address 0x00, then just wait for the ranging to be done before reading the measured value. The sensor will signal that it is done ranging by writing a 1 to it’s internal register address 0x14. Once the ranging is complete, the distance (in mm) can be gotten as a unsigned 16 bit integer by reading internal register address 0x1e and 0x1f.

pseudo code

  • initiate measuring by writing to sensor
  • wait for sensor to complete ranging
  • read the measurement and assemble the value into a single integer

Javascript
//get a reference to the device on i2c, in this case on the 
//standard address 0x29
var sensor = context.getModule("i2c").getDevice(1, 0x29);

//write the start ranging command to the sensor
sensor.write(0x00, 0x01);

var count = 0;
var value = 0;
//continuously poll the sensor to see if it has completed its ranging
//keep track of how many times it's been polled to avoid infinite loops
while (++count < 100) {
    context.sleep(10);
    value = sensor.read(0x14);

    //if the read value indicates a successfully completed ranging, stop polling
    if ((value & 0x01) == 0x01) {
        break;
    }
}

//if the read wasn't successful throw an error to indicate this
if ((value & 0x01) != 0x01) {
    // Not ready
    throw "Device not ready";
}

//read the 2 bytes from the sensor that hold the measurement
var data = sensor.read(0x1e, 2);

//assemble the 16 bit integer by shifting the first byte up and or-ing them together
var distance = (data[0] << 8) | data[1];