I want to get distance continuously using
while loop but a few minutes later my code suddenly stops working and doesn't get distance (there is
None value in distance variable) I have changed first
time.sleep(1) and to other seconds but It is still same with others.
I am using RPi 3 b+, Raspbian, Python3, HC-SR04 and 1K, 2K resistors.
I connect ultrasonic sensor to RPi using this tutorial from MODMYPI: HC-SR04 Ultrasonic Range Sensor on the Raspberry Pi
and here is my code:
import RPi.GPIO as GPIO import time try: GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) TRIG = 23 ECHO = 24 while True: GPIO.setup(TRIG,GPIO.OUT) GPIO.setup(ECHO,GPIO.IN) GPIO.output(TRIG,False) time.sleep(0.01) GPIO.output(TRIG,True) time.sleep(0.00001) GPIO.output(TRIG,False) while GPIO.input(ECHO) == 0: pulse_start = time.time() while GPIO.input(ECHO) == 1: pulse_end = time.time() pulse_duration = pulse_end - pulse_start distance = pulse_duration * 17000 distance = round(distance, 2) print(distance) except: GPIO.cleanup()