Laser-Based Pallet Level Monitor With VL53L0X Time of Flight Sensor and ESPHome

by taste_the_code in Circuits > Gadgets

1656 Views, 13 Favorites, 0 Comments

Laser-Based Pallet Level Monitor With VL53L0X Time of Flight Sensor and ESPHome

Time of Flight Sensor (VL53L0x) for Distance Measuring in ESPHome - Pellet Level Monitor Update
2024-04-24 08-21-15.mp4.00_00_02_07.Still001.jpg
2024-04-24 08-21-15.mp4.00_00_12_28.Still002.jpg
MVI_1301.MP4.00_00_03_10.Still001.jpg

Hi everyone, in this Instructable, I'll show you how I created a new version of my pellet level monitor using a laser-based distance measuring sensor—the VL53L0X Time of Flight sensor.

A while back, I used an ultrasonic distance sensor for a pallet monitoring project in my home. Based on your feedback, today we're upgrading to the VL53L0X sensor, which uses laser technology instead of sound for more precise measurements.

The video and Instructable is sponsored by Altium Designer and Altium 365. You can get 25% discount on any new license by using this link: https://www.altium.com/yt/taste_the_code

Supplies

MVI_1300.MP4.00_00_16_20.Still001.jpg

Introduction to the VL53L0X Sensor

MVI_1302.MP4.00_00_04_26.Still001.jpg
MVI_1302.MP4.00_00_25_11.Still002.jpg

The VL53L0X Time of Flight (ToF) sensor marks a notable advancement in distance sensing technology. Unlike traditional ultrasonic sensors that rely on sound waves, the VL53L0X utilizes infrared laser light, which allows it to provide accurate distance measurements. This sensor sends out a short infrared light pulse and then measures the time it takes for the light to return after reflecting off an object. This measurement technique is not only quick but also effective across a variety of lighting conditions, making it a robust choice for many practical applications in electronic projects.

The compact size of the VL53L0X sensor is particularly advantageous for projects where space constraints are a consideration. Despite its small form factor, it offers impressive functionality, capable of detecting objects up to 2 meters away (1.2 meters in my case) under optimal conditions. This makes it incredibly useful for a wide range of applications, from robotic obstacle avoidance to user presence detection.

Wiring the Sensor

MVI_1307.MP4.00_00_11_19.Still001.jpg

To get started with the VL53L0X sensor, the first step involves wiring it correctly to your microcontroller. For this demonstration, I'll be using the NodeMCU, which is based on the ESP8266.

First, make sure that your NodeMCU and the VL53L0X sensor are powered off before you begin making any connections. Start by connecting the VCC pin on the VL53L0X to a 3.3V output on the NodeMCU. This provides power to the sensor. Next, connect the GND (ground) pin of the sensor to one of the GND pins on the NodeMCU to complete the power setup. 

For data communication, the VL53L0X uses the I2C protocol, which requires two lines: SDA (Data Line) and SCL (Clock Line). Connect the SDA pin on the sensor to the D2 pin on your NodeMCU, and the SCL pin to the D1 pin. This setup will enable the NodeMCU to send and receive data from the sensor. After these connections, your hardware setup should be ready to go, and you can move on to programming the NodeMCU to interact with the VL53L0X sensor.

Testing the Sensor With Arduino

2024-04-24 08-34-38.mp4.00_00_38_26.Still001.jpg
MVI_1309.MP4.00_00_38_01.Still001.jpg

Once you have the VL53L0X sensor all wired up to your Arduino, it’s time to test it out to ensure everything is working correctly.

I used the Adafruit VL53L0X library and once installed I loaded the default example sketches that come with the library.

Next, I uploaded the test sketch to the NodeMCU. Here, you need to make sure your board and port settings match your actual hardware setup. Once the sketch is successfully uploaded, open the Serial Monitor from the Arduino IDE to view the output.

I moved my hand at different distances in front of the sensor to see how the readings change. The sensor measures the distance by calculating the time it takes for a laser to bounce back from an object, so you should see the distance values changing as you move objects closer or further away from the sensor.

If you’re getting consistent distance readings that logically change as you alter the distance of objects from the sensor, then it's all set up correctly! If the readings seem off or erratic, double-check your connections and make sure that the sensor isn't facing any surface that might reflect the laser irregularly (like transparent or highly reflective materials). It's also a good chance to play around with the range and angles to see the effective detection range of your setup, optimizing it based on your specific project needs.

Setting Up ESPHome Component

2024-04-24 09-36-49.mp4.00_00_38_19.Still001.jpg
2024-04-24 09-36-49.mp4.00_01_19_01.Still002.jpg

To integrate the VL53L0X Time of Flight sensor into an ESPHome setup, you'll need to go through a series of steps to ensure that both hardware connections and software configurations are correctly established. This will allow you to use the sensor in conjunction with an ESP8266 or ESP32 running ESPHome, which can then be integrated into platforms like Home Assistant.

The wiring in this case is exactly the same as with the Arduino version and the trick is to set the device with the right components in the YAML configuration.

ESPHome comes with a built-in component for the VL53L0X sensor so this is relatively easy. We just need to make sure that we also set up the I2C bus component with the right pins.

The full code is available on my website.

Finally, compile and upload your code to the ESP device. Once the upload is successful, you can monitor the sensor readings either directly through the ESPHome logs or through a Home Assistant dashboard if integrated.

For a better view of the data, I also created a gauge card in my dashboard, and you can find its code as well on my website.

Next Steps

MVI_1301.MP4.00_00_48_03.Still002.jpg

Now that the VL53L0X Time of Flight sensor is successfully integrated with ESPHome, my plan is to combine it in a single device with the previous ultrasonic version so I can compare them side by side for some period. I don't suspect that they will be very different but it will be interesting to see how they operate.

Based on the level, you can also add alerts and notifications in Home Assistant so that you can be notified when to fill in the tank.

Since there was some interest already, I will also work on a stand-alone version of the sensor, where you can connect it to your home WiFi and have a web page that you can access to get the level.

If you are interested in seeing that, then be sure to subscribe to my YouTube channel and in the meantime, check my other Instructables.