Código de búsqueda

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
Idioma: C++
/*
 * Este ejemplo muestra cómo utilizar el modo continuo para realizar mediciones de distancia con el VL53L0X. Se basa en vl53l0x_ContinuousRanging_Example.c de la API de VL53L0X.
 * 
 * Las lecturas de distancia están en unidades de mm.
 * 
 * Fuente original: https://github.com/pololu/vl53l0x-arduino
 * Modificado por Ahmad Shamshiri para RoboJax.com
 * Fecha de modificación: 26 de sep de 2017
 */
#include <Wire.h>
#include <VL53L0X.h>

VL53L0X sensor;

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

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

 // Iniciar el modo continuo de lectura continua (tomar lecturas como
 // tan rápido como sea posible). Para usar el modo continuo cronometrado
 // en su lugar, proporcione un período de inter-medición deseado en
 // ms (por ejemplo, sensor.startContinuous(100)).
  sensor.startContinuous();
}

void loop()
{
  int distance =sensor.readRangeContinuousMillimeters();
 distance = distance -55; // -55 es para compensar el error. Cámbialo o configúralo en cero para que funcione con tu sensor.
  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
Idioma: C++
++
/*
 * Este ejemplo muestra cómo usar el modo continuo para tomar mediciones de distancia con dos sensores VL53L0X. Se basa en vl53l0x_ContinuousRanging_Example.c de la API VL53L0X.
 * 
 * Las lecturas de distancia están en unidades de mm.
 * 
 * Fuente original: https://github.com/pololu/vl53l0x-arduino
 * Modificado por Ahmad Shamshiri para RoboJax.com
 * Fecha de modificación: 06 de junio de 2018
 * Dennis de YouTube solicitó este código el 06 de junio de 2018.
 */


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

VL53L0X sensor1; // definir objeto para el sensor 1
VL53L0X sensor2; // definir objeto para sensor 2

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

  sensor1.init(); // inicializar sensor 1
  sensor1.setTimeout(500); // establecer tiempo de espera para el sensor 1

  sensor2.init(); // inicializar sensor 2
  sensor2.setTimeout(500); // establecer tiempo de espera para el sensor 2


 // Iniciar modo continuo consecutivo (tomar lecturas como
 // tan rápido como sea posible). Para usar el modo continuo de temporizador
 // en su lugar, proporcione un período de intermedición deseado en
 // ms (por ejemplo, sensor.startContinuous(100)).
  sensor1.startContinuous(); // medir continuamente para el sensor 1
  sensor2.startContinuous(); // medir continuamente para el sensor 2
}

void loop()
{
  int distance1 =sensor1.readRangeContinuousMillimeters(); // obtener distancia para el sensor 1
  int distance2 =sensor2.readRangeContinuousMillimeters(); // obtener distancia para el sensor 2

 distance1 = distance1 -55; // -55 es para compensar el error. Cámbialo o configúralo en cero para que funcione con tu sensor.
 distance2 = distance2 -34; // -35 es para compensar el error. Cámbialo o configúralo a cero para que funcione con tu sensor.

  Serial.print("Distance 1: ");
  Serial.print(distance1); // imprimir distancia del sensor 1
  Serial.print("mm Distance 2: ");
  Serial.print(distance2); // imprimir distancia del sensor 2
  Serial.print("mm");
  if (sensor1.timeoutOccurred()) { Serial.print(" TIMEOUT 1"); }
  if (sensor2.timeoutOccurred()) { Serial.print(" TIMEOUT 2"); }

  Serial.println();
}

Recursos y referencias

Aún no hay recursos.

Archivos📁

No hay archivos disponibles.