# Unreliable HC-SR04 with Explorer HAT Pro

**URL:** <https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641>\
**Category:** Support\
**Created:** [April 18, 2015, 9:37am UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641 "2015-04-18T09:37:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ColinD](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/colind/32/229_2.png) [@ColinD](https://forums.pimoroni.com/u/ColinD)\
**Post date:** [April 18, 2015, 9:37am UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/1 "2015-04-18T09:37:12Z")

</div>

I’ve started to use the Explorer Pro HAT with an HC-SR04 ultrasonic sensor to build a Zumo-based robot and am getting strange results with the distance tests. If the distance is less than ~40 cm then the combination of HAT + sensor seems to get stuck, never returning a result (the “== 0” while loop below never exits). If greater than ~40cm then the result returned for, say, 50cm varies from 14cm to 80cm. Yet, the resolution is supposed to be down to the nearest mm. The sensor is also rated to ~400 cm yet I get results ranging from ~4cm to ~180cm for a wall just shy of 2m away.  
The object is at right-angles to the sensor, hence unlikely to be an artefact of bouncing.

Have you had success with the sensor with the board?

I am wondering if this is a bug in the Explorer HAT code, or possibly I’m not understanding something.

Here’s my code that does not work:

```
import explorerhat as eh
import time

# ensure the output is off
eh.output.one.off()
time.sleep(0.5)

signaloff = time.time()
signalon = signaloff

def objdist():
  eh.output.one.on()
  time.sleep(0.00001)
  eh.output.one.off()

  while eh.input.one.read() == 0:
    pass
  start = time.time()

  while eh.input.one.read() == 1:
    pass
  stop = time.time()

  return (stop - start) * 17000

print "start"
print objdist()
print "stop"

```

If I try something simpler it seems to work sort-of ok, although weirdly returning a 1 when it ought to be a 0 and vice versa. I’ve tried modifying the two while statements above swapping the 1 and 0 around but to no avail.

The following works confirming that when there’s an object in front of the sensor we see a result, but obviously won’t give me a distance back, just a 1 or 0:

```
import explorerhat as eh
import time

eh.output.one.off()
time.sleep(0.5)

while True:
  eh.output.one.on()
  time.sleep(0.00001)
  eh.output.one.off()
  print eh.input.one.read()
  print eh.input.one.read()
  print " ****"
  time.sleep(1)

```

My sensor is wired up as per the [photo](https://www.dropbox.com/s/d2s19b6stloopeo/2015-04-18%2010.30.13.jpg?dl=0) (sorry for the URL, new users can’t upload to your forum). Although a little tricky to see, hence to help the sensor is wired as follows:

VCC to 5V (blue wire)  
Trig to Output 1 (yellow wire)  
Echo to Input 1 (green wire)  
GND to GND (brown wire)

I’m using a Raspberry Pi A+.

Any thoughts would be most appreciated.

---

<div class="post-metadata">

**Author:** ![gadgetoid](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/gadgetoid/32/13_2.png) [@gadgetoid](https://forums.pimoroni.com/u/gadgetoid)\
**Post date:** [April 18, 2015, 2:42pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/2 "2015-04-18T14:42:19Z")

</div>

The problem is quite likely to be a timing problem- I suspect the Explorer HAT code is fine, but there’s just too much of it getting in the way of what is a very, very time sensitive measurement.

You will probably have to use RPi.GPIO directly if you want to get a more accurate reading- I also suspect some of the polling code in the library ( used to read the analog inputs and button states ) will be slowing down your readings and making them very unreliable, so using the two together might prove tricky.

I’ll have a look into expanding the Explorer HAT python library with options to turn off/slow down the analog/touch polling which might help.

---

<div class="post-metadata">

**Author:** ![ColinD](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/colind/32/229_2.png) [@ColinD](https://forums.pimoroni.com/u/ColinD)\
**Post date:** [April 18, 2015, 3:10pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/3 "2015-04-18T15:10:07Z")

</div>

Cheers, sounds good.

Are there any gotchas one needs to be aware of when using RPi.GPIO directly with the Explorer HAT Pro?

If you’re able to improve the response time with the HAT for ultrasonic readings that’d be great. Feel free to send me a message if you’d like me to test the code (seeing as I’ve a willing robot handy).

---

<div class="post-metadata">

**Author:** ![ColinD](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/colind/32/229_2.png) [@ColinD](https://forums.pimoroni.com/u/ColinD)\
**Post date:** [April 18, 2015, 4:44pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/4 "2015-04-18T16:44:47Z")

</div>

Just thought: the Explorer Pro has those tasty looking analog ports available. Would they be capable of playing nicely with one of these [https://www.pololu.com/product/136](https://www.pololu.com/product/136), the Sharp GP2Y0A21YK0F Analog Distance Sensor instead? It uses IR instead of ultrasound, hence only works 10-80cm but I don’t need greater than that, and can live with 10cm at the lower end. Or, [https://www.pololu.com/product/2464](https://www.pololu.com/product/2464) which does 4-30cm which would be equally good for the opposite reason.

---

<div class="post-metadata">

**Author:** ![gadgetoid](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/gadgetoid/32/13_2.png) [@gadgetoid](https://forums.pimoroni.com/u/gadgetoid)\
**Post date:** [April 18, 2015, 4:58pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/5 "2015-04-18T16:58:56Z")

</div>

I haven’t tried an analog one, but there’s no reason why it shouldn’t work. The supply voltage range is fine. It’s a shame it only outputs something like 0v to 3.3v, since that’s throwing resolution away but that’s not really a problem.

If it’s not a naff sensor it would probably be a better choice for Explorer HAT Pro users, since it’s not so ridiculously time sensitive and you could use 4 at a time.

---

<div class="post-metadata">

**Author:** ![ColinD](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/colind/32/229_2.png) [@ColinD](https://forums.pimoroni.com/u/ColinD)\
**Post date:** [May 28, 2015, 10:17am UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/6 "2015-05-28T10:17:25Z")

</div>

Success :)

The Sharp GP2Y0A41SK0F works really really well with the Explorer HAT. After a bit of research at [http://jeremyblythe.blogspot.co.uk/2012/09/raspberry-pi-distance-measuring-sensor.html](http://jeremyblythe.blogspot.co.uk/2012/09/raspberry-pi-distance-measuring-sensor.html) and [http://www.yoctopuce.com/EN/article/an-usb-optical-telemeter](http://www.yoctopuce.com/EN/article/an-usb-optical-telemeter) I’ve got it working within 10% accuracy, which for me is good enough (the difference between a robot being 5cm or 5.5cm away from a wall works for me). Playing around with the maths a bit may get this to be even more accurate. Here’s the code:

```
import explorerhat
import time

# with help from: http://www.yoctopuce.com/EN/article/an-usb-optical-telemeter
# and an idea from http://jeremyblythe.blogspot.co.uk/2012/09/raspberry-pi-distance-measuring-sensor.html

while True:
  v_readings = []
  # read the voltage 10 times so that we can get a decent average
  for i in range (0,10):
    v_readings.append(explorerhat.analog.four.read())

  av_voltage = sum(v_readings)/10.0

  if av_voltage <= 0:
    av_voltage = 0.001

  distanceInCm = 12 / av_voltage

  print("Distance: %3.2f cm (%3.2f V) " % (distanceInCm,av_voltage))
  time.sleep(0.25)

```

Sampling more frequently that every quarter second is doable. There’s no timing issue unlike the HC-SR04. I got my GP2Y0A41SK0F from eBay for a few squid.

As an added bonus the black colour of the GP2Y0A41SK0F now makes Zumo George look fierce (in a Goliath sort of way), where-as before the HC-SR04 made him look like WALL-E.

---

<div class="post-metadata">

**Author:** ![neilcford](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/neilcford/32/578_2.png) [@neilcford](https://forums.pimoroni.com/u/neilcford)\
**Post date:** [December 8, 2015, 12:09pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/7 "2015-12-08T12:09:40Z")

</div>

Sorry for the thread necromancy but I’m wondering if anyone got any further with this as I to am building an Explorer Hat Pro powered Zumo bot and want to use an SR04 to stop it bumping in to things. Any pointers would be appreciated.

TIA

- Neil.

---

<div class="post-metadata">

**Author:** ![ColinD](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/colind/32/229_2.png) [@ColinD](https://forums.pimoroni.com/u/ColinD)\
**Post date:** [December 8, 2015, 12:31pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/8 "2015-12-08T12:31:18Z")

</div>

Hi Neil

I stopped working with the HC-SR04 on Zumo + Explorer HAT Pro and moved to the Sharp GP2Y0A41SK0F which is very reliable. Plus, as an analogue device it gives me:

a) something to plug into the analogue pins (freeing up the digital)  
b) experience of reading and interpreting an analogue voltage signal.

I like the Sharp over the HC-SR04 as no time-accurate readings are needed, which is always going to be a little iffy on a non-realtime device like the Pi. I go into using the Sharp in a fair bit of detail at [http://rasptut.co.uk/files/zumo-george-part2.php](http://rasptut.co.uk/files/zumo-george-part2.php) . Whichever sensor you go with hopefully my (only two so far… more to come) posts on using the Zumo with the Pi will help. NB: the A+ is about 1.5mm too big on each side and I’m thinking of swapping to the Zero. Who would have thought the A+ is “too big” ;)

I do recall that Pimoroni were going to look at improving the timings to try to get things working. Although they did suggest to bypass the Explorer code and use RPi.GPIO direct may help. Sorry I can’t be more help on the HC-SR04.

---

<div class="post-metadata">

**Author:** ![neilcford](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/neilcford/32/578_2.png) [@neilcford](https://forums.pimoroni.com/u/neilcford)\
**Post date:** [December 8, 2015, 1:56pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/9 "2015-12-08T13:56:53Z")

</div>

Thanks for the prompt and informative reply, it’s really appreciated.

I’m using the SR04 simply because I have a box of them from previous workshops. But from the looks of it, I should accept that an analogue sensor is the way to go long term. Something to shop for in the new year perhaps.

I am currently using the Pro with a Pi2, which as you say, is a little large. Plan is to look a using the Zero with an Explorer pHat, though the lack of 3.3 might make the analogue sensor a no go.

So for now I think my demo will just be robot motors run unless obstacle is detected, regardless of distance, with buttons to start and stop the sequence.

- Neil.

---

<div class="post-metadata">

**Author:** ![ColinD](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/colind/32/229_2.png) [@ColinD](https://forums.pimoroni.com/u/ColinD)\
**Post date:** [December 8, 2015, 2:12pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/10 "2015-12-08T14:12:20Z")

</div>

I just checked Zumo George and he’s got his Sharp IR sensor wired up to 5V with no problem. Works a treat. I’ve a Explorer pHAT on order so will wire it all up and let you know how it goes.

---

<div class="post-metadata">

**Author:** ![neilcford](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/neilcford/32/578_2.png) [@neilcford](https://forums.pimoroni.com/u/neilcford)\
**Post date:** [December 9, 2015, 1:01pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/11 "2015-12-09T13:01:35Z")

</div>

Oooh, that’s good news! Zero powered robotics gets one step closer. Thanks for doing this, it’s really appreciated.

- Neil.

---

<div class="post-metadata">

**Author:** ![RogueM](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/roguem/32/311_2.png) [@RogueM](https://forums.pimoroni.com/u/RogueM)\
**Post date:** [December 10, 2015, 3:35pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/12 "2015-12-10T15:35:55Z")

</div>

[quote=“neilcford, post:9, topic:641, full:true”]  
I am currently using the Pro with a Pi2, which as you say, is a little large. Plan is to look a using the Zero with an Explorer pHat, though the lack of 3.3 might make the analogue sensor a no go.[/quote]

Note that if you need 3.3V you can use a ‘stackable’ header on the pHAT, exposing the GPIO for additional purposes as well.

Incidentally, here’s the pinout so you can easily see what you’ve got to play with without risk to overlap the pExplorer’s claimed pins:

> **[Explorer pHAT at Raspberry Pi GPIO Pinout](https://pinout.xyz/pinout/explorer_phat)**
>
> An all-in-one input, output and motor add-on board

---

<div class="post-metadata">

**Author:** ![nevsie](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/nevsie/32/165_2.png) [@nevsie](https://forums.pimoroni.com/u/nevsie)\
**Post date:** [September 18, 2016, 11:09pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/13 "2016-09-18T23:09:20Z")

</div>

I have used the HC-SR04 on the explorerhat, and addressing them via GPIO it worked perfectly. So just using it as 5v instead of 3.3v and resistors!

So feel free to try/use this cheap sensor!

---

<div class="post-metadata">

**Author:** ![ColinD](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/colind/32/229_2.png) [@ColinD](https://forums.pimoroni.com/u/ColinD)\
**Post date:** [September 19, 2016, 4:45pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/14 "2016-09-19T16:45:09Z")

</div>

Thanks for letting us know. I kept with the IR sensor in the end as it provided just what I needed on Zumo George, eg: [http://rasptut.co.uk/files/george-visits-digimakers-whiskers.php](http://rasptut.co.uk/files/george-visits-digimakers-whiskers.php) . Good to know that both IR and HC-SR04 are definite possibilities for the Explorer Pro HAT.

---

<div class="post-metadata">

**Author:** ![nevsie](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/nevsie/32/165_2.png) [@nevsie](https://forums.pimoroni.com/u/nevsie)\
**Post date:** [September 20, 2016, 1:42pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/15 "2016-09-20T13:42:47Z")

</div>

It does however need a good power supply. would not run off a cheap phone recharging battery for my bot…

---

<div class="post-metadata">

**Author:** ![ColinD](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/colind/32/229_2.png) [@ColinD](https://forums.pimoroni.com/u/ColinD)\
**Post date:** [September 20, 2016, 1:56pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/16 "2016-09-20T13:56:15Z")

</div>

I’ve had the Sharp IR sensor working both with USB power (1A supply) and via 9V battery using a UBEC (from Dawn Robotics. You can now get them from Mod My Pi).

---

<div class="post-metadata">

**Author:** ![nevsie](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/nevsie/32/165_2.png) [@nevsie](https://forums.pimoroni.com/u/nevsie)\
**Post date:** [September 20, 2016, 11:28pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/17 "2016-09-20T23:28:34Z")

</div>

Yeah the USB runs my pi, hat, and motors fine. But seemed to need more for ultrasonic sensor, which is odd. I’ll give it a fresh charge and see.

Likewise I have a ubek but trying to decide what to plug into it… might go for some RC batteries as these give loads of size choices to fit inside my Zumo! Or maybe get lipo zero and lipo battery. Or… Or… Bigger chassis than Zumo!

---

<div class="post-metadata">

**Author:** ![ColinD](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/colind/32/229_2.png) [@ColinD](https://forums.pimoroni.com/u/ColinD)\
**Post date:** [September 21, 2016, 7:43am UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/18 "2016-09-21T07:43:06Z")

</div>

If you shave out the AA-shaped plastic inside the battery housing you can just… just mind you (it’ll bulge a little) fit a 9V in there. A flatter lipo would be far easier.

---

<div class="post-metadata">

**Author:** ![kenjara](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/kenjara/32/1953_2.png) [@kenjara](https://forums.pimoroni.com/u/kenjara)\
**Post date:** [January 26, 2017, 9:20pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/19 "2017-01-26T21:20:48Z")

</div>

Hi I am also struggling to get the HC-SR04 working on the explorer phat. Did you leave the sensor plugged into the input / output pins and then reference those pins as GPIO?

---

<div class="post-metadata">

**Author:** ![nevsie](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/nevsie/32/165_2.png) [@nevsie](https://forums.pimoroni.com/u/nevsie)\
**Post date:** [January 26, 2017, 9:52pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/20 "2017-01-26T21:52:12Z")

</div>

This is my code: [http://pastebin.com/HRubx53n](http://pastebin.com/HRubx53n)  
Note - i am no pro, but it did do the job in test…

Just reconnected and it works - using:  
Output 1 to Trig  
Input 1 to Echo

and for reference this might be handy:

> **[Explorer HAT Pro at Raspberry Pi GPIO Pinout](https://pinout.xyz/pinout/explorer_hat_pro#)**
>
> An all-in-one light, input, motor, touch and output add-on board

Image added if it helps:

 ![](https://us1.discourse-cdn.com/flex016/uploads/pimoroni/original/2X/c/cec39f3c15df314d926fd29faeae5ea959885486.jpg)

[Next page](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641.md?page=2)
