Built-in project · Arduino Uno R3

Laser ruler on an Arduino Uno

A VL53L0X time-of-flight sensor over I2C, read with Pololu’s library: millimeters to the target, and “out of range” past about 1.2 m. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

Step by step, with the wiring and the common mistakes: Arduino VL53L0X Distance Sensor: Wiring and Code

Laser ruler · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Laser ruler: a VL53L0X measures how far away the target is by timing a
// pulse of infrared laser light there and back, read with Pololu's library.
//
//   VL53L0X   VIN -> 5 V   GND -> GND   SDA -> A4   SCL -> A5
//
// Light covers a meter in about 3.3 nanoseconds, so the chip times the trip
// with a grid of single-photon detectors rather than a clock, and reports the
// result in millimeters. In its default mode it reaches about 1.2 m; past
// that it reports 8190, which means nothing came back.
//
// Drag the slider on the sensor to move the target.

#include <Wire.h>
#include <VL53L0X.h>

VL53L0X sensor;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  sensor.setTimeout(500);
  if (!sensor.init()) {
    Serial.println("Failed to detect and initialize sensor!");
    while (1) {
    }
  }
  sensor.startContinuous();
  Serial.println("Laser ruler ready");
}

void loop() {
  uint16_t mm = sensor.readRangeContinuousMillimeters();
  if (sensor.timeoutOccurred()) {
    Serial.println("timeout");
  } else if (mm >= 8190) {
    Serial.println("out of range");
  } else {
    Serial.print("distance ");
    Serial.print(mm);
    Serial.println(" mm");
  }
  delay(200);
}

Parts list

3 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.

How it is wired

4 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 5V; VL53L0X time-of-flight sensor pin VIN
  • Ground: Arduino Uno R3 pin GND; VL53L0X time-of-flight sensor pin GND
  • Arduino Uno R3 pin A4; VL53L0X time-of-flight sensor pin SDA
  • Arduino Uno R3 pin A5; VL53L0X time-of-flight sensor pin SCL

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.