[Documentation] [TitleIndex] [WordIndex

Speed measurement with ultrasonic sensor HC-SR04 (Python)

This tutorial guides you through calculating a relative velocity by using the distance measurement of the HC-SR04 sensor over the time.

In order to be able to measure a speed at all, we must first be able to measure a distance to an object. Ultimately, the ultrasonic sensor can do no more. Therefore, first read Distance measurement with ultrasonic sensor HC-SR04 (Python).

Calculating the speed

Let's take another closer look at the triangle from the previous chapter:

https://lastminuteengineers.b-cdn.net/wp-content/uploads/arduino/Distance-Speed-Time-Formula-Triangle.png

So we need

Speed = Distance / Time

However, we do not want to go to the speed of sound now and check whether there is a deviation here.

I will show you how to measure the speed of movement of an object using this sensor. For that purpose we need to take two distance measurements in a short time apart and we have:

distance2 - distance1 = distance speed at a given time

If we make the measurements in a time period of 1 second, then we get the speed of movement of the object in cm/s.

When the object is moving in the opposite direction, the speed represented on the display has a negative sign.

Programming the speed measurement

We change our code from the previous example as follows:

# import libraries
import RPi.GPIO as GPIO
import time
 
# GPIO Modus (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
 
# assign GPIO Pins
GPIO_TRIGGER = 18
GPIO_ECHO = 24
 
# Set direction of GPIO pins (IN --> Input / OUT --> Output)
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.01 ms to LOW
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)
 
    startTime = time.time()
    arrivalTime = time.time()
 
    # store startTime
    while GPIO.input(GPIO_ECHO) == 0:
        startTime = time.time()
 
    # store arrivalTime
    while GPIO.input(GPIO_ECHO) == 1:
        arrivalTime = time.time()
 
    # Time difference between start and arrival
    timeElapsed = arrivalTime - startTime
    # multiply by the speed of sound (34300 cm/s)
    # and divide by 2, there and back again
    distance = (timeElapsed * 34300) / 2
 
    return distance

def speed():
    # calls the distance() function above
    distance1 = distance()

    # giving a time gap of 1 sec
    time.sleep(1)

    # calls the distance() function above a second time
    distance2 = distance()

    # formula change in distance divided by change in time
    # as the time gap is 1 sec we divide it by 1.
    speed = (distance2 - distance1)/1.0

    return speed

if __name__ == '__main__':
    try:
        while True:
            speed = speed()
            print ("Measured speed = %.1f cm" % speed)
            time.sleep(1)
 
        # When canceling with CTRL+C, resetting
    except KeyboardInterrupt:
        print("Measurement stopped by user")
        GPIO.cleanup()

Now that we know what we can measure with the HC-SR04 and how, we can write our driver and wrap it in ROS in the next chapter. Please read for this Writing your HC-SR04 driver (Python).


2024-11-23 12:14