🛠 Lire le capteur de distance (ToF) embarqué

Arduino Nicla Vision · debutant · Schéma : IMG-nicla-vision-debutant-3-niclavision-capteur-distance.svg — Nicla Vision avec le capteur ToF en façade émettant un cône de mesure vers un objet à 20 cm et le moniteur série affichant la distance

Objectives

Query the embedded time-of-flight (ToF) distance sensor on the Nicla Vision and display the distance in millimeters on the serial console.

Hardware

Complete Code

// Lecture brute I2C du ToF embarque (bus interne)
// > 
#include <Wire.h>

#define TOF_ADDR 0x29
#define REG_DISTANCE 0x51   // distance en mm sur VL53L0x (hypothese)

void setup {
  Serial.begin(115200);
  Wire.begin;             // bus I2C interne (PF_0/PF_1 cote core)
  delay(100);
  Wire.beginTransmission(TOF_ADDR);
  if (Wire.endTransmission != 0) {
    Serial.println);
  }
}

void loop {
  Wire.beginTransmission(TOF_ADDR);
  Wire.write(REG_DISTANCE);
  Wire.endTransmission(false);
  Wire.requestFrom(TOF_ADDR, (uint8_t)2);
  if (Wire.available >= 2) {
    uint16_t mm = (Wire.read << 8) | Wire.read;
    Serial.print(F("distance = ")); Serial.print(mm); Serial.println(F(" mm"));
  }
  delay(200);
}

Expected Verification

Bring your hand close to the front of the board: the printed value should decrease, typically showing a few hundred millimeters at rest. A "ToF absent" message indicates that you need to scan the I²C addresses (standard scan 0x08–0x77) to find the sensor on the internal bus.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials