HC-SR04 Ultrasonic Sensor not Looping measurement

Hey there, just using this copy pasted code:


#Libraries
import RPi.GPIO as GPIO
import time
 
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
 
#set GPIO Pins
GPIO_TRIGGER = 18
GPIO_ECHO = 24
 
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
 
def distance():
    # set Trigger to HIGH
    GPIO.output(GPIO_TRIGGER, True)
 
    # set Trigger after 0.01ms to LOW
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)
 
    StartTime = time.time()
    StopTime = time.time()
 
    # save StartTime
    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()
 
    # save time of arrival
    while GPIO.input(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 there and back
    distance = (TimeElapsed * 34300) / 2
 
    return distance
 
if __name__ == '__main__':
    try:
        while True:
            dist = distance()
            print ("Measured Distance = %.1f cm" % dist)
            time.sleep(1)
 
        # Reset by pressing CTRL + C
    except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()

But the measurement output is not looping, just shows the first measurement and then nothing else. Any ideas?

Thanks

-formlessform

2 Likes

Hi Formlessform,

While many people “you get what you code for”, this code does look sound. Are you getting the same measurement each time, or does it vary based on how far the sensor is from something when you start the code? Is it accurate?

Weird bug, keen to get to the bottom of it with you!
-James

1 Like

Hey James,

it displays:

Measured Distance = 20cm

But does not keep printing the distance every second, just displays the first one and stops measuring and/or printing.

Hi FF,

Looks like it’s getting stuck in a while loop then. While crude, I like adding a “waiting for x…” to loops I’m suspicious of, like the one waiting for the return signal from the HC-SR04.

Perhaps a wiring/GPIO-config issue is causing this problem, and the code just waits forever. Are you able to send a photo of how you’ve got everything setup?

-James

FYI, I have just tested this code on a raspberry pi zero, and it executes correctly, this is the second issue im having with gpio on my pi 4, have another post about an i2c error on it.

1 Like

Hi All
I don’t know anything about RPi or Python but would this have anything to do with your problem…

“As this module is a 5V sensor,”

Quoted from the Core description.
Just a thought
Cheers Bob

Dont think so, it runs perfectly on a pi-zero.

I just thought that maybe you get 5V signals from the sensor and the Pi zero might be a bit more tolerant to this.
Cheers Bob

Hi Formless

Could there be some interrupt happening which cause you to miss the high pulse?
Maybe you could use either an interrupt routine yourself or use machine.time_pulse_us() to measure the echo return.

Cheers, Steve