Ultrasound Sensing with Python

Viewing a single blog post

Sensing with Python

 

Now that we’ve hooked our Ultrasonic Sensor up to our Pi, we need to program a Python script to detect distance!

The Ultrasonic sensor output (ECHO) will always output low (0V) unless it’s been triggered in which case it will output 5V (3.3V with our voltage divider!). We therefore need to set one GPIO pin as an output, to trigger the sensor, and one as an input to detect the ECHO voltage change.

First, import the Python GPIO library, import our time library (so we make our Pi wait between steps) and set our GPIO pin numbering.

import RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BCM)

Next, we need to name our input and output pins, so that we can refer to it later in our Python code. We’ll name our output pin (which triggers the sensor) GPIO 23 [Pin 16] as TRIG, and our input pin (which reads the return signal from the sensor) GPIO 24 [Pin 18] as ECHO.

TRIG = 23

ECHO = 24

We’ll then print a message to let the user know that distance measurement is in progress. . . .

print "Distance Measurement In Progress"

Next, set your two GPIO ports as either inputs or outputs as defined previously.

GPIO.setup(TRIG,GPIO.OUT)

GPIO.setup(ECHO,GPIO.IN)

Then, ensure that the Trigger pin is set low, and give the sensor a second to settle.

GPIO.output(TRIG, False)

print "Waiting For Sensor To Settle"

time.sleep(2)

The HC-SR04 sensor requires a short 10uS pulse to trigger the module, which will cause the sensor to start the ranging program (8 ultrasound bursts at 40 kHz) in order to obtain an echo response. So, to create our trigger pulse, we set out trigger pin high for 10uS then set it low again.

GPIO.output(TRIG, True)

time.sleep(0.00001)

GPIO.output(TRIG, False)

Now that we’ve sent our pulse signal we need to listen to our input pin, which is connected to ECHO. The sensor sets ECHO to high for the amount of time it takes for the pulse to go and come back, so our code therefore needs to measure the amount of time that the ECHO pin stays high. We use the “while” string to ensure that each signal timestamp is recorded in the correct order.

The time.time() function will record the latest timestamp for a given condition. For example, if a pin goes from low to high, and we’re recording the low condition using the time.time() function, the recorded timestamp will be the latest time at which that pin was low.

Our first step must therefore be to record the last low timestamp for ECHO (pulse_start) e.g. just before the return signal is received and the pin goes high.

while GPIO.input(ECHO)==0:

  pulse_start = time.time()

Once a signal is received, the value changes from low (0) to high (1), and the signal will remain high for the duration of the echo pulse. We therefore also need the last high timestamp for ECHO (pulse_end).

while GPIO.input(ECHO)==1:

  pulse_end = time.time()      

We can now calculate the difference between the two recorded timestamps, and hence the duration of pulse (pulse_duration).

pulse_duration = pulse_end - pulse_start

With the time it takes for the signal to travel to an object and back again, we can calculate the distance using the following formula. 

The speed of sound is variable, depending on what medium it’s travelling through, in addition to the temperature of that medium. However, some clever physicists have calculated the speed of sound at sea level so we’ll take our baseline as the 343m/s. If you’re trying to measure distance through water, this is where you’re falling down – make sure you’re using the right speed of sound!

We also need to divide our time by two because what we’ve calculated above is actually the time it takes for the ultrasonic pulse to travel the distance to the object and back again. We simply want the distance to the object! We can simplify the calculation to be completed in our Python script as follows:

We can plug this calculation into our Python script:

distance = pulse_duration x 17150

Now we need to round our distance to 2 decimal places (for neatness!)

distance = round(distance, 2)

Then, we print the distance. The below command will print the word “Distance:” followed by the distance variable, followed by the unit “cm”

print "Distance:",distance,"cm"

Finally, we clean our GPIO pins to ensure that all inputs/outputs are reset

GPIO.cleanup()

Save your python script, I called ours "range_sensor.py", and run it using the following command. Running a root (sudo), is important with this script:

The sensor will settle for a few seconds, and then record your distance!

 

Download Code

 

 

 

Posted in Internet, Raspberry Pi on Jun 02, 2018


Comments

Please sign in to comment!