-
- Posts: 48
- Joined: Fri Apr 08, 2016 1:10 pm
Using the HC-SR04 ultrasonic receiver
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
What is wrong with the many solutions offered when you google "raspberry pi sr04"?
-
- Posts: 48
- Joined: Fri Apr 08, 2016 1:10 pm
Re: Using the HC-SR04 ultrasonic receiver
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
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).
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
Here is some commented code that explains how it works.
it was written for python but could be simply changed to run with python3
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

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

We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported
The use of crystal balls & mind reading are not supported
-
- Posts: 3
- Joined: Mon Nov 18, 2019 5:40 pm
Re: Using the HC-SR04 ultrasonic receiver
Which resistors should I use for the HC-SR04 ultrasonic sensor?
Re: Using the HC-SR04 ultrasonic receiver
As shown in my diagram a 1K ohm and 2K ohm , even the 1/8 W power rated ones will be fine.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported
The use of crystal balls & mind reading are not supported
Re: Using the HC-SR04 ultrasonic receiver
The two while loops in that code need timeouts - they could cause the code to hang. Looks like code ported from Arduino.