🛠 Lire le capteur de distance (ToF) embarqué
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
- Arduino Nicla Vision (the distance sensor is integrated into the board, no wiring required)
- Arduino IDE + "Arduino Mbed OS Nicla Boards" core
- >
- Note: unlike the Nicla Sense ME, the Nicla Vision does not include a humidity, pressure, or gas sensor; the relevant environmental sensor here is the ToF, complemented by the LSM6DSOXTR IMU.
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
- Looking for the sensor on the external I²C bus (D11/D12): embedded sensors are on the internal bus PF_0/PF_1 managed by the core; the standard Wire library in the core targets the external bus — read the NICLA_VISION core documentation and select the correct peripheral.
- Confusing it with the Nicla Sense ME: there is no BME680/HTS221 on the Vision; any attempt to read humidity or pressure will fail.
- Incorrect registers: the code above assumes a VL53L0x mapping; >
- Non-reflective surface: absorbing black surfaces and grazing angles result in erratic readings.
Other tutorials for this board
Code examples for this board
led_rgb.inoArduinoimu_lsm6dsox.inoArduinotof_distance.inoArduinomicro_pdm.inoArduinocamera_capture.inoArduinowifi_scan.inoArduino