Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 21: Time of Flight Micro-LIDAR Distance Sensor (VL53L0X)

In this lesson, you will learn how to use the Adafruit VL53L0X Time of Flight Distance Sensor with an ESP32 Development Board. We’ll cover initializing the sensor, reading distance measurements, and displaying them in millimeters on the serial monitor.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

ESP32 & Development Board (ESP32 Board)

BUY

Time of Flight Micro-LIDAR Distance Sensor (VL53L0X)

BUY

Breadboard

BUY

Wiring

../_images/Lesson_21_VL53L0X_esp32_bb.png

Code

Note

To install the library, use the Arduino Library Manager and search for “Adafruit_VL53L0X” and install it.

Code Analysis

  1. Including the necessary library and initializing the sensor object. We start by including the library for the VL53L0X sensor and creating an instance of the Adafruit_VL53L0X class.

    Note

    To install the library, use the Arduino Library Manager and search for “Adafruit_VL53L0X” and install it.

    #include <Adafruit_VL53L0X.h>
    Adafruit_VL53L0X lox = Adafruit_VL53L0X();
    
  2. Initialization in the setup() function. Here, we set up serial communication and initialize the distance sensor. If the sensor can’t be initialized, the program halts.

    void setup() {
      Serial.begin(115200);
      while (!Serial) {
        delay(1);
      }
      Serial.println("Adafruit VL53L0X test");
      if (!lox.begin()) {
        Serial.println(F("Failed to boot VL53L0X"));
        while (1)
          ;
      }
      Serial.println(F("VL53L0X API Simple Ranging example\n\n"));
    }
    
  3. Capturing and displaying the measurements in the loop() function. Continuously, the ESP32 Development Board captures a distance measurement using the rangingTest() method. If the measurement is valid, it’s printed to the serial monitor.

    void loop() {
      VL53L0X_RangingMeasurementData_t measure;
      Serial.print("Reading a measurement... ");
      lox.rangingTest(&measure, false);
      if (measure.RangeStatus != 4) {
        Serial.print("Distance (mm): ");
        Serial.println(measure.RangeMilliMeter);
      } else {
        Serial.println(" out of range ");
      }
      delay(100);
    }