Many people in construction and other trades often use very convenient and precise distance meters that use laser light to make their measurements. These instruments can have a range of a few meters to several tens of meters, various memories to store different measurements, use different units, and even calculate areas and volumes.
The operating principle of these meters is quite simple: they have a laser diode (Emitter) that sends a beam of light to the surface or object whose distance is to be measured (Object). Part of that light is reflected and received by a detector also installed in the meter (Receiver). By knowing the time (t) between the emission and reception of the light beam and its speed (c, speed of light), the distance (d) can be determined with a simple calculation:
The time it takes for the light to travel from the emitter to the object and back to the receiver is called Time Of Flight and is abbreviated as TOF. Therefore, the measurement technique and the sensors that use it are often referred to in the same way, TOF.
This operating principle is similar to that of an ultrasonic distance meter, which is very popular in low-cost robots. However, in this case, the measurement can be affected by several factors that make it less accurate than a meter that uses laser light.
Among all the modules and accessories that M5Stack sells for its various devices, we can find three that we can use for our distance meter.
Two of them use the Grove interface. One has a maximum range of 2 meters, and the other has an extended range of up to 4 meters.
There is also a third HAT-format module for M5Stick that I will use in this project.
This HAT contains the specialized chip VL53L0X, designed specifically for distance measurement function, with an invisible laser for the human eye, the receiver, and a microcontroller with I2C interface. The VL53L0X is capable of making measurements in a maximum range of 2m with great precision.
The ProgramThe TOF Hat is supported by UIFlow (both version 1 and version 2), although in this project, I use UIFlow 1.
Adding the HATAs with other HATs, you need to add it to the project using the "+" button in the interface area. When you click on the "+", all Hats recognized by UIFlow appear
Once you add it, it will appear in the UIFlow block menu.
As you can see, a single block called Get Distance is added, which returns the measured distance in millimeters.
OperationThe measuring program continuously takes distance measurements and displays the measured value on the screen every 1 second.
At startup, the measurement unit is set to millimeters (mm), but by pressing button B (the small button on the right with the HAT facing up), you can switch to centimeters (cm) and inches (in).
Additionally, to compare measurements, there is a memory feature. The current value is saved to memory by pressing button A (the large front button). When the current measurement is saved to memory, any previously stored value is erased.
The meter interface is simple. It uses only three labels to display the measured value, the unit of measurement, and the value stored in memory.
- Label0: Displays the current measurement
- Label1: Displays the unit of measurement (mm, cm, or in)
- Label2: Value stored in memory
In the following image, you can see the meter code
Analyzing the program, you can see that within Setup, the default unit of measurement (mm) is displayed, the variable Factor is initialized to 1, and the variable PreviousDistance, which acts as memory, is set to 0.
The Factor variable is used to display the measurement (and the value stored in memory) in different units. Since the block that performs the measurement returns the value in mm, if we want to display the value in the same unit, the conversion factor is 1. If we want to display the measurement in cm, the conversion factor is 10, because 1 cm = 10 mm. Finally, if the chosen unit is inches, the factor is 25.4 because 1 inch = 25.4 mm.
Then, in the loop section, the measurement is taken. The measured value (in mm) is divided by Factor to adjust it to the chosen unit, and the same is done with the value stored in memory, PreviousDistance, so that it appears in the same unit. Finally, these two values are displayed, each in its Label with two decimal places, and a 1-second delay is added.
On the other hand, there are two events for when button A and button B are pressed.
When button A is pressed, a “beep” is made, and the current measurement is stored in memory.
When button B is pressed, a different sound is made, and the value of the Factor variable and the unit of measurement displayed on the screen are changed. For this, I use a switch-case block that allows different actions to be selected based on a numerical value (in this case, the Factor variable).
In each case, the value of the Factor variable is modified, and the label indicating the unit of measurement on the screen is changed.
RepositoryThis project, like all the others I've done with the M5Stick, is in my GitHub repository.
More InfoConclusionsIn this project, I showed you how to easily build a laser distance meter with an M5StickC Plus2 and the TOF Hat programmed in UIFlow1. It's another example of how easy it is to use M5Stack devices to create various types of projects.
There are many possible improvements, such as averaging multiple measurements before displaying the measured value to reduce error or adding more memory. I hope this program serves as a basis for you to continue creating your own versions.
If you're a teacher, this project can be used to teach basic programming concepts, as well as units of measurement and conversion between them, among other topics.
Comments
Please log in or sign up to comment.