File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ """Proximity alert using VL53L1X distance sensor and buzzer.
2+
3+ Beeps the buzzer when an object is detected within 200 mm. The pitch
4+ increases as the object gets closer.
5+ """
6+
17from time import sleep_ms
28
39from machine import I2C , Pin
612
713DISTANCE_THRESHOLD_MM = 200
814
9- # sensor configuration
15+ # Sensor configuration
1016i2c = I2C (1 )
1117tof = VL53L1X (i2c )
1218
13- #buzzer initialisation
19+ # Buzzer initialisation
1420buzzer_tim = Timer (1 , freq = 1000 )
1521buzzer_ch = buzzer_tim .channel (4 , Timer .PWM , pin = Pin ("SPEAKER" ))
1622buzzer_ch .pulse_width_percent (0 )
2127 print ("Distance: {} mm" .format (distance ))
2228
2329 if distance < DISTANCE_THRESHOLD_MM :
24- #frequency ranging from 1500 (really close) to 500 (at 200mm)
25- freq = ( 1500 - (distance * 5 ) )
26- #security limits
30+ # Frequency ranging from 1500 (really close) to 500 (at 200mm)
31+ freq = 1500 - (distance * 5 )
32+ # Security limits
2733 freq = max (500 , min (1500 , freq ))
2834
2935 buzzer_tim .freq (freq )
3036 buzzer_ch .pulse_width_percent (50 )
3137 else :
3238 buzzer_ch .pulse_width_percent (0 )
3339
34-
3540 sleep_ms (50 )
3641
37-
3842except KeyboardInterrupt :
39- buzzer_ch .pulse_width_percent (0 ) #Properly cut the sound when the user presses CTLR+C
43+ pass
44+ finally :
45+ buzzer_ch .pulse_width_percent (0 )
You can’t perform that action at this time.
0 commit comments