tutorialspinouttools
Sensors · ESP32-C3

VL53L0X on OLED: Laser Time-of-Flight Distance

Free ESP32 & Arduino tutorial · wiring, code & parts · gokul.builds

Overview & Wiring

What it is: The VL53L0X measures distance with a tiny invisible laser, timing how long light takes to bounce back (time-of-flight). It's accurate from ~3 cm to ~1.2 m and, unlike an ultrasonic sensor, has a narrow beam and doesn't care about soft surfaces. How it works: I2C device at address 0x29. The ST/Adafruit library fires the laser and returns the range in millimetres. Shares SDA/SCL with the OLED. Wiring (shared I2C bus with the OLED): VL53L0X SDA -> GPIO4 VL53L0X SCL -> GPIO5 VL53L0X VIN -> 3V3 VL53L0X GND -> GND OLED VCC=3V3, GND=GND, SDA=GPIO4, SCL=GPIO5 Libraries: "Adafruit VL53L0X" (installs "Adafruit BusIO"), plus "Adafruit SSD1306" and "Adafruit GFX Library". On screen / Serial: distance in mm, or "out of range" when nothing is in view. Move your hand toward and away from it. Gotchas: Keep the small window clean and unobstructed. Bright sunlight and shiny/mirror surfaces reduce range. Address is 0x29; to use several, you toggle each one's XSHUT pin and reassign addresses (advanced).

Circuit Diagram

VL53L0X on OLED: Laser Time-of-Flight Distance wiring diagram for ESP32-C3

Parts You Need

ESP32-C3 SuperMiniBuy →
0.96" SSD1306 OLEDBuy →

Source Code (Arduino / ESP32)

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_VL53L0X.h>

Adafruit_SSD1306 display(128, 64, &Wire, -1);
Adafruit_VL53L0X lox = Adafruit_VL53L0X();

void setup() {
  Serial.begin(115200);
  Wire.begin(4, 5);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  if (!lox.begin()) {
    Serial.println("VL53L0X not found");
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0, 28); display.println("VL53L0X not found");
    display.display();
    while (1) delay(10);
  }
}

void loop() {
  VL53L0X_RangingMeasurementData_t measure;
  lox.rangingTest(&measure, false);

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("VL53L0X Laser");

  if (measure.RangeStatus != 4) {              // 4 = out of range
    int mm = measure.RangeMilliMeter;
    display.setTextSize(3);
    display.setCursor(0, 22);
    display.printf("%d", mm);
    display.setTextSize(1);
    display.setCursor(0, 54); display.print("mm");
    Serial.printf("Distance: %d mm\n", mm);
  } else {
    display.setTextSize(2);
    display.setCursor(0, 28); display.println("out of range");
    Serial.println("out of range");
  }
  display.display();
  delay(150);
}
Browse all tutorials ESP32-C3 pinout Calculators