Hyungsun Yoon

[HW] HC-SR04 Added

......@@ -5,6 +5,7 @@ import rp2
import neopixel
import dht
import bluetoooth as bto
import ultrasonic
# --------------------------------------------------- #
# INIT
......@@ -33,6 +34,9 @@ def _run():
send_string = str(dht_data[0]) + ',' + str(dht_data[1])
print(send_string)
bto.send_data_bt(send_string)
elif input_data == 'C':
ultasonic_data = ultrasonic.work_sr04()
bto.send_data_bt(str(ultasonic_data))
else:
print('WRONG INPUT')
......
''' Original code from https://www.iottrends.tech/blog/how-to-use-ultrasonic-sensor-with-raspberry-pi-pico/
'''
from machine import Pin
import utime
trigger = Pin(26, Pin.OUT)
echo = Pin(27, Pin.IN)
# --------------------------------------------------- #
# FUNCTIONS
# --------------------------------------------------- #
def work_sr04():
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
distance = (timepassed * 0.0330) / 2
return distance