From f308d05a91c483f135b81182b6272d9d98031ceb Mon Sep 17 00:00:00 2001 From: Kaan Ozen Date: Mon, 30 Mar 2026 14:41:26 +0200 Subject: [PATCH 1/2] feat(vl53l1x): Add proximity alert example with buzzer. --- lib/vl53l1x/examples/proximity_alert.py | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 lib/vl53l1x/examples/proximity_alert.py diff --git a/lib/vl53l1x/examples/proximity_alert.py b/lib/vl53l1x/examples/proximity_alert.py new file mode 100644 index 00000000..aedf37d7 --- /dev/null +++ b/lib/vl53l1x/examples/proximity_alert.py @@ -0,0 +1,39 @@ +from time import sleep_ms + +from machine import I2C, Pin +from pyb import Timer +from vl53l1x import VL53L1X + +DISTANCE_THRESHOLD_MM = 200 + +# sensor configuration +i2c = I2C(1) +tof = VL53L1X(i2c) + +#buzzer initialisation +buzzer_tim = Timer(1, freq=1000) +buzzer_ch = buzzer_tim.channel(4, Timer.PWM, pin=Pin("SPEAKER")) +buzzer_ch.pulse_width_percent(0) + +try: + while True: + distance = tof.read() + print("Distance: {} mm".format(distance)) + + if distance < DISTANCE_THRESHOLD_MM: + #frequency ranging from 1500 (really close) to 500 (at 200mm) + freq = (1500- (distance * 5)) + #security limits + freq = max(500, min(1500, freq)) + + buzzer_tim.freq(freq) + buzzer_ch.pulse_width_percent(50) + else: + buzzer_ch.pulse_width_percent(0) + + + sleep_ms(50) + + +except KeyboardInterrupt: + buzzer_ch.pulse_width_percent(0) #Properly cut the sound when the user presses CTLR+C From 55691fc7ed1f2c7dfa42b4d3d6e03d3f0351928f Mon Sep 17 00:00:00 2001 From: Kaan Ozen Date: Mon, 30 Mar 2026 16:38:22 +0200 Subject: [PATCH 2/2] fix(vl53l1x): Address review comments on proximity alert example. --- lib/vl53l1x/examples/proximity_alert.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/vl53l1x/examples/proximity_alert.py b/lib/vl53l1x/examples/proximity_alert.py index aedf37d7..0aba36e0 100644 --- a/lib/vl53l1x/examples/proximity_alert.py +++ b/lib/vl53l1x/examples/proximity_alert.py @@ -1,3 +1,9 @@ +"""Proximity alert using VL53L1X distance sensor and buzzer. + +Beeps the buzzer when an object is detected within 200 mm. The pitch +increases as the object gets closer. +""" + from time import sleep_ms from machine import I2C, Pin @@ -6,11 +12,11 @@ DISTANCE_THRESHOLD_MM = 200 -# sensor configuration +# Sensor configuration i2c = I2C(1) tof = VL53L1X(i2c) -#buzzer initialisation +# Buzzer initialisation buzzer_tim = Timer(1, freq=1000) buzzer_ch = buzzer_tim.channel(4, Timer.PWM, pin=Pin("SPEAKER")) buzzer_ch.pulse_width_percent(0) @@ -21,9 +27,9 @@ print("Distance: {} mm".format(distance)) if distance < DISTANCE_THRESHOLD_MM: - #frequency ranging from 1500 (really close) to 500 (at 200mm) - freq = (1500- (distance * 5)) - #security limits + # Frequency ranging from 1500 (really close) to 500 (at 200mm) + freq = 1500 - (distance * 5) + # Security limits freq = max(500, min(1500, freq)) buzzer_tim.freq(freq) @@ -31,9 +37,9 @@ else: buzzer_ch.pulse_width_percent(0) - sleep_ms(50) - except KeyboardInterrupt: - buzzer_ch.pulse_width_percent(0) #Properly cut the sound when the user presses CTLR+C + pass +finally: + buzzer_ch.pulse_width_percent(0)