In this lab we create a program that will show the distance measured by the Time-of-Flight sensor by printing the distance on the console and also displaying the distance on 11 blue LEDs.
First, make sure you have your driver for the Time-of-Flight sensor installed.
You can copy the code from here and save it in the file VL53L0X.py. Note the zero between the "L" and "X" in the file name, not the letter "O".
We use a non-linear distance scale as we get closer to an object. We store the numbers of each LED and the distance it should change in a lists:
There are three numbers you can change when you calibrate the sensor:
123
ZERO_DIST=60# The value of the sensor when an object is 0 CM awayMAX_DIST=1200# max raw distance we are able to readSCALE_DIST=.3# multiplier for raw to calibrated distance in CM
# Demo for Maker Pi RP2040 board using the VL32L0X time of flight distance sensor# Note the driver I used came from here: https://github.com/CoderDojoTC/micropython/blob/main/src/drivers/VL53L0X.py# Perhaps derived from here: https://github.com/uceeatz/VL53L0X/blob/master/VL53L0X.py# This demo makes the blue LEDs show the distance and prints the distance on the consoleimportmachineimporttimeimportVL53L0Xsda=machine.Pin(0)# row one on our standard Pico breadboardscl=machine.Pin(1)# row two on our standard Pico breadboardi2c=machine.I2C(0,sda=sda,scl=scl,freq=400000)# print("Device found at decimal", i2c.scan())# The Maker Pi RP2040 has 13 fantastic blue GPIO status LEDs which we can use 11# The distance scale is non linear# GP0 and GP1 will always be on since they are the I2C Data and Clockblue_led_pins=[2,3,4,5,6,7,16,17,26,27,28]dist_scale=[2,6,10,20,30,40,50,60,80,110,150]number_leds=len(blue_led_pins)led_ports=[]delay=.05# initial calibration parametersZERO_DIST=60MAX_DIST=1200# max raw distance we are able to readSCALE_DIST=.3# multiplier for raw to calibrated distance# create a list of the portsforiinrange(number_leds):led_ports.append(machine.Pin(blue_led_pins[i],machine.Pin.OUT))# Create a VL53L0X objecttof=VL53L0X.VL53L0X(i2c)# get the normalized time-of-flight distancedefget_distance():globalzero_dist,scale_factortof_distance=tof.read()iftof_distance>MAX_DIST:returntof_distance# if our current time-of-flight distance is lower than our zero distance then reset the zero distanceiftof_distance<ZERO_DIST:zero_dist=tof_distancereturnint((tof_distance-ZERO_DIST)*SCALE_DIST)# use the dist_scale to turn on LEDsdefled_show_dist(in_distance):globalnumber_ledsforled_indexinrange(0,number_leds):ifin_distance>dist_scale[led_index]:led_ports[led_index].high()else:led_ports[led_index].low()print('Using',number_leds,' blue leds to show distance.')# blue upforiinrange(0,number_leds):led_ports[i].high()time.sleep(delay)led_ports[i].low()# blue downforiinrange(number_leds-1,0,-1):led_ports[i].high()time.sleep(delay)led_ports[i].low()# start our time-of-flight sensortof.start()# autocalibrate the minimum distancemin_distance=1000# loop foreverwhileTrue:raw_distance=get_distance()# recalibrate if we have a new min distanceifraw_distance<min_distance:min_distance=raw_distancecalibrated_distance=raw_distance-min_distanceprint(raw_distance,calibrated_distance)led_show_dist(calibrated_distance)time.sleep(0.05)# clean uptof.stop()