Cerca codice

Measuring Distance with a Laser Time-of-Flight VL53L0X Sensor Using Arduino

Measuring Distance with a Laser Time-of-Flight VL53L0X Sensor Using Arduino

In this tutorial, we will explore how to measure distances using the VL53L0X laser time-of-flight sensor with an Arduino. This sensor allows for precise distance measurements and can be integrated into various projects, such as robotics or automation systems. By the end of this tutorial, you will be able to set up the sensor, wire it correctly, and write the necessary code to obtain distance readings.

We will cover the hardware components required, wiring instructions, and a detailed explanation of the code that operates the VL53L0X sensor. For a more visual representation of the process, be sure to check out the associated video (in video at 00:15).

Hardware Explained

The primary component of this project is the VL53L0X sensor. This is a time-of-flight distance sensor that works by emitting a laser beam and measuring the time it takes for the reflected light to return to the sensor. It provides distance measurements in millimeters and is capable of operating over a range of distances, making it suitable for various applications.

In addition to the VL53L0X, you will need an Arduino board to process the sensor data. The Arduino will communicate with the sensor via the I2C protocol, which allows for easy integration and control. You will also require jumper wires to establish connections between the sensor and the Arduino.

Datasheet Details

Manufacturer STMicroelectronics
Part number VL53L0X
Logic/IO voltage 1.8 V to 3.6 V
Supply voltage 2.6 V to 3.6 V
Output current (per channel)
Peak current (per channel)
PWM frequency guidance
Input logic thresholds
Voltage drop / RDS(on) / saturation
Thermal limits -40 °C to 85 °C
Package QFN-16
Notes / variants Time-of-flight sensor, I2C interface

 

  • Ensure proper voltage levels to avoid damaging the sensor.
  • Use pull-up resistors on the I2C lines if necessary.
  • Keep the sensor clean and unobstructed for accurate readings.
  • Calibrate the sensor for specific environments to improve accuracy.
  • Check for timeout errors in the code to handle measurement issues.

Wiring Instructions

To wire the VL53L0X sensor to the Arduino, start by connecting the VCC pin on the sensor to the 5V pin on the Arduino. This is safe because the sensor has an internal regulator that allows it to operate with a 5V supply while still functioning at 3.3V levels. Next, connect the GND pin on the sensor to the ground pin on the Arduino.

Now, connect the SDA pin on the sensor to the A4 pin on the Arduino, which is the default I2C data line. Then, connect the SCL pin on the sensor to the A5 pin on the Arduino, which is the default I2C clock line. For other Arduino models, the SDA and SCL pins may vary; for example, on the Arduino Mega, use pins 20 and 21, respectively. Ensure all connections are secure before powering on the system.

Code Examples & Walkthrough

In the Arduino code, we begin by including the necessary libraries and initializing the VL53L0X sensor. In the setup() function, we set up the serial communication and initialize the sensor:

void setup() {
  Serial.begin(9600);
  Wire.begin();
  sensor.init();
  sensor.setTimeout(500);
  sensor.startContinuous();
}

This snippet initializes the sensor and starts continuous measurement mode. The setTimeout(500) function ensures that the sensor will not hang indefinitely if it fails to read a distance.

In the loop() function, we read the distance and print it to the serial monitor:

void loop() {
  int distance = sensor.readRangeContinuousMillimeters();
  distance = distance - 55; // Compensate for error
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.print("mm");
  if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
  Serial.println();
}

This section of the code retrieves the distance measurement and compensates for any systematic error by subtracting a constant value. It also checks for timeouts in case the sensor fails to acquire a reading.

Demonstration / What to Expect

When you run the program, you should see continuous updates of the distance measured by the VL53L0X sensor in the serial monitor. The readings will display in millimeters. If you move an object closer or farther from the sensor, the distance values will change accordingly. If there's a timeout, you will see a "TIMEOUT" message indicating that the sensor failed to read the distance (in video at 05:30).

Video Timestamps

  • 00:00 - Introduction
  • 00:15 - Overview of components
  • 01:00 - Wiring instructions
  • 02:30 - Code walkthrough
  • 05:30 - Demonstration
16-Source code for the VL523L0X distance measurement module for Arduino
Lingua: C++
/*
 * Questo esempio mostra come utilizzare la modalità continua per effettuare misurazioni di distanza con il VL53L0X. Si basa su vl53l0x_ContinuousRanging_Example.c dell'API VL53L0X.
 * 
 * Le letture della distanza sono espresse in mm.
 * 
 * Fonte originale: https://github.com/pololu/vl53l0x-arduino
 * Modificato da Ahmad Shamshiri per RoboJax.com
 * Data della modifica: 26 set 2017
 */
#include <Wire.h>
#include <VL53L0X.h>

VL53L0X sensor;

void setup()
{
  Serial.begin(9600);
  Wire.begin();

  sensor.init();
  sensor.setTimeout(500);

 // Avvia la modalità continua consecutiva (prendi letture come
 // il più velocemente possibile). Per utilizzare la modalità temporizzata continua
 // invece, fornire un periodo di intermisura desiderato in
 // ms (ad esempio sensor.startContinuous(100)).
  sensor.startContinuous();
}

void loop()
{
  int distance =sensor.readRangeContinuousMillimeters();
 distance = distance -55; // -55 è per compensare l'errore. Modificalo o impostalo su zero per farlo funzionare con il tuo sensore.
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.print("mm");
  if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }

  Serial.println();
}
17-Source code for VL53L0X distance measurement using two sensor modules for Arduino
Lingua: C++
++
/*
 * Questo esempio mostra come utilizzare la modalità continua per effettuare misurazioni di distanza con due sensori VL53L0X. Si basa su vl53l0x_ContinuousRanging_Example.c dell'API VL53L0X.
 * 
 * Le letture della distanza sono in unità di mm.
 * 
 * Fonte originale: https://github.com/pololu/vl53l0x-arduino
 * Modificato da Ahmad Shamshiri per RoboJax.com
 * Data di modifica: 06 giugno 2018
 * Dennis da YouTube ha richiesto questo codice il 06 giugno 2018
 */


#include <Wire.h>
#include <VL53L0X.h>

VL53L0X sensor1; // definire oggetto per il sensore 1
VL53L0X sensor2; // definire oggetto per il sensore 2

void setup()
{
  Serial.begin(9600);
  Wire.begin();

  sensor1.init(); // inizializza sensore 1
  sensor1.setTimeout(500); // imposta il timeout per il sensore 1

  sensor2.init(); // inizializza il sensore 2
  sensor2.setTimeout(500); // imposta timeout per il sensore 2


 // Avvia la modalità continua ininterrotta (prendi letture mentre
 // il più veloce possibile). Per utilizzare la modalità continua con timer
 // invece, fornire un periodo di intermisura desiderato in
 // ms (ad es. sensor.avviaContinuo(100)).
  sensor1.startContinuous(); // misura in modo continuo per il sensore 1
  sensor2.startContinuous(); // misura continuamente per il sensore 2
}

void loop()
{
  int distance1 =sensor1.readRangeContinuousMillimeters(); // ottieni distanza per il sensore 1
  int distance2 =sensor2.readRangeContinuousMillimeters(); // ottieni distanza per il sensore 2

 distance1 = distance1 -55; // -55 deve essere utilizzato per compensare l'errore. Modificalo o impostalo su zero per farlo funzionare con il tuo sensore.
 distance2 = distance2 -34; // -35 è per compensare l'errore. Cambialo o impostalo su zero per farlo funzionare con il tuo sensore.

  Serial.print("Distance 1: ");
  Serial.print(distance1); // stampa la distanza dal sensore 1
  Serial.print("mm Distance 2: ");
  Serial.print(distance2); // stampa la distanza dal sensore 2
  Serial.print("mm");
  if (sensor1.timeoutOccurred()) { Serial.print(" TIMEOUT 1"); }
  if (sensor2.timeoutOccurred()) { Serial.print(" TIMEOUT 2"); }

  Serial.println();
}

Risorse e riferimenti

Nessuna risorsa ancora.

File📁

Nessun file disponibile.