shiroku.net
Ultrasonic Sensor on Raspberry Pi | shiroku.Net
Environment Raspberry Pi 3B+ Raspbian GNU/Linux 9.4 Python 3 HC-SR04 Ultrasonic Module 3.3V-5V Logic Level Converter CYT1076 Code $ vim sonicsensor.py #!/usr/bin/python3 import RPi.GPIO as GPIO import time class UltraSonic: def __init__(self): # GPIO Mode (BOARD / BCM) GPIO.setmode(GPIO.BCM) # set GPIO Pins self.GPIO_TRIGGER = 4 self.GPIO_ECHO = 17 # set GPIO direction (IN / OUT) GPIO.setup(self.GPIO_TRIGGER, GPIO.OUT) GPIO.setup(self.GPIO_ECHO, GPIO.IN) def distance(self): # set Trigger to HIGH GPIO.output(self.GPIO_TRIGGER, True) # set Trigger after 0.01ms to LOW time.sleep(0.00001) GPIO.output(self.GPIO_TRIGGER, False) starttime = time.time() stoptime = time.time() # save StartTime while GPIO.input(self.GPIO_ECHO) == 0: starttime = time.time() # save time of arrival while GPIO.input(self.GPIO_ECHO) == 1: stoptime = time.time() # time difference between start and arrival timeelapsed = stoptime - starttime # multiply with the sonic speed (34300 cm/s) # and divide by 2, because