Explorer Phat and Hc-SR04 range sensor (again)

Hi,

I know there is a post from 2015 on this subject, to save people reading through all of the earlier posts, I thought it might be best to start again.

So I am helping someone build a robot, they have got a PI zero, explorer Phat, two 50;1 micro motors etc. That side is ok.

we want to use a HC-Sr04 range sensor. As this is a 5 volt device I thought we could connect to the 5volt input / outputs on the explorer hat.

we are using input 1 for the echo and output 1 for the trigger with the following gpiozero code.

from gpiozero import DistanceSensor
from time import sleep

sensor = DistanceSensor(echo=23, trigger=6)
while True:
print('Distance: ', sensor.distance * 100)
sleep(1)

Echo is on gpio23 and trigger on gpio 6 as per the explorer hat documentation.

When we do get a reading it is in correct.

I notice in the technical documentation for the board there is a comment about the outputs sinking current to ground when turned on and having to connect device to a voltage supply and then to the output pin.

does anyone know how this relates to the distance sensor, e.g. i have got to wire the sensor up differently, I am confuse at the moment.

If we connect the sensor direct to the PI using a potential divider on the echo pin as per the GPIO example, then it works.

thanks

Steve

A 10K pull up on the trigger solves the problem, and then invert the logic.
To generate the pulse for the trigger

output a 1 to pull the trigger low, wait 10micro secs then write a 0 to pull it high. I now get a consistent reading to less with noise of +/- 0.25 cm, more than adequate for my needs.

having sorted it, I have just seen more replies to an earlier post where they suggest the same solution.

On to the next problem/

Steve

Could you give any more details on what your circuit was and the code you used? Aiming to do something similar in the next couple of days and a pointer or two would speed things up greatly :D

Here is a copy of the code with comments!
‘’’
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

TRIG = 6 # op1 pull up resistor fitted between Vcc and the trigger pin
ECHO = 23 # ip1

print “Distance Measurement In Progress”

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

GPIO.output(TRIG, True)
print “Waiting For Sensor To Settle”
time.sleep(1)

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

while GPIO.input(ECHO)==0:
  pulse_start = time.time()


while GPIO.input(ECHO)==1:
  pulse_end = time.time()

pulse_duration = pulse_end - pulse_start

distance = pulse_duration * 17150

distance = round(distance, 2)


   
print "Distance:",distance,"cm"

time.sleep(1)

GPIO.cleanup()
‘’’

one day I will get the hang of mark up text and make it a code block, think it has lost the indents, hopefully it wont be too difficult to sort out!

Steve

Thanks Steve for solving the problem. I have modified your code to make sure the GPIO.cleanup() method is executed. Now when the program is terminated using Control + C on the keyboard, the code handles the error and cleans the GPIO allocations.

I have included a pic of the wiring which I used successfully for the HC-SR04 and the Explorer Hat Pro.