# Explorer Phat and Hc-SR04 range sensor (again)

**URL:** https://forums.pimoroni.com/t/explorer-phat-and-hc-sr04-range-sensor-again/9246
**Category:** Support
**Created:** [November 29, 2018, 2:45pm UTC](https://forums.pimoroni.com/t/explorer-phat-and-hc-sr04-range-sensor-again/9246 "2018-11-29T14:45:03Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![windy54](https://avatars.discourse-cdn.com/v4/letter/w/e19adc/32.png) [@windy54](https://forums.pimoroni.com/u/windy54)
#### Post date: [November 29, 2018, 2:45pm UTC](https://forums.pimoroni.com/t/explorer-phat-and-hc-sr04-range-sensor-again/9246/1 "2018-11-29T14:45:03Z")

</div>

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

---

<div class="post-metadata">

### Author: ![windy54](https://avatars.discourse-cdn.com/v4/letter/w/e19adc/32.png) [@windy54](https://forums.pimoroni.com/u/windy54)
#### Post date: [December 13, 2018, 11:00pm UTC](https://forums.pimoroni.com/t/explorer-phat-and-hc-sr04-range-sensor-again/9246/2 "2018-12-13T23:00:11Z")

</div>

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

---

<div class="post-metadata">

### Author: ![KramKroc](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/kramkroc/32/5290_2.png) [@KramKroc](https://forums.pimoroni.com/u/KramKroc)
#### Post date: [March 12, 2019, 11:59am UTC](https://forums.pimoroni.com/t/explorer-phat-and-hc-sr04-range-sensor-again/9246/3 "2019-03-12T11:59:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![windy54](https://avatars.discourse-cdn.com/v4/letter/w/e19adc/32.png) [@windy54](https://forums.pimoroni.com/u/windy54)
#### Post date: [March 12, 2019, 4:25pm UTC](https://forums.pimoroni.com/t/explorer-phat-and-hc-sr04-range-sensor-again/9246/4 "2019-03-12T16:25:20Z")

</div>

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

---

<div class="post-metadata">

### Author: ![franlo](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@franlo](https://forums.pimoroni.com/u/franlo)
#### Post date: [August 14, 2021, 6:14pm UTC](https://forums.pimoroni.com/t/explorer-phat-and-hc-sr04-range-sensor-again/9246/5 "2021-08-14T18:14:07Z")

</div>

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.

 ![HC_SR04_code2](https://us1.discourse-cdn.com/flex016/uploads/pimoroni/original/2X/e/e823933bdeabaabcd8e804de6848192c0719cede.jpeg)

---

<div class="post-metadata">

### Author: ![franlo](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@franlo](https://forums.pimoroni.com/u/franlo)
#### Post date: [August 14, 2021, 6:14pm UTC](https://forums.pimoroni.com/t/explorer-phat-and-hc-sr04-range-sensor-again/9246/6 "2021-08-14T18:14:40Z")

</div>

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

 ![ultrasonic_wiring](https://us1.discourse-cdn.com/flex016/uploads/pimoroni/original/2X/2/2636883f6740f01778e3973e6d8951eaef51403d.jpeg)
