Arduino Modulino Distance Tutorial Featuring an STM ToF Sensor

Summary

Article highlights:

  • The Arduino Plug and Make kit includes seven Qwiic modules known as Modulinos.

  • The Distance Modulino features an STM Time-of-Flight (ToF) sensor able to accurately measure distances up to about 1 meter.

  • Combine the Modulino Distance and Modulino Buzzer modules to make a musical instrument. This provides a highly interactive way to interact with the ToF sensor.

  • A cell phone camera may be used to view the ToF sensor’s laser emitter.

  • The Arduino Modulino is easy to use - software included.

What is the Arduino Distance Modulino?

The Modulino Distance is one of several modules included in the Arduino Plug and Make kit. It features Qwiic connections providing easy a breadboard-free connection to the Arduino microcontroller and other Modulinos included in the kit.

The Modulino Distance module (ABX00102), as pictured in Figure 1, is a breakout board for the STMicroelectronics Time-Of-Flight (ToF) sensor model VL53L4CDV0DH/1 sensor. This sensor features a 940 nm infrared (IR) laser to measure distances from 0 to 3.9 ft (1200 mm). An I2C interface is used with compatiblty with other members of the Arduino Modulino family. Note that the Distance module is different than other Modulinos as the breakout board does not include the dedicated ARM microcontroller such as used on the Pixels.

Figure 1: Image of the Arduino Modulino Distance module with the STM ToF in the center.

What is a Time-of-Flight (ToF) Sensor?

The ToF operates like a radar. It calculates a distance to an object based on the time of flight of a reflected pulse. Instead of a microwave pulse, it sends an IR pulse from a laser.

The featured STM VL53L4CDV0DH/1 sensor is designed to provide a distance calculation to an object independent of the object’s color or reflectivity. The STM sensor uses a 940 nm (infrared) laser emitter. Closely spaced in the same surface mounted body is a SPAD (Single Photon Avalanche Diode). As implied by the name the “receiver” is very sensitive to the emitter’s energy as reflected from the target.

Have you ever used one of those restroom automatic faucets or an automatic hand towel dispenser? Chances are high that the device featured a ToF sensor similar to the one featured in this article. This sensor is designed for low power battery applications. It includes features such as interrupt-on-detection of an object. This allows the microcontroller to sleep in a low energy state until it is needed.

Tech Tip: The STM ToF sensor is ever so slightly sensitive to color, ambient light, and infrared emission. Targets of different colors are used to characterize the sensor. These targets are identified using a Munsell code such as N4.74 to indicate a gray target with 17% reflectance. This is similar to the sample color paint swatch cards obtained from the hardware store.

Modulino class-based software

Arduino has provided a carefully constructed class-based interface for all Arduino modulino devices. This provides a consistent user interface across all Modulino devices. The critical code is included as part of the Modulino.h file:

 #include <Modulino.h>

The Distance module has three associated methods including:

  • begin( ); initialize the module.
  • available( ); Returns a Boolean indicating if the sensor is actively detecting an object. From a practical perspective, this acts as an indication that no object detected within the 3.9 ft window.
  • get( ); return the distance to the measured object in mm.

Tech Tip: The infrared emitter is not visible to the human eye. However, most cell phones can detect the emitter, as shown in Figure 2. Note that the cell phone camera is specifically designed to filter out infrared. However, it’s not perfect, and a small but perceptible amount still passes.

Figure 2: The ToF infrared emitter may be seen when a cell phone camera is held a few inches above the sensor. It appears as a purple dot.

Example code

The following code presents a working demo of the Distance module as part of a theremin. The Distance and Buzzer Modulinos work together to make a crude instrument that changes pitch based on the measured distance. The Arduino Plug and Make setup is shown in Figure 3 with a short demonstration video included as Video 1. Note that the system is fast, allowing a vibrato-like sound as the board is shaken. This responsiveness could be improved by increasing the I2C clock speed and using a buzzer driven directly from one of the microcontroller’s hardware timers. The datasheet identifies a 100 Hz refresh rate.

#include "Modulino.h"

ModulinoDistance distance;
ModulinoBuzzer buzzer;

void setup() {
  //  Serial.begin(19200);
  Modulino.begin();
  distance.begin();
  buzzer.begin();
}

void loop() {
  if (distance.available()) {
    int measure = distance.get();
    buzzer.tone(measure << 1, 15);
    //    Serial.println(measure);
    delay(10);
  }
}

Figure 3: The Distance and Buzzer Modulinos installed on the Arduino Plug and Make kit’s base along with an UNO R4 WiFi.

Video 1: Spooky Halloween-like music using the Arduino theremin. Note the responsive vibrato.

Next steps:

Here are a few ideas for you to explore:

  • Determine which materials are best detected by the sensor. For example, could the ToF be used as a tank level indicator for a 5-gallon bucket or even a 2-liter bottle?

  • Mount the ToF sensor on an RC servomotor. This could operate as a radar scanner allowing your robot to detect and avoid obstacles.

  • Explore concepts such as field of view to determine where the sensor’s attention is focused.

  • Explore “windowed” enclosures that protect the sensor while allowing to see the world. An example is the restroom sink where we must protect the sensor from getting wet.

  • Explore low energy sensor to microcontroller application for extended run time for battery powered devices.

  • Add a ToF to a quadcopter to hover at a specific distance above the ground. This adds many layers of complexity, as you must contend with inconsistent reflections from different material, as well the infrared energy from the sun.

Parting thoughts

The Distance Modulino is a fun component of the Arduino Plug and Make kit. The theremin provides a very good introduction as it provides near real-time audio response. With this setup we have immediate feedback allowing us to intuit the system’s characteristics including the speed of operation.

Please like this post if you learned something new. Also, let’s expand the next steps section. What else could we do with this sensor?

Best wishes,

APDahlen

Related Information

Please follow these links to related and useful information:

About this author

Aaron Dahlen, LCDR USCG (Ret.), serves as an application engineer at DigiKey. He has a unique electronics and automation foundation built over a 27-year military career as a technician and engineer which was further enhanced by 12 years of teaching (interwoven). With an MSEE degree from Minnesota State University, Mankato, Dahlen has taught in an ABET-accredited EE program, served as the program coordinator for an EET program, and taught component-level repair to military electronics technicians. Dahlen has returned to his Northern Minnesota home and thoroughly enjoys researching and writing articles such as this.
LinkedIn | Aaron Dahlen - Application Engineer - DigiKey