Rangefinder with M5Stack + TOF-Sensor and ANNEX32
A handy distance sensor / rangefinder based on portable M5Stack-Fire and a TOF-sensor VL53LOX.
My M5Stack fire became - thanks to its LiPol battery - together with some ANNEX32 code and an I2C VL53LOX (~10€) a very handy rangefinder.
This VL53LOX-TOF-sensor works in two ranges from 0 to 60cm to 200cm..
The accuracy has three levels with influence on the measuring speed.
The range and the accuracy can be selected via two of the hardware buttons or the webinterface
Distance ist shown in millimeter on the build-in display and on the web interface.
The firmware used in this project is ANNEX32 for ESP32/M5 hardware. So the code is a BASIC script, which can easily be adapted to other needs.
Take a look at the ANNEX-FORUM
' #START##########BASIC-CODE for ANNEX32#############################
' TOF Distance-Sensor
' -M5Stack-Hardware with
' -3 pulldown-buttons at pin 37,38,39
' -TOF Sensor VL53LOX via I2C
' Range: 0-200cm
' peter.neufeld@gmx.de
VERSION$ = "V1.0"
'-----TFT-DISPLAY-setup
COL_BACKGROUND = tft.rgb(00,00,0)
COL_TEXT = tft.rgb(00,255,100)
TFT.INIT 1
TFT.FILL COL_BACKGROUND
TFT.TEXT.COL COL_TEXT,COL_BACKGROUND
'-----TOF-Sensor-setup
ACCURACY$=""
RANGE$=""
while not VL53L0X.init ' Test for TOF device
gosub NO_TOF_SENSOR
onhtmlreload NO_TOF_SENSOR
pause 1000
wend
TFT.FILL COL_BACKGROUND
'VL53L0X.SetAccuracy 0 ' set the refresh rate at 33 msec (min accuracy)
VL53L0X.SetAccuracy 1 ' set the refresh rate at 200 msec (med accuracy)
'VL53L0X.SetAccuracy 2 ' set the refresh rate at 400 msec (max accuracy)
'VL53L0X.SetRange 0 ' set the range at 60 mm ( 6o cm )
VL53L0X.SetRange 1
BUTTON_A = 39
BUTTON_B = 38
BUTTON_C = 37
BUTTON_Pressed = 0
DIST$ = ""
X$ = ""
'pin.mode BUTTON_A,input
'pin.mode BUTTON_B,input
'pin.mode BUTTON_C,input
'
' set initial accuracy and range
BUT1_IDX = 2
BUT2_IDX = 0
gosub BUT_1
gosub BUT_2
gosub WebInterface
onhtmlreload WebInterface
onhtmlchange WebInterface
' ----endless MAIN LOOP----
timer1 750, MAIN_PRG
wait
end
' #############################################
MAIN_PRG:
gosub TEST_M5_BUTTON
gosub READ_TOF
gosub PAINT_DISPLAY
return
' #############################################
READ_TOF:
d = VL53L0X.Distance ' get the distance
' suppress all out-of-range values
If (RANGE$ ="60cm") and (d > 660) then d= -1
If (RANGE$ ="200cm") and (d > 2200) then d= -1
DIST$= str$(d,"%4.0f") +"mm "
return
' #############################################
TEST_M5_BUTTON:
if pin(BUTTON_A)+pin(BUTTON_B)+pin(BUTTON_C) <3 then
pause 10 'Press longer than 10ms
if pin(BUTTON_A)= 0 then gosub BUT_1
if pin(BUTTON_B)= 0 then gosub BUT_2
if pin(BUTTON_C)= 0 then gosub BUT_3
else
BUTTON_Pressed=0
end if
return
' #########################################################################
BUT_1:
BUTTON_Pressed = 1
BUT1_IDX= (BUT1_IDX +1) mod 3
IF BUT1_IDX = 0 then ACCURACY$ = "LOW but FAST "
IF BUT1_IDX = 1 then ACCURACY$ = "MEDIUM "
IF BUT1_IDX = 2 then ACCURACY$ = "HIGH but SLOW "
VL53L0X.SetAccuracy BUT1_IDX
'gosub BUTTON_ACTION
return
' #########################################################################
BUT_2:
BUTTON_Pressed = 2
BUT2_IDX= (BUT2_IDX +1) mod 2
If BUT2_IDX = 0 then RANGE$ = "60cm"
If BUT2_IDX = 1 then RANGE$ = "200cm"
VL53L0X.SetRange BUT2_IDX
'gosub BUTTON_ACTION
return
' #########################################################################
BUT_3:
BUTTON_Pressed = 3
'gosub BUTTON_ACTION
return
' #########################################################################
BUT_0:
BUTTON_Pressed = 0
gosub BUTTON_ACTION
return
' #############################################
PAINT_DISPLAY:
' do not forgett to set M5 Display at config-page.
' There seem to be two types of displays in M5Stack.
' Colors may bee inverted with older(?) display
tft.text.size 1
tft.text.pos 80,2
TFT.PRINT "@: HTTP://";WORD$(IP$,1)
tft.text.size 3
tft.text.pos 120,25
tft.print "TOF-"
tft.text.pos 20,50
tft.print "distance-sensor"
tft.text.size 8
tft.text.pos 20,110
tft.print DIST$
tft.text.size 1
tft.text.pos 5,195
tft.print "A C C U R A C Y :"
tft.text.pos 5,210
tft.print ACCURACY$+" "
tft.text.size 1
tft.text.pos 130,195
tft.print "R A N G E :"
tft.text.pos 130,210
tft.print RANGE$+" "
TFT.LINE 000, 190, 320, 190, COL_TEXT
TFT.LINE 110, 190, 110, 240, COL_TEXT
TFT.LINE 210, 190, 210, 240, COL_TEXT
return
' #########################################################################
WebInterface:
cls
autorefresh 800
a$=""
a$ = a$ + "
This VL53LOX-TOF-sensor works in two ranges from 0 to 60cm to 200cm..
The accuracy has three levels with influence on the measuring speed.
The range and the accuracy can be selected via two of the hardware buttons or the webinterface
Distance ist shown in millimeter on the build-in display and on the web interface.
The firmware used in this project is ANNEX32 for ESP32/M5 hardware. So the code is a BASIC script, which can easily be adapted to other needs.
Take a look at the ANNEX-FORUM
' #START##########BASIC-CODE for ANNEX32#############################
' TOF Distance-Sensor
' -M5Stack-Hardware with
' -3 pulldown-buttons at pin 37,38,39
' -TOF Sensor VL53LOX via I2C
' Range: 0-200cm
' peter.neufeld@gmx.de
VERSION$ = "V1.0"
'-----TFT-DISPLAY-setup
COL_BACKGROUND = tft.rgb(00,00,0)
COL_TEXT = tft.rgb(00,255,100)
TFT.INIT 1
TFT.FILL COL_BACKGROUND
TFT.TEXT.COL COL_TEXT,COL_BACKGROUND
'-----TOF-Sensor-setup
ACCURACY$=""
RANGE$=""
while not VL53L0X.init ' Test for TOF device
gosub NO_TOF_SENSOR
onhtmlreload NO_TOF_SENSOR
pause 1000
wend
TFT.FILL COL_BACKGROUND
'VL53L0X.SetAccuracy 0 ' set the refresh rate at 33 msec (min accuracy)
VL53L0X.SetAccuracy 1 ' set the refresh rate at 200 msec (med accuracy)
'VL53L0X.SetAccuracy 2 ' set the refresh rate at 400 msec (max accuracy)
'VL53L0X.SetRange 0 ' set the range at 60 mm ( 6o cm )
VL53L0X.SetRange 1
BUTTON_A = 39
BUTTON_B = 38
BUTTON_C = 37
BUTTON_Pressed = 0
DIST$ = ""
X$ = ""
'pin.mode BUTTON_A,input
'pin.mode BUTTON_B,input
'pin.mode BUTTON_C,input
'
' set initial accuracy and range
BUT1_IDX = 2
BUT2_IDX = 0
gosub BUT_1
gosub BUT_2
gosub WebInterface
onhtmlreload WebInterface
onhtmlchange WebInterface
' ----endless MAIN LOOP----
timer1 750, MAIN_PRG
wait
end
' #############################################
MAIN_PRG:
gosub TEST_M5_BUTTON
gosub READ_TOF
gosub PAINT_DISPLAY
return
' #############################################
READ_TOF:
d = VL53L0X.Distance ' get the distance
' suppress all out-of-range values
If (RANGE$ ="60cm") and (d > 660) then d= -1
If (RANGE$ ="200cm") and (d > 2200) then d= -1
DIST$= str$(d,"%4.0f") +"mm "
return
' #############################################
TEST_M5_BUTTON:
if pin(BUTTON_A)+pin(BUTTON_B)+pin(BUTTON_C) <3 then
pause 10 'Press longer than 10ms
if pin(BUTTON_A)= 0 then gosub BUT_1
if pin(BUTTON_B)= 0 then gosub BUT_2
if pin(BUTTON_C)= 0 then gosub BUT_3
else
BUTTON_Pressed=0
end if
return
' #########################################################################
BUT_1:
BUTTON_Pressed = 1
BUT1_IDX= (BUT1_IDX +1) mod 3
IF BUT1_IDX = 0 then ACCURACY$ = "LOW but FAST "
IF BUT1_IDX = 1 then ACCURACY$ = "MEDIUM "
IF BUT1_IDX = 2 then ACCURACY$ = "HIGH but SLOW "
VL53L0X.SetAccuracy BUT1_IDX
'gosub BUTTON_ACTION
return
' #########################################################################
BUT_2:
BUTTON_Pressed = 2
BUT2_IDX= (BUT2_IDX +1) mod 2
If BUT2_IDX = 0 then RANGE$ = "60cm"
If BUT2_IDX = 1 then RANGE$ = "200cm"
VL53L0X.SetRange BUT2_IDX
'gosub BUTTON_ACTION
return
' #########################################################################
BUT_3:
BUTTON_Pressed = 3
'gosub BUTTON_ACTION
return
' #########################################################################
BUT_0:
BUTTON_Pressed = 0
gosub BUTTON_ACTION
return
' #############################################
PAINT_DISPLAY:
' do not forgett to set M5 Display at config-page.
' There seem to be two types of displays in M5Stack.
' Colors may bee inverted with older(?) display
tft.text.size 1
tft.text.pos 80,2
TFT.PRINT "@: HTTP://";WORD$(IP$,1)
tft.text.size 3
tft.text.pos 120,25
tft.print "TOF-"
tft.text.pos 20,50
tft.print "distance-sensor"
tft.text.size 8
tft.text.pos 20,110
tft.print DIST$
tft.text.size 1
tft.text.pos 5,195
tft.print "A C C U R A C Y :"
tft.text.pos 5,210
tft.print ACCURACY$+" "
tft.text.size 1
tft.text.pos 130,195
tft.print "R A N G E :"
tft.text.pos 130,210
tft.print RANGE$+" "
TFT.LINE 000, 190, 320, 190, COL_TEXT
TFT.LINE 110, 190, 110, 240, COL_TEXT
TFT.LINE 210, 190, 210, 240, COL_TEXT
return
' #########################################################################
WebInterface:
cls
autorefresh 800
a$=""
a$ = a$ + "
+- - - - - - - - - - - - - - - - - - - - - -+"
a$ = a$ + "
- TOF-DISTANCE-SENSOR - -"
a$ = a$ + "
########### "+ VERSION$ + " ###########"
a$ = a$ + "
"
a$ = a$ + TEXTBOX$(DIST$,"cssTB")
a$ = a$ + "
"
a$ = a$ + TEXTBOX$(ACCURACY$,"cssTB1")
a$ = a$ + TEXTBOX$(RANGE$,"cssTB1")
a$ = a$ + TEXTBOX$(x$,"cssTB1")
a$ = a$ + "
"
a$ = a$ + BUTTON$("set ACCURACY", BUT_1,"cssBT")
a$ = a$ + BUTTON$("set RANGE", BUT_2,"cssBT")
a$ = a$ + BUTTON$("-------", BUT_3,"cssBT")
a$ = a$ + "
+- - - - - - - - - - - - - - - - - - - - - -+
Diskussion (0 Kommentare)