-
-
Save Bhavithiran97/076afc2d0b098d39fe51b2d73feb6caf to your computer and use it in GitHub Desktop.
read ultrasonic
This file contains 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
import machine | |
import utime | |
# read ultrasonic | |
Trig = machine.Pin(6,machine.Pin.OUT) # Connect Trig pin to pin 6 and set to OUTPUT | |
Echo = machine.Pin(7,machine.Pin.IN) # Connect Echo pin to pin 7 and set to INPUT | |
def read_ultrasonic(): | |
Trig.value(0) | |
utime.sleep_us(2) | |
Trig.value(1) | |
utime.sleep_us(10) | |
Trig.value(0) | |
while Echo.value() == 0: | |
pulse_start = utime.ticks_us() | |
while Echo.value() == 1: | |
pulse_end = utime.ticks_us() | |
pulse_duration = pulse_end - pulse_start | |
distance = pulse_duration//58 | |
return distance | |
while True: | |
print(read_ultrasonic()) # print ultrasonic distance | |
utime.sleep(1) # sleep 1 second |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment