Showing
2 changed files
with
28 additions
and
0 deletions
| ... | @@ -5,6 +5,7 @@ import rp2 | ... | @@ -5,6 +5,7 @@ import rp2 |
| 5 | import neopixel | 5 | import neopixel |
| 6 | import dht | 6 | import dht |
| 7 | import bluetoooth as bto | 7 | import bluetoooth as bto |
| 8 | +import ultrasonic | ||
| 8 | 9 | ||
| 9 | # --------------------------------------------------- # | 10 | # --------------------------------------------------- # |
| 10 | # INIT | 11 | # INIT |
| ... | @@ -33,6 +34,9 @@ def _run(): | ... | @@ -33,6 +34,9 @@ def _run(): |
| 33 | send_string = str(dht_data[0]) + ',' + str(dht_data[1]) | 34 | send_string = str(dht_data[0]) + ',' + str(dht_data[1]) |
| 34 | print(send_string) | 35 | print(send_string) |
| 35 | bto.send_data_bt(send_string) | 36 | bto.send_data_bt(send_string) |
| 37 | + elif input_data == 'C': | ||
| 38 | + ultasonic_data = ultrasonic.work_sr04() | ||
| 39 | + bto.send_data_bt(str(ultasonic_data)) | ||
| 36 | else: | 40 | else: |
| 37 | print('WRONG INPUT') | 41 | print('WRONG INPUT') |
| 38 | 42 | ... | ... |
hardware/rpi_pico/ultrasonic.py
0 → 100644
| 1 | +''' Original code from https://www.iottrends.tech/blog/how-to-use-ultrasonic-sensor-with-raspberry-pi-pico/ | ||
| 2 | +''' | ||
| 3 | +from machine import Pin | ||
| 4 | +import utime | ||
| 5 | +trigger = Pin(26, Pin.OUT) | ||
| 6 | +echo = Pin(27, Pin.IN) | ||
| 7 | + | ||
| 8 | +# --------------------------------------------------- # | ||
| 9 | +# FUNCTIONS | ||
| 10 | +# --------------------------------------------------- # | ||
| 11 | +def work_sr04(): | ||
| 12 | + trigger.low() | ||
| 13 | + utime.sleep_us(2) | ||
| 14 | + trigger.high() | ||
| 15 | + utime.sleep_us(5) | ||
| 16 | + trigger.low() | ||
| 17 | + while echo.value() == 0: | ||
| 18 | + signaloff = utime.ticks_us() | ||
| 19 | + while echo.value() == 1: | ||
| 20 | + signalon = utime.ticks_us() | ||
| 21 | + timepassed = signalon - signaloff | ||
| 22 | + distance = (timepassed * 0.0330) / 2 | ||
| 23 | + | ||
| 24 | + return distance |
-
Please register or login to post a comment