Maker Portal

View Original

Arduino + VL53L1X Time of Flight Distance Measurement

“As an Amazon Associates Program member, clicking on links may result in Maker Portal receiving a small commission that helps support future projects.”

See this content in the original post

Time of flight (ToF) is an approximation of the time it takes a traveling wave to come in contact with a surface and reflect back to the source. Time of flight has applications in automotive obstacle detection, resolving geographic surface composition, and computer vision and human gesture recognition. In the application here, the VL53L1X ToF sensor will be used to track the displacement of a ping pong ball falling down a tube. We can predict the acceleration and behavior of a falling ping pong ball by balancing the forces acting on the ball, and ultimately compare the theory to the actual displacement tracked by the time of flight sensor.


See this content in the original post

Time of flight uses the basic principles of Newtonian physics by assuming perfectly elastic contact with a surface. Most inexpensive ToF sensors use amplitude modulation to emit a singular or series of pulses at a particular frequency. For a laser, the frequency is usually in the infrared spectrum to ensure that the measurement is not visible to the human eye, and for an ultrasonic pulse its above the audible range. For an object at a distance x away from the light emitter (laser), we can approximate the reflection time for the light to hit the object and return back to the emitter:

See this content in the original post

where x is the distance to the object (2x because the signal has to reflect back), c is the speed of the wave (light here), and ΔT is the time it takes to travel 2x. This process is also shown in the image below.

The width of the signal pulse limits the minimum detectable distance and is often the working limit of the ToF module. This is a limitation of hardware that is capable of producing and reading short pulses of light. For example, a pulse width in the nano or pico second range are capable of resolving distances in the meter and millimeter ranges, respectively. The pulses are repeated hundreds or thousands of times to increase the sensor’s ability to measure longer distances. Therefore, the longer the desired distance, the longer the pulse ‘train’ needs to be. This means that either the pulses need to be shorter or the pulse train needs to be shorter to allow for longer reflections. More on ToF sensors and their limitations can be found here. The VL53L1X has a maximum range of 4m, which likely relates to a pulse width in the nanosecond range.


See this content in the original post

For this tutorial, I will be tracking the displacement of a ping pong ball falling down a tube using the VL53L1X time of flight sensor. The tube is used to both slow the ball down and also ensure the ToF sensor tracks the same position on the ball. An Arduino will be used to record the ToF measurements and a Raspberry Pi will be used to record and analyze the values. The full parts list is given below:

See this product in the original post
  1. VL53L1X ToF Sensor - $21.99 [Amazon]

  2. Ping Pong Ball - $6.17 [Amazon]

  3. Acrylic Tube - $12.12 [Amazon]

  4. Arduino Uno - $11.00 [Shop]

  5. Raspberry Pi - $38.10 [Amazon]

  6. Jumper Wires - $5.99 [Amazon]

The VL53L1X can be wired to the Arduino board using the standard I2C ports on the Uno board (pins A4/A5). The wiring diagram is shown below:

In the next section, I will introduce the experimental setup for the ping pong ball falling down the tube and the VL53L1X placement in order to monitor the displacement of the falling ball.


See this content in the original post

For the actual experiment, the ball will be dropped by hand down the tube. The VL53L1X will be recording the time and displacement, which will then be read by the Raspberry Pi from the serial port. The setup of the ball, tube, and ToF sensor is shown below.


See this content in the original post

The basic setup for the experiment consists of a ping pong ball, VL53L1X sensor, and tube. The ball will be dropped from the top of the tube, and the VL53L1X will record the displacement of the ball as it travels down the tube due to gravity. The force balance for the ball is shown below:

The VL53L1X datasheet can be found here.

Applying Newton’s law leads to the following:

See this content in the original post

where Fg is the force due to gravity, FD is the drag force, and FB is the buoyant force. The equation above can be expanded and written in its explicit form:

See this content in the original post

where m is the mass of the ball, v is the velocity of the ball, g is the gravitation acceleration, ρf is the density of air, A is the aerodynamic area of the ball susceptible to drag, CD is the drga coefficient, and V is the volume of the ball. We can further rearrange the differential equation above and simplify a few parameters:

See this content in the original post

with the constants α and β defined as:

See this content in the original post

the differential equation has a solution of the form:

See this content in the original post

As an initial condition, we know the ball is not moving, so we can quantify the constant C0:

See this content in the original post

Therefore, our final velocity expression is:

See this content in the original post

Now, in order to approximate distance, we need to integrate the velocity:

See this content in the original post

which is much easier than the original differential equation, such that we now have an expression for the displacement as a function of time for a falling ping pong ball in a tube:

See this content in the original post

See this content in the original post

The VL53L1X library by Pololu is the simplest method for getting started with the ToF sensor. The code used to print the distance data from the VL53L1X is shown below.

See this content in the original post

The code prints out milliseconds and distance for each measurement. The milliseconds prinout will allow us to attach a timestamp to each measurement. On the Raspberry Pi side, we can read the printout using Python’s Serial reader, pyserial. I wrote a full tutorial on reading from the serial port previously, which can be reviewed here. The code for reading the VL53L1X data is shown below:

See this content in the original post

The above code reads the Arduino data and saves it to two arrays (time and data). It also checks that the data is valid for temporal and distance formats and not invalid data. Then, once the user presses ‘CTRL+C’ the program stops and plots the data in terms of points. The points data should look like the figure below:

In the next section, I address issues and results surrounding the ball drop experiment and the expectations when comparing the theory to experiments and how to properly handle the data and use the start/end points of the data shown in the figure above.


See this content in the original post

The assumption in this section is that the user was able to obtain data of the ball being dropped. The Python code should save the data shown in the profile above into a .csv file named ‘test_data.csv.’ Assuming all of this is true, the code and analysis to follow should be seamless. As stated in the theory sections above, we expect velocity and displacement profiles that follow those functions. We can now implement them into Python and see what the theory tells us about the expected behavior of the ball in the tube. The full code is shown below, but the pieces will be outline in a logical sequence that follows.

See this content in the original post

The three most important inputs in the code above are the ‘start_comp’ and ‘end_comp’ points and the drag coefficient ‘C_D_analyt.’ The start and end comparison points will determine where the effective ‘drop’ points are. Having the proper start and finish times are essential for proper comparison between theory and experiment, and it takes some manual work to find the true points. Second, the drag coefficient determines the speed at which the ball reaches terminal velocity, meaning that the higher the value for the drag coefficient, the quicker the ball will reach terminal velocity, the lower the drag coefficient, the longer it will take to reach terminal velocity. The terminal velocity for the ping pong ball, for example, can be seen to be about 2.4 m/s in the figure below.

If the start/end points were chosen correctly for the ball drop and the drag coefficient is appropriate (depending on the material of the tube and roughness of the ball), then the profiles should be similar to those above. A good way to determine if the endpoints are correct is to look at the time stamp - if the end time is around the time it takes the ball to hit the ground (something that can be measured with a stopwatch). Another way to verify the ending timestamp is to make sure the bounce after hitting the ground is not included. The bounce is easy to find because the velocity changes direction.

See this content in the original post

I also included the profile of a ball drop without any drag, which makes it easy to see just how essential it is that drag effects are included in the analysis of such an experiment.


The plot below is further validation that the experimental results are fairly accurate as well as repeatable. Five separate plots are given with the same drop experiment, but this time with a tube of 1.2 m length. We are not only proving our hypothesis, but also extending it by using a different sized tube. We can see that the ball profile reaches terminal velocity around the 0.5 second mark, where we start to see a linear displacement profile.

Surely, the theory and experiments do not match perfectly, there are neglected factors such as a non-constant drag coefficient, however, for the purposes of this experiment we are still able to recover theoretical displacement results within 9% of the experimental values.


See this content in the original post

The VL53L1X time of flight sensor is a powerful sensor that is capable of resolving fast changes in distance with fairly accurate and consistent results. This experiment proved the reliability of a ToF sensor for approximating displacement of a moving object. The relatively high sample rate (20Hz-50Hz) of the VL53L1X was able to resolve the quick movements of a ping pong ball down an acrylic tube, which allowed us to compare the theoretical displacement prediction to the actual physical behavior of the ball. The experiment can be used as an educational tool for fluid mechanics classes and engineering students to explain the viscous effects of pipes and surface area drag effects. We were able to determine an approximate drag coefficient for the system, around 6, which could be used to characterize a similar system involving traveling objects in tubes. Lastly, the real-world example demonstrates an end-to-end experimental and theoretical development of a physical system - something that could be useful to students and professionals interested in studying fluid mechanics and experimentation in engineering.

Social Links

See this social icon list in the original post

Related Tutorials

See this gallery in the original post

See this gallery in the original post

See More in Engineering:

See this content in the original post