Page 1 of 1

Using the HC-SR04 ultrasonic receiver

Posted: Wed Jul 03, 2019 3:04 pm
by fotoartist78
Has anyone used the Sensor mentioned in topic header? If so could they make the code they used to pulse & activate it that was used available to me through this forum, I would be very grateful.

Re: Using the HC-SR04 ultrasonic receiver

Posted: Wed Jul 03, 2019 3:12 pm
by joan
What is wrong with the many solutions offered when you google "raspberry pi sr04"?

Re: Using the HC-SR04 ultrasonic receiver

Posted: Wed Jul 03, 2019 3:24 pm
by fotoartist78
Many thanks 'Joan' for your suggestion , I have not tried that so I will do that and hope to find something useful.

Re: Using the HC-SR04 ultrasonic receiver

Posted: Wed Jul 03, 2019 3:32 pm
by joan
In your other post you mentioned the Arduino Uno.

Software used on the Pi won't be optimal on an Arduino (strangely enough most of the poor Pi software for the HC-SR04 was ported from a good Arduino solution without considering the differences between the platforms).

Re: Using the HC-SR04 ultrasonic receiver

Posted: Wed Jul 03, 2019 8:34 pm
by pcmanbob
Here is some commented code that explains how it works.

it was written for python but could be simply changed to run with python3

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()

One other thing you should be aware of is if you are using a 5v HC-SR04 then you need to reduce the echo pin voltage before connecting it to you pi gpio.

like this

Image

Re: Using the HC-SR04 ultrasonic receiver

Posted: Thu Dec 12, 2019 5:32 pm
by JustSayingHi!
Which resistors should I use for the HC-SR04 ultrasonic sensor?

Re: Using the HC-SR04 ultrasonic receiver

Posted: Thu Dec 12, 2019 5:52 pm
by pcmanbob
As shown in my diagram a 1K ohm and 2K ohm , even the 1/8 W power rated ones will be fine.

Re: Using the HC-SR04 ultrasonic receiver

Posted: Thu Dec 12, 2019 5:53 pm
by rpiMike
The two while loops in that code need timeouts - they could cause the code to hang. Looks like code ported from Arduino.