Skip to main content

VL53L1X ToF Laser Distance Sensor - Measure on Interrupt (MicroPython)

An example showing how to configure the sensor to initiate an interrupt when the measurement is within, outside, below or above a certain threshold. Useful because it lets the MCU react immediately to changes without constantly checking the sensor, saving power and improving efficiency.

Interrupt Example

from machine import I2C, Pin
from VL53L1X import VL53L1X
import time

# Global variable that checks if an interrupt was detected
detected = 0

# Function that executes when an interrupt happens
def my_interrupt_handler(pin):
global detected
# Set the variable to 1 if an interrupt was triggered
detected = 1

# Initialize the sensor and define the interrupt pin as well as the handler function
# that will be called. The pin defined must be connected to the GPIO1 pin on the breakout board!
sensor = VL53L1X(interruptPin=34, interruptCallback=my_interrupt_handler)

# Set the threshold when the interrupt will be triggered in a range of millimeters
sensor.set_distance_threshold_interrupt(
100, 300, window="out"
) # Trigger when outside of 100–300mm

# Infinite loop
while True:
# The sensor itself starts taking continous measurements, must be called when using interrupts
sensor.start_ranging()
# If the 'detected' variable was set to 1
if detected:
dist = sensor.read() # Save the sensor measurement into a variable
print("Object outside of range window!")
print("Distance:", dist, "mm") # Print out the distance to the console

detected = 0 # reset the detection variable

sensor.set_distance_threshold_interrupt()

Configure distance threshold interrupt window. Based on measured range and specified threshold trigger an interrupt.

Function parameters:

TypeNameDescription
intlow_mmLower threshold.
inthigh_mmUpper threshold.
stringwindowParameter to specify when to trigger an interrupt.
ValueDescription
InWill be triggered if the range is within the threshold specified
OutWill be triggered if the range is outside the threshold specified
AboveTriggered if the measurement is above the upper defined threshold
BelowTriggered if the measurement is below the lower defined threshold

sensor.start_ranging()

Start continuous ranging mode.

Returns type: None

MeasureOnInterrupt.py

Measure on interrupt full example on GitHub