Other Arduino Codes and Videos by Robojax

Measure Distance with Laser VL53L0X 6 pin module with LCD1602-I2C for Arduino Robojax.com

Measure Distance with Laser VL53L0X 6 pin module with LCD1602-I2C for Arduino

This code is to measure distance using laser module ToF Time of Flight VL53L0X 6 pin module. This module is sold on eBay or AliExpress in different color but mostly (as of June 2018) sold in pink color.

In the video you will see how to use this code with LCD1602-I2C or LCD2004-I2c. The distance is selectable as mm or cm.

   /* 

This is Arduino code to measure distance with VL53L0X and display it on LCD1602 with I2C module
Distance is displayed in mm (melimeter) or cm (centimeter).

Original Laser Sensor source source: https://github.com/adafruit/Adafruit_VL53L0X
Modefied by Ahmad Shamshiri for RoboJax.com
Date modefied: Jun 28, 2018 at 19:06 in Ajax, Ontario, Canada

Watch the video instruction for this code: https://youtu.be/t14ly7Y09oI
You can get this code from Robojax.com
Pin connection

VL53L0X Pin  Arduino Pin
VCC         5V
GND         GND
SDA         A4 or SDA if available
SCL         A5 or SCL if available
GPIO1       leave it unconnected
XSHUT       D12 (digital 12 or pin 12)

LCD1602 Pin

*/


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

VL53L0X sensor;


#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

const int type = 1;// 1=mm , 2= cm
String unit;// variable for unit, mm or cm
void setup()
{

  Serial.begin(9600);
  Wire.begin();

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

  // Start continuous back-to-back mode (take readings as
  // fast as possible).  To use continuous timed mode
  // instead, provide a desired inter-measurement period in
  // ms (e.g. sensor.startContinuous(100)).
  sensor.startContinuous();
  
  // initialize the LCD, 
  lcd.begin();
  // Turn on the blacklight and print a message.
  lcd.backlight(); 
}

void loop()
{
  int distance =sensor.readRangeContinuousMillimeters();
  //int distance =sensor.startContinuous(100);

 //distance = distance;
  Serial.print("Distance: ");

  //start of loop Robojax code for LCD with I2C
  lcd.clear();
  lcd.print("Robojax VL53L0X");
  lcd.setCursor (0,1); // go to start of 2nd line
  lcd.print("Dist.: ");
  lcd.setCursor (7,1); // go to start of 2nd line
  if(type ==2){
    float distanceCM = (float) (distance/10.0);
      unit ="cm";
      lcd.print(distanceCM);
      Serial.print(distanceCM);
      Serial.print(unit);      
    }else{
    unit ="mm";
      lcd.print(distance);
      Serial.print(distance);
      Serial.print(unit);       
    }  

  lcd.print(unit);    
  
  if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }

  Serial.println();
  delay(100);
}