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

Using the HC-SR04 ultrasonic receiver

Wed Jul 03, 2019 3:04 pm

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.

User avatar
joan
Posts: 15384
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Using the HC-SR04 ultrasonic receiver

Wed Jul 03, 2019 3:12 pm

What is wrong with the many solutions offered when you google "raspberry pi sr04"?

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

Re: Using the HC-SR04 ultrasonic receiver

Wed Jul 03, 2019 3:24 pm

Many thanks 'Joan' for your suggestion , I have not tried that so I will do that and hope to find something useful.

User avatar
joan
Posts: 15384
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Using the HC-SR04 ultrasonic receiver

Wed Jul 03, 2019 3:32 pm

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

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

Re: Using the HC-SR04 ultrasonic receiver

Wed Jul 03, 2019 8:34 pm

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
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

JustSayingHi!
Posts: 3
Joined: Mon Nov 18, 2019 5:40 pm

Re: Using the HC-SR04 ultrasonic receiver

Thu Dec 12, 2019 5:32 pm

Which resistors should I use for the HC-SR04 ultrasonic sensor?

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

Re: Using the HC-SR04 ultrasonic receiver

Thu Dec 12, 2019 5:52 pm

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

User avatar
rpiMike
Posts: 1681
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Using the HC-SR04 ultrasonic receiver

Thu Dec 12, 2019 5:53 pm

The two while loops in that code need timeouts - they could cause the code to hang. Looks like code ported from Arduino.

Return to “Automation, sensing and robotics”