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’ll learn how to use the Raspberry Pi Pico W for measuring distances with the VL53L0X Time of Flight Micro-LIDAR Distance Sensor. We’ll walk you through setting up I2C communication between the Raspberry Pi Pico W and the sensor, and then we’ll explore configuring the sensor’s settings for optimal performance. You will also learn how to adjust the measurement timing budget and VCSEL pulse periods to improve accuracy and range.

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

Raspberry Pi Pico W

BUY

Time of Flight Micro-LIDAR Distance Sensor (VL53L0X)

BUY

Breadboard

BUY

Wiring

../_images/Lesson_21_vl53l0x_bb.png

Code

Note

  • Open the 21_vl53l0x_module.py file under the path of universal-maker-sensor-kit-main/pico/Lesson_21_VL53L0X_Module or copy this code into Thonny, then click “Run Current Script” or simply press F5 to run it. For detailed tutorials, please refer to Open and Run Code Directly.

  • Here you need to use the vl53l0x.py, please check if it has been uploaded to Pico W, for a detailed tutorial refer to Upload the Libraries to Pico.

  • Don’t forget to click on the “MicroPython (Raspberry Pi Pico)” interpreter in the bottom right corner.

import time
from machine import Pin, I2C
from vl53l0x import VL53L0X

print("setting up i2c")
id = 0
sda = Pin(20)
scl = Pin(21)

i2c = I2C(id=id, sda=sda, scl=scl)

print(i2c.scan())

# print("creating vl53lox object")
# Create a VL53L0X object
tof = VL53L0X(i2c)

# Pre: 12 to 18 (initialized to 14 by default)
# Final: 8 to 14 (initialized to 10 by default)

# the measuting_timing_budget is a value in ms, the longer the budget, the more accurate the reading.
budget = tof.measurement_timing_budget_us
print("Budget was:", budget)
tof.set_measurement_timing_budget(40000)

# Sets the VCSEL (vertical cavity surface emitting laser) pulse period for the
# given period type (VL53L0X::VcselPeriodPreRange or VL53L0X::VcselPeriodFinalRange)
# to the given value (in PCLKs). Longer periods increase the potential range of the sensor.
# Valid values are (even numbers only):

# tof.set_Vcsel_pulse_period(tof.vcsel_period_type[0], 18)
tof.set_Vcsel_pulse_period(tof.vcsel_period_type[0], 12)

# tof.set_Vcsel_pulse_period(tof.vcsel_period_type[1], 14)
tof.set_Vcsel_pulse_period(tof.vcsel_period_type[1], 8)

while True:
    # Start ranging
    print(tof.ping() - 50, "mm")

    time.sleep_ms(100)  # Short delay of 0.1 seconds to reduce CPU usage

Code Analysis

  1. Setting up the I2C Interface:

    The code begins by importing necessary modules and initializing the I2C communication. The machine module is used to set up I2C with the correct pins of the Raspberry Pi Pico W.

    For more information about the vl53l0x library, please visit kevinmcaleer/vl53l0x.

    import time
    from machine import Pin, I2C
    from vl53l0x import VL53L0X
    
    print("setting up i2c")
    id = 0
    sda = Pin(20)
    scl = Pin(21)
    i2c = I2C(id=id, sda=sda, scl=scl)
    print(i2c.scan())
    
  2. Creating VL53L0X Object:

    An object of VL53L0X class is created. This object will be used to interact with the VL53L0X sensor.

    tof = VL53L0X(i2c)
    
  3. Configuring Measurement Timing Budget:

    The measurement timing budget is set up. This determines how long the sensor takes to perform a measurement. A longer timing budget allows for more accurate readings.

    budget = tof.measurement_timing_budget_us
    print("Budget was:", budget)
    tof.set_measurement_timing_budget(40000)
    
  4. Setting VCSEL Pulse Periods:

    Here, the pulse periods for the VCSEL (Vertical Cavity Surface Emitting Laser) are set. This affects the range and accuracy of the sensor.

    tof.set_Vcsel_pulse_period(tof.vcsel_period_type[0], 12)
    tof.set_Vcsel_pulse_period(tof.vcsel_period_type[1], 8)
    
  5. Continuous Measurement Loop:

    The sensor continuously measures the distance and prints it. The ping() method of VL53L0X class is used to get the distance in millimeters. A small delay is added to reduce CPU usage.

    while True:
        print(tof.ping() - 50, "mm")
        time.sleep_ms(100)