tof_distance.ino

Arduino Nicla Vision · File : tof_distance.ino · Language : Arduino · Use case : Mesurer une distance avec le capteur ToF

The Nicla Vision carries a Time-of-Flight distance sensor (VL53Lx family per docs-db, exact part not published). This sketch runs it in continuous mode and prints millimeters — tank level, presence, contactless gesture.

Hardware

An Arduino Nicla Vision (STM32H7 M7+M4, 23×23 mm) with GC2145 camera, LSM6DSOX IMU, PDM mic, ToF (VL53Lx), ESP32 Wi-Fi/BLE module and RGB LED (LEDR/G/B). The board alone; the ToF is soldered on board with its window on the front edge. No external module needed.

Wiring

No wiring: the ToF sits on the internal I2C. For an external VL53L1X, the connector Wire/Wire1 bus runs the same code.

How it works

VL53L1X.init() detects the sensor (else try the VL53L0X library — the exact family is not guaranteed); startContinuous paces readings and readRange returns millimeters, useful range ~4 m in long mode.

Running it

Programmed from the Arduino IDE (ArduinoCore-mbed, NICLA_VISION target) over USB-C; Camera, PDM, Arduino_LSM6DSOX, WiFiNINA and ArduinoBLE libraries provided.

Variants

Change timing (15/30/50 ms) and mode (short/mid/long range), average N readings to stabilize, or turn distance into a fill level in %.

tof_distance.ino

// Capteur de distance Time-of-Flight embarqué (famille VL53Lx,
// référence exacte non listée par la doc Arduino — la bibliothèque
// ST VL53L1X fonctionne sur ce bus I2C interne).
// Bibliothèque : ST STM32duino VL53L1X + VL53L0X selon le cas.

#include <Wire.h>
#include <VL53L1X.h>

VL53L1X tof;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  tof.setTimeout(500);
  if (!tof.init()) {
    Serial.println("ToF absent (essayer la lib VL53L0X)");
    while (1) { ; }
  }
  tof.startContinuous();          // mesure en continu, 50 ms
  Serial.println("ToF prêt — approchez la main");
}

void loop() {
  uint16_t mm = tof.readRangeContinuousMillimeters();
  if (tof.timeoutOccurred()) {
    Serial.println("Timeout ToF");
  } else {
    Serial.print("Distance : ");
    Serial.print(mm);
    Serial.println(" mm");
  }
  delay(100);
}

← Back to the board