Explorer Phat and Hc-SR04 range sensor (again)



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)

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.




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/