Zellazoom
Posts: 2
Joined: Tue Jun 25, 2019 2:33 am

HC - SR04 Python Programming Help

Tue Jun 25, 2019 2:43 am

Hello, I hope I am in the right section for this. I have been starting to working with hardware on my raspberry pi, and I came across something I do not understand and hoping someone could help.

Code: Select all

import RPi.GPIO as GPIO
import time

#________________SETUP_____________________________
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

TRIG = 4
ECHO = 18

GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)

#____________THIS_IS_INITIAL_BURST_________

# I would think this is where to start the initial timer.

GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)

#____________RECIEVES_INPUT_OF_BURST_______
while GPIO.input(ECHO) == False:     #THIS IS WHERE I GET CONFUSED 
    start = time.time()

while GPIO.input(ECHO) == True:
    end = time.time()
    #________CALCULATES_TIME___________________
sig_time = end - start

#________CALCULATES DISTANCE_______________
distance = sig_time / 0.000058

print('Distance: {} cm'.format(distance))
print(start)
print(end)
GPIO.cleanup()

So I am confused on why everyone measures just the echo duration and not the whole time the signal is sent out. I was wondering if anyone could elaborate this or show me what I am not understanding. Thanks for your help, and tell me if I am in the wrong place for this question.

pcmanbob
Posts: 6934
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: HC - SR04 Python Programming Help

Tue Jun 25, 2019 9:00 am

Hi.

Its always a good idea to look up the data sheet if you don't understand how a device works.

So from the data sheet.

Image

so from that if we relate it to your code, after sending the trigger

Code: Select all

GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
we wait for the echo pin to go high and while waiting we reset the start time constantly, until it goes high then the code will exit the while loop saving the start time .

Code: Select all

while GPIO.input(ECHO) == False:     #THIS IS WHERE I GET CONFUSED 
    start = time.time()
    
then we wait for the echo pin to go low again resetting the end time constantly, again until it goes low again when the while loop will end this time saving the end time.

Code: Select all

while GPIO.input(ECHO) == True:
    end = time.time()
    
by doing this we get the time the sensor was outputting the echo back pulse which allows you to calculate the time and hence the distance the pulse travelled.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Zellazoom
Posts: 2
Joined: Tue Jun 25, 2019 2:33 am

Re: HC - SR04 Python Programming Help

Thu Jun 27, 2019 2:52 am

Thank you for the data sheet. I totally did not think of it. I guess it makes a little more sense now.

fotoartist78
Posts: 48
Joined: Fri Apr 08, 2016 1:10 pm

Re: HC - SR04 Python Programming Help

Wed Jul 03, 2019 3:20 pm

Hi @pcmanbob'
Have you actually used the code and timings which you quoted with success. I ask as I am trying experiment with this sensor on an Arduino Uno project but am having no success.

pcmanbob
Posts: 6934
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: HC - SR04 Python Programming Help

Wed Jul 03, 2019 8:53 pm

fotoartist78 wrote:
Wed Jul 03, 2019 3:20 pm
Hi @pcmanbob'
Have you actually used the code and timings which you quoted with success. I ask as I am trying experiment with this sensor on an Arduino Uno project but am having no success.
I have not used the code that was posted by the OP I was just explaining how it works.

I have used this code on a pi using python.

Code: Select all

#!/usr/bin/python


# Import required Python libraries
import time
import RPi.GPIO as GPIO

# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# Define GPIO to use on Pi
GPIO_TRIGGER = 23
GPIO_ECHO    = 24



# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo

# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)

print "Ultrasonic Measurement"

# Allow module to settle
time.sleep(0.5)

# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()

while GPIO.input(GPIO_ECHO)==0:
  start = time.time()

while GPIO.input(GPIO_ECHO)==1:
  stop = time.time()

# Calculate pulse length
elapsed = stop-start

# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distancet = elapsed * 34300

# That was the distance there and back so halve the value
distance = distancet / 2

print "Distance :", distance


# Reset GPIO settings
GPIO.cleanup()
I don't use Arduino's but I doubt the code would run on one as it has been written , I suggest you ask for help on an arduino forum as we only support the raspberry pi here.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Automation, sensing and robotics”