Measure distance using VL53L0X ToF LASER Range sensor + Arduino + OLED (mm/cm/in)
Hi, this tutorial is about distance measuring using the GY-VL53L0X ToF sensor, with an Arduino Uno board, we’ll go through a test then a little project using an OLED screen and a pushbutton to measure in different units (Metrics, Imperial).
So the sensor uses the ToF (Time of Flight) technique, it sends a LASER beam to the target and as long as it’s in the range, the LASER reflects back and get detected by the sensor, the time duration between emitting and receiving is measured, the velocity which is already known (approx = c, speed of light, and if there’s a mistake, a calibration is always possible), we can then calculate the distance, Distance = Velocity * Time.

One of the advantages of the ToF method, is that it can reach higher ranges, of course depending on the module used, here as usual we’re using 2-3$ ones, but it’s a very precise module thanks to the fine LASER used, not like the Ultrasound modules, or IR ones, that spreads a wide wave, and can mislead the readings, but they have their own useful applications though.
Range and accuracy
The module’s range depends on the surface color and the mode, there are two modes: default one (we’re using) and the long range one, in the default mode it can handle up to 1200mm (1,2m ; 40 in).

Hardware and parts
So for this project, alongside some jump wires we’ll be using:
The module is actually very small, around (1.5cm by 1cm) very useful for small applications like ToF10120
Wirings
Wiring 1: Direct wiring of the module

Wiring 2: Adding OLED i2c display and a push button

Libraries
- VL53L0X Adafruit library: Download here from Github or Download here.
- OLED libraries:
- Adafruit OLED SSD1306 library:Â Download here from Github or Donwload here.
- Adafruit GFX Library Download here from Github or Download here.
Codes
Check the codes below, or download them here. The results can be found below too.
Code 1: Library example – VL53L0x
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); void setup() { Serial.begin(115200); // wait until serial port opens for native USB devices while (! Serial) { delay(1); } Serial.println("Adafruit VL53L0X test"); if (!lox.begin()) { Serial.println(F("Failed to boot VL53L0X")); while(1); } // power Serial.println(F("VL53L0X API Simple Ranging example\n\n")); } void loop() { VL53L0X_RangingMeasurementData_t measure; Serial.print("Reading a measurement... "); lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout! if (measure.RangeStatus != 4) { // phase failures have incorrect data Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter); } else { Serial.println(" out of range "); } delay(100); } |
Code 2: Using OLED and push button
The second code that uses the OLED and button you can download it here.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
/* This code works with VL53L0X ToF sensor + 128x32 OLED screen and a pushbutton * It displays the distance in mm at default, then if you press the button it switches to cm then to in * and it comes back to mm, if Out of Range it will display OoR * For more details check http://www.surtrtech.com */ #include <Adafruit_GFX.h> //OLED display and VL53L0X Libraries #include <Adafruit_SSD1306.h> #include <Adafruit_VL53L0X.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display) Adafruit_VL53L0X lox = Adafruit_VL53L0X(); //Declaring sensor entity int Button1, State=1; //Button1 is our push button, State=1 means the button state and there are 3 (mm/cm/in) //that increases by pressing the button void setup() { lox.begin(); //Start the VL53L0X display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display display.clearDisplay(); display.setTextSize(2); //The following is just a message to display (VL53L0X LASER Range...) alongside a delay display.setTextColor(WHITE); display.setCursor(17,5); display.println("VL53L0X"); display.setTextSize(1); display.setCursor(0,23); display.println("LASER Range Measure"); display.display(); delay(3000); display.clearDisplay(); display.setTextSize(3); } void loop() { Button1=digitalRead(2); //We're constantly reading the button wired on D2 if it's pressed or not if(Button1==HIGH){ //if the button is pressed if(State<3) //We check the State value it should be 1 or 2 State+=1; else //if it's 3 we go back to one State=1; delay(200); //a delay is needed so the push won't be counted twice or more times } VL53L0X_RangingMeasurementData_t measure; //Gettings the measures lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout! //avoid true in this case to not cause problems if (measure.RangeStatus != 4) { // 4 means there's incorrect or out of range reading display.clearDisplay(); display.setCursor(0,0); if(State==1){ //Depending on the button state we will display mm, cm or in display.print(measure.RangeMilliMeter); display.print(" mm"); } if(State==2){ float Cm_m=measure.RangeMilliMeter*0.1; //Conversion, it's better to use floats as there are decimals display.print(Cm_m,1); display.print("cm"); } if(State==3){ float in_m=measure.RangeMilliMeter/25.4; //Conversion to inches also a float display.print(in_m); display.print("in"); } display.display(); Serial.println(); delay(50); display.clearDisplay(); } else { //if the target is out of range display.setTextSize(3); display.setCursor(0,0); display.print("OoR"); //We display OoR that stands for Out of Range, I couldn't get it printed all, check the problem at the website display.display(); return; } //You can add a delay here to avoid flickering } |
Tests
First test with the Serial monitor

If the target is out of range

Second test using OLED + push button
The strange part here, is that I couldn’t write some sentences in the OLED screen, like as Out of range, I just wrote OoR, I wanted to add “distance” while measuring but couldn’t too, the whole OLED goes black, also there are some strange spots on the bottom right, if someone can explain please welcome. Otherwise here are the results:
Out of range case

Showing result in mm

Switching to cm

changing to inches

You can of course add or remove units as you want, it’s simple. Check the video tutorial if you need help, or want to see the demo.
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.

what resistor did you use?