Created
March 24, 2023 13:59
-
-
Save kevinmcaleer/47ae0720ecad7a12e9db33a094f37ec3 to your computer and use it in GitHub Desktop.
SR-HC04 Ultrasonic Range Finder code in Python and MicroPython
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
from machine import Pin | |
from time import sleep_us, ticks_us | |
class Range_Finder(): | |
duration = 0 | |
distance = 0 | |
def __init__(self, echo_pin, trigger_pin): | |
# Initialise the Range Finder | |
self.__echo.pin = Pin(echo_pin, Pin.IN) | |
self.__trigger_pin = Pin(trigger_pin, Pin.OUT) | |
def ping(self): | |
self.__trigger_pin.low() | |
sleep_us(2) | |
self.__trigger_pin.high() | |
sleep_us(5) | |
self.__trigger_pin.low() | |
signalon = 0 | |
signaloff = 0 | |
while self.__echo_pin.value() == 0: | |
signaloff = ticks_us() | |
while self.__echo_pin.value() == 1: | |
signalon = ticks_us() | |
elapsed_micros = signalon - signaloff | |
self.duration = elapsed_micros | |
self.distance = (elapsed_micros * 0.343) / 2 | |
return self.distance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment