-
Notifications
You must be signed in to change notification settings - Fork 1
feat(vl53l1x): Add proximity alert example with buzzer. #337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| """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 | ||
| 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: | ||
| pass | ||
| finally: | ||
| buzzer_ch.pulse_width_percent(0) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tof.read()can raiseOSError("VL53L1X data ready timeout")(seelib/vl53l1x/vl53l1x/device.py:_ensure_data). If that happens while the buzzer is on, this script will exit without silencing it. Consider using atry: ... finally:(or catchingException) around the loop to always set the buzzer duty back to 0 before exiting.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an excellent point, I had handled the KeyboardInterrupt to prevent the PWM from running indefinitely, but I completely missed the potential OSError from the I2C bus timeouts. Moving the cleanup code to a finally: block is the perfect way to guarantee the buzzer is silenced no matter how the script exits. I'm updating the structure right away.