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 About 15 minutes to build and run. 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

The circuit runs here once the page has loaded.

What you'll learn

  • Time of flight distance sensing
  • I2C on A4 and A5
  • Continuous readings and timeouts
  • Handling out of range values
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

A VL53L0X is a small laser distance sensor. It sends a pulse of infrared light, times how long it takes to come back, and reports the distance in millimeters. It uses I2C: SDA to A4, SCL to A5, with 5 V on VIN.

Light crosses a meter in about 3.3 nanoseconds, so the chip times it with a grid of single photon detectors. The Pololu VL53L0X library handles all that and starts continuous measuring.

Every 200 ms the sketch prints the distance. It reaches about 1.2 m in this mode. A reading of 8190 or more means nothing came back, and the sketch prints "out of range".

Parts list

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

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
VL53L0X time-of-flight sensor pin VINArduino Uno R3 pin 5V
VL53L0X time-of-flight sensor pin GNDArduino Uno R3 pin GND
VL53L0X time-of-flight sensor pin SDAArduino Uno R3 pin A4
VL53L0X time-of-flight sensor pin SCLArduino Uno R3 pin A5

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);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Starts serial and Wire, sets a 500 ms timeout, and calls sensor.init(). If the sensor is not found, it prints a message and stops forever. Otherwise it calls startContinuous() and prints "Laser ruler ready".

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");
}

loop sketch.ino

Reads the distance in millimeters. It prints "timeout" if the read timed out, "out of range" if the value is 8190 or more, and otherwise prints the distance. Then it waits 200 ms.

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);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • "Failed to detect and initialize sensor!" This project
    Check SDA on A4, SCL on A5, and VIN and GND.
  • It says out of range close up This project
    Aim at a flat surface within about 1.2 m.
  • The sensor is not found, or two sensors clash. VL53L0X time-of-flight sensor
    The default I2C address is 0x29 and it is the same on every VL53L0X. For two or more, hold the XSHUT pins low, then bring each up in turn and set a new address in code.
  • Distances are wrong or read about 8190 mm. VL53L0X time-of-flight sensor
    The range is about 30 mm up to 1.2 m, shorter in bright light or on dark targets. A reading near 8190 usually means out of range or a timeout. It is a 2.8 V part, so use a board with a regulator.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

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