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 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Raspberry Pi Pico W |
|
Wiringď
Codeď
Note
Open the
21_vl53l0x_module.pyfile under the path ofuniversal-maker-sensor-kit-main/pico/Lesson_21_VL53L0X_Moduleor 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ď
Setting up the I2C Interface:
The code begins by importing necessary modules and initializing the I2C communication. The
machinemodule is used to set up I2C with the correct pins of the Raspberry Pi Pico W.For more information about the
vl53l0xlibrary, 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())
Creating VL53L0X Object:
An object of
VL53L0Xclass is created. This object will be used to interact with the VL53L0X sensor.tof = VL53L0X(i2c)
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)
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)
Continuous Measurement Loop:
The sensor continuously measures the distance and prints it. The
ping()method ofVL53L0Xclass 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)