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 to connect with a Time of Flight Micro-LIDAR Distance Sensor (VL53L0X). You’ll be guided through setting up the sensor, initializing I2C communication, and measuring distances in real-time. This project will enhance your comprehension of connecting hardware with the Raspberry Pi and utilizing Python for practical applications. Additionally, you’ll delve into adjusting measurement parameters to meet varying accuracy and speed needs.
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 5 |
|
Wiring
Install Library
Note
The adafruit-circuitpython-vl53l0x library relies on Blinka, so please ensure that Blinka has been installed. To install libraries, refer to Insatll Adafruit_Blinka (CircuitPython) - Optional.
Before installing the library, please make sure that the virtual Python environment is activated:
source ~/env/bin/activate
Install adafruit-circuitpython-vl53l0x library:
pip3 install adafruit-circuitpython-vl53l0x
Code
Note
Please ensure that you have installed the Python library required for running the code according to the “Install Library” steps.
Before running the code, please make sure that you have activated the virtual Python environment with blinka installed. You can activate the virtual environment using a command like this:
source ~/env/bin/activate
Find the code for this lesson in
universal-maker-sensor-kit-main/pi/directory, or directly copy and paste the code below. Execute the code by running the following commands in terminal:python 21_vl53l0x_module.py
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Simple demo of the VL53L0X distance sensor.
# Will print the sensed range/distance every second.
import time
import board
import busio
import adafruit_vl53l0x
# Initialize I2C bus and sensor.
i2c = busio.I2C(board.SCL, board.SDA)
vl53 = adafruit_vl53l0x.VL53L0X(i2c)
# Optionally adjust the measurement timing budget to change speed and accuracy.
# See the example here for more details:
# https://github.com/pololu/vl53l0x-arduino/blob/master/examples/Single/Single.ino
# For example a higher speed but less accurate timing budget of 20ms:
# vl53.measurement_timing_budget = 20000
# Or a slower but more accurate timing budget of 200ms:
# vl53.measurement_timing_budget = 200000
# The default timing budget is 33ms, a good compromise of speed and accuracy.
try:
# Main loop will read the range and print it every second.
while True:
print("Range: {0}mm".format(vl53.range))
time.sleep(1.0)
except KeyboardInterrupt:
print("Exit") # Exit on CTRL+C
Code Analysis
Importing Libraries
import time import board import busio import adafruit_vl53l0x
time: Used for implementing delays.board: Provides access to the physical pins on the Raspberry Pi.busio: Manages I2C communication between the Pi and the sensor.adafruit_vl53l0x: The specific library for the VL53L0X sensor. For more detail about theadafruit_vl53l0xlibrary, please refer to adafruit/Adafruit_CircuitPython_VL53L0X.
Initializing the Sensor
# Initialize I2C bus and sensor. i2c = busio.I2C(board.SCL, board.SDA) vl53 = adafruit_vl53l0x.VL53L0X(i2c)
This sets up the I2C communication using SCL (clock line) and SDA (data line) pins.
The VL53L0X sensor is then initialized with this I2C bus.
Configuration (Optional)
# Optionally adjust the measurement timing budget... # vl53.measurement_timing_budget = 20000 # ...
This part of the code, which is commented out, allows for adjusting the sensor’s timing budget, affecting the balance between speed and accuracy.
Main Loop
try: while True: print("Range: {0}mm".format(vl53.range)) time.sleep(1.0) except KeyboardInterrupt: print("Exit")
In an infinite loop, the sensor’s range is read and printed every second.
The loop can be exited with a CTRL+C interrupt, which is handled by the KeyboardInterrupt exception.