4.9 KiB
Detect proximity - Wio Terminal
In this part of the lesson, you will add a proximity sensor to your Wio Terminal and read distance measurements from it.
Hardware
The Wio Terminal requires a proximity sensor.
The sensor you'll use is a Grove Time of Flight distance sensor. This sensor uses a laser ranging module to measure distance. It has a range of 10mm to 2000mm (1cm - 2m) and provides fairly accurate readings within this range. Distances above 1000mm are reported as 8109mm.
The laser rangefinder is located on the back of the sensor, opposite the Grove socket.
This is an I2C sensor.
Connect the time of flight sensor
The Grove time of flight sensor can be connected to the Wio Terminal.
Task - connect the time of flight sensor
Connect the time of flight sensor.
-
Insert one end of a Grove cable into the socket on the time of flight sensor. It will only fit one way.
-
With the Wio Terminal disconnected from your computer or other power source, connect the other end of the Grove cable to the left-hand Grove socket on the Wio Terminal as you face the screen. This socket is closest to the power button. It is a combined digital and I2C socket.
- You can now connect the Wio Terminal to your computer.
Program the time of flight sensor
The Wio Terminal can now be programmed to use the attached time of flight sensor.
Task - program the time of flight sensor
-
Create a new Wio Terminal project using PlatformIO. Name this project
distance-sensor
. Add code in thesetup
function to configure the serial port. -
Add a library dependency for the Seeed Grove time of flight distance sensor library to the project's
platformio.ini
file:lib_deps = seeed-studio/Grove Ranging sensor - VL53L0X @ ^1.1.1
-
In
main.cpp
, add the following below the existing include directives to declare an instance of theSeeed_vl53l0x
class to interact with the time of flight sensor:#include "Seeed_vl53l0x.h" Seeed_vl53l0x VL53L0X;
-
Add the following to the bottom of the
setup
function to initialize the sensor:VL53L0X.VL53L0X_common_init(); VL53L0X.VL53L0X_high_accuracy_ranging_init();
-
In the
loop
function, read a value from the sensor:VL53L0X_RangingMeasurementData_t RangingMeasurementData; memset(&RangingMeasurementData, 0, sizeof(VL53L0X_RangingMeasurementData_t)); VL53L0X.PerformSingleRangingMeasurement(&RangingMeasurementData);
This code initializes a data structure to store the data, then passes it into the
PerformSingleRangingMeasurement
method, which populates it with the distance measurement. -
Below this, write out the distance measurement, then delay for 1 second:
Serial.print("Distance = "); Serial.print(RangingMeasurementData.RangeMilliMeter); Serial.println(" mm"); delay(1000);
-
Build, upload, and run this code. You will be able to see distance measurements in the serial monitor. Place objects near the sensor, and you will see the distance measurement:
Distance = 29 mm Distance = 28 mm Distance = 30 mm Distance = 151 mm
The rangefinder is located on the back of the sensor, so ensure you use the correct side when measuring distance.
💁 You can find this code in the code-proximity/wio-terminal folder.
😀 Your proximity sensor program was a success!
Disclaimer:
This document has been translated using the AI translation service Co-op Translator. While we aim for accuracy, please note that automated translations may include errors or inaccuracies. The original document in its native language should be regarded as the authoritative source. For critical information, professional human translation is advised. We are not responsible for any misunderstandings or misinterpretations resulting from the use of this translation.