Build a Distance Meter With LED Indicators and a Live LCD Readout
by pouyan-asgharian in Circuits > Arduino
28 Views, 0 Favorites, 0 Comments
Build a Distance Meter With LED Indicators and a Live LCD Readout
I built a distance meter using a SparkFun VL53L4CD time-of-flight sensor and an Arduino board. A 16×2 LCD shows the live distance in millimeters, and three LEDs indicate how close the object is.
I needed to measure distance for another project, so I built this prototype to see how well the sensor worked. The LCD gives me the exact reading, and the LEDs let me see the distance range at a glance.
Supplies
Required components
- SparkFun RedBoard Plus, an Arduino Uno–compatible board
- SparkFun VL53L4CD Qwiic distance sensor and Qwiic cable
- 5 V, 16×2 LCD with a 16-pin parallel interface
- 10 kΩ potentiometer for LCD contrast
- Three LEDs, one red, one yellow, and one green
- Three 100 Ω resistors, one for each LED
- One 100 Ω or 220 Ω resistor for the LCD backlight, depending on your LCD module
- Breadboard and jumper wires
- USB cable to power and program the RedBoard from a laptop
Introduction
For one of my projects, I needed a simple way to measure how far away an object was. After looking at a few options, I chose the SparkFun VL53L4CD. It’s a compact, affordable time-of-flight (ToF) sensor that measures distance by timing how long it takes light to travel to an object and back.
SparkFun lists a precision of 1 mm, a minimum reading distance of 1 mm, and an accuracy of about ±7 mm under indoor test conditions with a white target and no interfering infrared light. The sensor has a typical 18° field of view and can take up to 100 readings per second. The breakout operates from 2.6 to 3.5 V and gets 3.3 V through the Qwiic cable in this build. SparkFun VL53L4CD specifications
I tested the sensor before building this project. You can find my test setup and results here on GitHub.
In this build, a SparkFun RedBoard Plus reads the sensor and displays the live distance in millimeters on a 16×2 LCD. Three LEDs let you tell the distance range at a glance.
Distance
- Less than 50 mm ---> Red
- 50–120 mm, -- > Yellow
- More than 120 mm ---> Green
Here’s a quick look at the finished circuit in action.
Connect the Distance Sensor
Plug one end of the Qwiic cable into the VL53L4CD breakout and the other end into the RedBoard Plus’s Qwiic port. I powered the finished circuit from my laptop over USB.
Wire the LCD and Adjust the Contrast
With the USB cable unplugged, set the RedBoard’s LOGIC switch to 5V. Use the circuit diagram and pin table below to wire the LCD. For the backlight, connect LCD pin 15 (A or LED+) to 5 V through a series resistor. Check your LCD module’s specifications when choosing the resistor value. Arduino LCD wiring guide
Connect the two outer legs of the 10 kΩ potentiometer to 5 V and GND, one leg to each. Connect the center leg to LCD pin 3 (VO). Once you’ve powered the circuit and uploaded the main sketch, turn the knob until the text is easy to read.
Connect the LEDs
For each LED, connect the longer leg (anode) to its assigned digital pin through a 100 Ω resistor. Connect the shorter leg (cathode) to GND.
- Green ---> D4
- Yellow ---> D5
- Red ---> D6
I built the prototype on a breadboard with jumper wires. I used the side rails to distribute 5 V and GND to the components that needed them. For a permanent build, you could transfer the circuit to a PCB.
Calibrate the Sensor
I used two Arduino sketches, one to calibrate the sensor and one to run the distance indicator. The calibration sketch is based on SparkFun’s example. In my setup, I had to add distanceSensor.startRanging(); before the sketch could detect the target and begin the countdown.
- Upload the calibration sketch and open the Serial Monitor.
- Hold a flat target less than 10 cm from the sensor for at least one second.
- When the five-second countdown starts, move the target to 140 mm from the front of the sensor and hold it steady.
- Record the offset reported by the calibration sketch so you can apply it in the main sketch.
I used white paper for my test. The calibration procedure this example is based on specifies a target with 17% reflectance at 940 nm. STMicroelectronics calibration guide
Upload and Test the Main Sketch
Install the SparkFun VL53L1X Arduino Library, which also supports the VL53L4CD breakout, and the LiquidCrystal library. In the main sketch, apply your measured offset with setOffset() after the sensor initializes successfully. SparkFun sensor hookup guide
Upload the main C++ sketch below. I’ve added comments to explain how it reads the sensor, updates the LCD, and selects an LED.
To test the finished circuit, move a flat object to about 30 mm, 80 mm, and 150 mm from the sensor. You should see the red, yellow, and green LEDs light up in that order as the LCD updates.