Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Connect HC-SR01 to Raspberry

Fri Sep 18, 2015 4:28 pm

Good day, I connected to the raspberry pi ultrasonic rangefinder HC-SR01 - the instructionshttp://www.modmypi.com/blog/hc-sr04-ult ... spberry-pi. But the program in python gives random values. In what may be the problem? Image Image

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

Re: Connect HC-SR01 to Raspberry

Fri Sep 18, 2015 4:43 pm

Random values? How random?

You would expect some variation but are you saying the values don't make any sense at all?

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Connect HC-SR01 to Raspberry

Fri Sep 18, 2015 4:55 pm

In the first was more than 4 000 000 cm. And now i have problem: Image

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Connect HC-SR01 to Raspberry

Fri Sep 18, 2015 5:14 pm

I change code to:

Code: Select all

import time
import RPi.GPIO as GPIO
 
GPIO.setmode(GPIO.BCM)

# Define GPIO to use on Pi
GPIO_TRIGGER = 23
GPIO_ECHO = 24
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)
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/2)
dist = elapsed * 1715
print "Distance : %.1f" % dist
# Reset GPIO settings
GPIO.cleanup()
time.sleep(2)
But I have spread to 100 centimeters.

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

Re: Connect HC-SR01 to Raspberry

Fri Sep 18, 2015 6:03 pm

The two most likely errors are GPIO numbering or the wiring.

Make sure you are using the same pins as the software thinks you are using.

Because all the wires are the same colour it's a bit hard to make out which wire is going where.

I'd follow each wire back from the sensor to the Pi and not the terminating pin. Check that the pin matches with what the software expects.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Connect HC-SR01 to Raspberry

Fri Sep 18, 2015 6:21 pm

Yes, problem was in pins, i connected ground, but need to gpio 24. Thank you.

Return to “Beginners”