Use VL53L0X (Time of Flight distance sensor) on ESP32-S3/CircuitPython

The VL53L0X is a Time of Flight distance sensor contains a very tiny invisible laser source (invisible for human eyes, but can be seen by digital camera), and a matching sensor. The VL53L0X can detect the "time of flight", or how long the light has taken to bounce back to the sensor.

Here is my exercise of using VL53L0X (or GY-530) on Waveshare ESP32-S3-Zero running CircuitPython 9.0.0-beta.0, to read VL53L0X and display on 128x64 SSD1306 I2C OLED, and control onboard RGB LED if too close.


Connection:

Both VL53L0X and SSD1306 OLED share the same I2C bus.
VL53L0X		SSD1306 I2C    ESP32-S3-Zero
VIN		GND            GND
GND		VCC            3V3
SCL		SCL            GP5
SDA		SDA            GP4
Exercise code:

cpy_S3-Zero_I2CIO45_vl53l0x.py
"""
Simple exercise of using VL53L0X Distance Sensor
Run on Wave ESP32-S3-Zero/CircuitPython 9.0.0-beta.0

library need:
- adafruit_vl53l0x.mpy
"""
import board
import busio
import adafruit_vl53l0x
import time

SCL = board.IO5
SDA = board.IO4
i2c = busio.I2C(SCL, SDA)

sensor = adafruit_vl53l0x.VL53L0X(i2c)
print(sensor)

while True:
    print('Range: {}mm'.format(sensor.range), end="\r")
    time.sleep(0.1);


cpy_S3-Zero_I2CIO45_vl53l0x_ssd1306.py
"""
Exercise of using VL53L0X Distance Sensor
Run on Wave ESP32-S3-Zero/CircuitPython 9.0.0-beta.0
with SSD1306 I2C OLED setup digitalio

Once a Display is setup, before switche to display any given group of layers,
the default CircuitPython terminal will be shown; work as REPL display terminal.

library need:
- adafruit_vl53l0x.mpy
- adafruit_displayio_ssd1306.mpy
"""
import os, sys
import board
import busio
import adafruit_vl53l0x
import time

import displayio
import adafruit_displayio_ssd1306
import i2cdisplaybus

displayio.release_displays()

# Create the I2C interface
SCL = board.IO5
SDA = board.IO4
i2c = busio.I2C(SCL, SDA)

vl53l0x_sensor = adafruit_vl53l0x.VL53L0X(i2c)

ssd1306_i2c_addr = 0x3C
display_width =128
display_height = 64
# FutureWarning: I2CDisplay moved from displayio to i2cdisplaybus
# FutureWarning: I2CDisplay renamed I2CDisplayBus
#display_bus = displayio.I2CDisplay(i2c, device_address=ssd1306_i2c_addr)
display_bus = i2cdisplaybus.I2CDisplayBus(i2c, device_address=ssd1306_i2c_addr)

display = adafruit_displayio_ssd1306.SSD1306(
    display_bus, width=display_width, height=display_height)

print("=================================================")
info = sys.implementation[0] + ' ' + os.uname()[3] + '\n' + \
       'run on ' + os.uname()[4]
print(info)
print("=================================================")
print(adafruit_vl53l0x.__name__,
      adafruit_vl53l0x.__version__)
print(adafruit_displayio_ssd1306.__name__,
      adafruit_displayio_ssd1306.__version__)
print("SCL: ", SCL)
print("SDA: ", SDA)
print()

#vl53l0x_sensor.start_continuous()
print("is_continuous_mode:\n", vl53l0x_sensor.is_continuous_mode)

time.sleep(1)
print()
while True:
    print('Range: {}mm'.format(vl53l0x_sensor.range), end="\r")
    time.sleep(0.1);


cpy_S3-Zero_I2CIO45_vl53l0x_ssd1306_framebuf_RGB.py
"""
Exercise of using VL53L0X Distance Sensor
Run on Wave ESP32-S3-Zero/CircuitPython 9.0.0-beta.0
with SSD1306 I2C OLED using adafruit_ssd1306/adafruit_framebuf
and onboard RGB LED (NeoPixel)

library need:
- adafruit_vl53l0x.mpy
- adafruit_ssd1306.mpy
- adafruit_framebuf.mpy
- adafruit_simplemath.mpy
- font5x8.bin (from examples folder,
  to current folder in CircuitPython device)
"""

import os, sys
import board
import busio
import adafruit_vl53l0x
import time

import neopixel
import adafruit_ssd1306

from adafruit_simplemath import map_range

# Create the NeoPixel object
pixel = neopixel.NeoPixel(board.NEOPIXEL,
                          1,
                          pixel_order=neopixel.RGB)

# Create the I2C interface
SCL = board.IO5
SDA = board.IO4
i2c = busio.I2C(SCL, SDA)

vl53l0x_sensor = adafruit_vl53l0x.VL53L0X(i2c)

# Create the I2C interface and display object of SSD1306_I2C.
ssd1306_i2c_addr = 0x3C
DISP_WIDTH = 128
DISP_HEIGHT = 64
# display = adafruit_ssd1306.SSD1306_I2C(DISP_WIDTH, DISP_HEIGHT, i2c)
# Or pass with an addr parameter:
display = adafruit_ssd1306.SSD1306_I2C(DISP_WIDTH, DISP_HEIGHT, i2c, addr=ssd1306_i2c_addr)

print("=================================================")
info = sys.implementation[0] + ' ' + os.uname()[3] + '\n' + \
       'run on ' + os.uname()[4]
print(info)
print("=================================================")
print(adafruit_vl53l0x.__name__,
      adafruit_vl53l0x.__version__)
print(adafruit_ssd1306.__name__,
      adafruit_ssd1306.__version__)
print("SCL: ", SCL)
print("SDA: ", SDA)
print()

pixel[0] = (0, 0, 0)
display.fill(0)
display.show()
time.sleep(0.5)

pixel[0] = (5, 5, 5)
display.fill(1)
display.show()
time.sleep(0.5)

pixel[0] = (0, 0, 0)
display.fill(0)
display.text("VL53L0X exercise", 0, 0, 1)
display.text(sys.implementation[0], 0, 15, 1)
display.text(os.uname()[3], 0, 25, 1)
display.text(os.uname()[4], 0, 35, 1)

display.show()

#================================================
#vl53l0x_sensor.start_continuous()
print("is_continuous_mode:", vl53l0x_sensor.is_continuous_mode)

print()
while True:

    distance = vl53l0x_sensor.range

    dist_length = int(map_range(distance, 0, 500, 0, DISP_WIDTH - 1))
    dist_brightness = int(map_range(distance, 0, 150, 255, 0))

    display.fill_rect(0, 50, 80, 8, 0)
    display.text('Range: {}mm'.format(distance), 0, 50, 1)
    pixel[0] = (dist_brightness, 0, 0)
    display.hline(0, 60, dist_length, 1)
    display.hline(dist_length, 60, DISP_WIDTH-dist_length, 0)
    display.show()
    
    print('Range: {}mm'.format(distance), end="\r")


next:
Radar-like distance scanner on ESP32-S3/CircuitPython 9, using SG90 servo motor, VL53L0X distance sensor and SSD1306 OLED.

Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

my dev.tools - FNIRSI 2C23T 3-in-1 Dual Channel Oscilloscope/Multimeter/Signal Generator