Labs / Bipes + MicroPython / 7 Sensors: Enable Programs to "Perceive the World"

7.15 Measure Distance Without Touching? Precision Non-Contact Ranging Tips! β€”β€” Laser Ranging Module Based on VL53L0X Chip

Author: Li Zisheng Β· Source: FreakStudio open documentation Β· License: CC BY 4.0

Contents

Young makers, let’s unlock an extremely cool module of "installing a laser ruler on the development board" πŸ“ right now!

Normally we use a ruler to measure things, and today this laser ranging module based on the VL53L0X can emit laser just like a laser pointer, accurately measure the distance of the object in front, enabling the development board to "perceive" the distance of the world, and it is indispensable for making obstacle avoidance cars and automatic doors✨!

1. Module Introduction

Want to let your development board know "how far the object in front of me is"? The VL53L0X laser ranging module is exactly designed for this purpose!

It is a Time-of-Flight (ToF) laser ranging sensor: the module emits a laser beam, which reflects back after hitting an object; the chip calculates the time it takes for the laser to "go out β†’ come back" to work out the distance to the object (in millimeters), with an accuracy of up to Β±3mm, which is far more precise than ordinary infrared ranging sensors!

Compared with the infrared ranging module, it has the following features:

In graphical programming, these blocks are commonly used for the laser ranging module:

In practical projects, it is often used for the following purposes:

Quick Science: What is Time of Flight (ToF)?

Just like when you shout into a valley, you can calculate the distance to the valley based on the time it takes for the echo to reach you. After the VL53L0X emits a laser, it calculates the round-trip time of the laser, multiplies that by the speed of light, and then obtains the distance (distance = time Γ— speed of light Γ· 2). Because the speed of light is extremely fast, it can accurately measure distances down to the millimeter level!

2. Usage of Basic Building Blocks

2.1 Initialization Block (Init VL53L0X: I2C. .., SCL. .., SDA. ..)

Once the sensor finishes initialization, it’s time to use this block to β€œfetch the parcel” β€” that is, read out the distance data measured by the VL53L0X for use in the program!

7.15 Measure Distance Without Touching? Precision Non-Contact Ranging Tips! β€”β€” Laser Ranging Module Based on VL53L0X Chip

First, figure out: How do reading blocks fetch data?

VL53L0X is a "Time-of-Flight" ranging sensor. After initialization, it automatically emits laser pulses in real time, calculates the time elapsed from the laser being transmitted to its return, then converts this into a distance value and temporarily stores it in its internal buffer. This building block acts like a "courier" for the development board, specifically tasked with retrieving the "distance package" from the sensor's buffer, and reads the latest distance measurement from the sensor, with the unit being millimeter (mm).

Chinese Meaning: Read the time-of-flight ranging value of the VL53L0X (Time of Flight) sensor, and return the currently detected target distance (in millimeters).

When the program runs to this block, it will retrieve the distance data from the VL53L0X cache and return a specific number (e. g., 50,200,1000): the smaller the value, the closer the target is to the sensor; the larger the value, the farther the target is from the sensor. This number can be directly used for printing, displaying on the screen, or making conditional judgments (e. g., "trigger an alarm when the distance is less than 100mm").

Tips

  1. Do not reverse the order! If you read the distance first and then initialize the sensor, you will get invalid data (such as 0);
  2. Avoid blocking the laser port! The laser transmitting/receiving window of the sensor must not be blocked by dust or fingers, otherwise it will result in inaccurate ranging or complete failure;
  3. Reference for the measurement range! The VL53L0X can measure distances from 0 to 2000 mm, making it suitable for high-precision short-range distance measurement (e. g., obstacle avoidance for robotic cars, object positioning).

✨ Interactive Quiz: Quick Response Challenge

Slowly move your hand closer to the laser opening of the VL53L0X sensor. Will the read distance value increase or decrease? (Answer: Decrease! As the distance between your hand and the sensor gets shorter, the laser time-of-flight becomes shorter, so the converted distance value will also be smaller~)

2.2 ToF Ranging Read Block (Time of Flight)

After the sensor completes initialization, it's time to use this block to "pick up the parcel" β€” that is, read out the distance data measured by the VL53L0X for use in the program!

7.15 Measure Distance Without Touching? Precision Non-Contact Ranging Tips! β€”β€” Laser Ranging Module Based on VL53L0X Chip

First, let's figure out: How do reading blocks get data?

VL53L0X is a "time-of-flight" ranging sensor. After initialization, it automatically and continuously emits laser pulses in real time, calculates the time elapsed from the laser being transmitted to its return, then converts this time into a distance value and temporarily stores it in its internal buffer. This building block acts like a "courier" for the development board, specifically retrieving the "distance package" from the sensor's buffer, and reads the latest distance measurement from the sensor, with the unit being millimeters (mm).

Chinese meaning: Read the time-of-flight ranging value of the (Time of Flight) VL53L0X sensor, and return the currently detected target distance (in millimeters).

When the program runs to this block, it will retrieve the distance data from the VL53L0X cache and return a specific number (e. g., 50,200,1000): the smaller the value, the closer the target is to the sensor; the larger the value, the farther the target is from the sensor. This number can be directly used for printing, displaying on the screen, or making conditional judgments (e. g., "trigger an alarm when the distance is less than 100mm").

Tips

  1. Do not reverse the order! If you read the distance first and then initialize the sensor, you will get invalid data (such as 0);
  2. Avoid blocking the laser port! The laser transmitting/receiving window of the sensor must not be blocked by dust or fingers, otherwise it will result in inaccurate ranging or complete failure;
  3. Reference for the measurement range! The VL53L0X can measure distances from 0 to 2000 mm, making it suitable for high-precision short-range distance measurement (e. g., obstacle avoidance for robotic cars, object positioning).

✨ Interactive Quick Quiz

When you slowly move your hand closer to the laser aperture of the VL53L0X sensor, will the read distance value increase or decrease? (Answer: Decrease! Because the distance between your hand and the sensor gets shorter, the laser time-of-flight becomes shorter, and the converted distance value will be smaller~)

3. Wiring Instructions

For the laser ranging module to "perceive" distances correctly, the first step remains the same: connect the module properly!

7.15 Measure Distance Without Touching? Precision Non-Contact Ranging Tips! β€”β€” Laser Ranging Module Based on VL53L0X Chip

We will connect the VL53L0X-based laser ranging module to the I2C0 interface of the development board, and the corresponding relationship is as follows:

Development Board Pins

Module Pins

Description

Pin 4

SDA

I2C data transmission line, corresponding to I2C0 's SDA

Pin 5

SCL

I2C clock signal line, corresponding to I2C0 's SCL

πŸ“Œ Note

4. Install the VL53L0X library function

Before using graphical building blocks, you need to install the driver library for VL53L0X first:

7.15 Measure Distance Without Touching? Precision Non-Contact Ranging Tips! β€”β€” Laser Ranging Module Based on VL53L0X Chip
  1. Click the "Install vl53l0x library" button to complete the installation;
  2. After the installation is complete, restart the editor and you will see Init VL53L0X and other exclusive blocks.

5. Example: Measure the distance in real time and print it out

Experimental Objective: Continuously measure the distance of the object in front, print the value to the terminal in real time, and intuitively experience the accuracy of laser ranging.

Everyday Thought: Robot vacuums rely on similar laser ranging sensors to avoid furniture, so let’s build our own tiny laser tape measure right now!

The graphical program is as follows:

7.15 Measure Distance Without Touching? Precision Non-Contact Ranging Tips! β€”β€” Laser Ranging Module Based on VL53L0X Chip

Building Block Assembly Steps:

  1. Initialize the sensor
    • Drag in the Init VL53L0X block;
    • Set I2C to 0 (corresponding to the I2C0 interface);
    • SCL selects Pin 7 / GPIO5, SDA selects Pin 6 / GPIO4, which matches the wiring.
  2. Infinite Loop Measurement
    • Drag in the repeat while true block to keep the program running continuously.
  3. Read and print the distance
    • Drag the print block into the loop, select the Time of Flight block as its content, and the current distance value (unit: millimeter) will be output directly;
  4. Add Delay
    • Drag a delay block to the end of the loop and set it to 200 milliseconds β†’ this controls the measurement frequency and prevents the terminal from being flooded with messages.

Experimental Effect:

The graphical blocks will be automatically converted into MicroPython code adapted for VL53L0X, and the running result will be displayed:

7.15 Measure Distance Without Touching? Precision Non-Contact Ranging Tips! β€”β€” Laser Ranging Module Based on VL53L0X Chip

Directly importable building blocks:

Laser ranging module.xml 2 KB

Tips

  1. If the distance value cannot be read:
    • Check whether the SCL and SDA pins are connected correctly (Pin7/GPIO5, Pin6/GPIO4) and whether the wiring is secure;
    • Confirm that the VL53L0X library has been installed correctly, then restart the editor and try again;
    • Avoid exposing the module to direct strong light, and keep the laser window clean.
  2. Numerical display 8190 What should I do?
    • This indicates that the object is beyond the measurement range (>2 meters) or no object is detected. Simply move the object to a position within the range of 30 mm to 2 meters.
  3. Advanced Gameplay:
    • Pair it with the TM1637 digital tube module to display the distance value on the digital tube, creating a "visual laser ruler";
    • Add conditional judgment: when the distance is less than 100 millimeters, turn on the LED to simulate the obstacle avoidance function of the car.

✨ Interactive Mini Quiz

  1. Think about it: Why is laser ranging more accurate than infrared ranging? Will the readings be inaccurate if you use infrared ranging to measure a black object?
  2. Try it: Change the delay time from 200 milliseconds to 100 milliseconds and see if the measurement speed becomes faster. Then change it to 500 milliseconds to experience different measurement frequencies.

Summary

  1. The laser ranging module based on VL53L0X is a Time-of-Flight (ToF) sensor that calculates distance based on the round-trip time of laser.
  2. Before use, please complete the I2C wiring and library installation as two preparatory steps to ensure normal communication;
  3. It boasts the advantages of high precision, fast response, and immunity to color interference, making it the top choice for obstacle avoidance and distance measurement projects.
  4. The basic usage is to "read + print" the distance value, while the advanced usage is to cooperate with peripherals to realize automatic control.