# 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:** 2

<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 27, 2017, 8:41am UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/21 "2017-01-27T08:41:57Z")

</div>

Thank you for the code.

I tried it and it works but it does not solve my issue unfortunately.

Like the code I have tried so far it works fine on mains power but on battery it just halts for ages and occasionally returns a reading. What power source are you using now?

I currently have tried the 4xAA as provided by the zumo chassis and a 5v power bank.

I think I may have to switch to a lipo battery perhaps?

I did make a new topic before discovering this one. [PI Zero Explorer pHat sensor not working on battery power](http://forums.pimoroni.com/t/pi-zero-explorer-phat-sensor-not-working-on-battery-power/3847)

---

<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 27, 2017, 9:25am UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/22 "2017-01-27T09:25:56Z")

</div>

I too have the same issue from the battery.  
I am running:

> **[LiPo SHIM - Pimoroni](https://shop.pimoroni.com/products/lipo-shim)**
>
> The tiny little LiPo/LiIon power supply shim for all versions of the Raspberry Pi!

  

> **[Lithium Ion Battery Pack - Pimoroni](https://shop.pimoroni.com/products/lithium-ion-battery-pack)**
>
> Give your project some grunt with these lithium ion battery packs! Made from balanced 2200mAh cells connected in parallel including protection circuitry.

Mine fires up, completes the waiting for sensor to settle, enters the while loop, and never detects the bounce back.  
The explorer hat is working - as i tested with a few things before entering the loop. But is suspect it must be a power issue with the trigger send or echo detect.

To be honest the reason i had stripped mine back from the zumo was to use with my Devestator Tank setup. So i had not done much more testing on the explorer hat. Instead i was using:

> **[Picon Zero - Intelligent Robotics Controller for Raspberry Pi - Pimoroni](https://shop.pimoroni.com/products/picon-zero-intelligent-robotics-controller-for-raspberry-pi)**
>
> Picon Zero is an Intelligent robotics controller for your Raspberry Pi based mobile robot. A built-in processor handles all the direct communication with your input and output devices, leaving you to worry about the overall control with your...

But that only allows for one sensor - so depends upon your needs.  
If you need more, i would probably consider a different sensor.

---

<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: [January 27, 2017, 10:27am UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/23 "2017-01-27T10:27:19Z")

</div>

if it works from PSU but not battery I’m not sure the following comment is relevant but do note that the Explorer outputs rely on a sinking driver… looking at the pic you posted I don’t think this is taken into account, but I may be wrong.

---

<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 27, 2017, 10:42am UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/24 "2017-01-27T10:42:49Z")

</div>

Hi Rogue,  
Yes you are 100% correct… And to be brutally honest i am not 100% sure i can explain the reasoning and logic in my code. I cannot recall where i found out about, or if it was by accident - but the GPIO BCM 31 i believe is doing some magic!

I believe it is down to how the GPIO BCM 31 also works on BCM 6, and uses different setup:  
[http://elinux.org/RPi\_schematic\_differences](http://elinux.org/RPi_schematic_differences)

Or perhaps it is using the one of the alternative functions:  
[http://elinux.org/RPi\_BCM2835\_GPIOs](http://elinux.org/RPi_BCM2835_GPIOs)

Its well out of my skillset now, and maybe its just a fudge that happened to work for main power… But lets see what thoughts you have.

N

---

<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: [January 27, 2017, 10:57am UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/25 "2017-01-27T10:57:15Z")

</div>

right, but you are using notation that simply do not match your wiring, for a start… with or without the Explorer involved I can’t imagine that code working.

---

<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: [January 27, 2017, 11:22am UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/26 "2017-01-27T11:22:44Z")

</div>

in other words, bcm31 has no relation to bcm6… in fact it isn’t even a drivable pin on a Pi B+/2/3 I believe, although that’s irrelevant to the problem at hand.

… if you had _some result_ at _any point_ with that code, I would hasard that it would be due to a floating input and outside interference/noise. I would certainly declare the pin with a pull up (or down) to avoid that possibility.

still, even if you addressed the correct pin, the Explorer HAT output is only capable to sink the signal to ground, so I’m very doubtful that wiring the sensor direct to output1 would result in the trigger ever reaching the HC-SR04.

---

<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 27, 2017, 12:03pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/27 "2017-01-27T12:03:22Z")

</div>

As i say, i am no expert, and finding information is just confusing me more!  
I agree that the BCM31 info i was looking at was from early models.

So could BCM31 (as it does not exist) be falling back to BOARD31 - i.e. pin 31?  
Or perhaps is it used in some way via I2C? As i say i really do not know.

So you are saying that the trigger should not be being fired? (which i agree)  
And or the echo should not be detected?

All i can say is - that it does work, consistently and accurately when on USB supplied power. Even down to the distance measurements! and it works every time… I do not know what to assume or why - as i say i cannot explain it.

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

---

<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 27, 2017, 1:15pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/28 "2017-01-27T13:15:48Z")

</div>

I forgot to mention I changed 31 to 6 to get that code to work on the pi zero / explorer pHat.

So bcm 6 = output1 and bcm 21 = input1.

It worked exactly the same as using explorerhat.output.one.on() etc…

No difference in performance, fine on mains and broken on battery.

Am I right in thinking that other robotics controller board will just work with the sensor on battery and not have these issues?

---

<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: [January 27, 2017, 1:42pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/29 "2017-01-27T13:42:50Z")

</div>

> [@kenjara](#):
>
> Am I right in thinking that other robotics controller board will just work with the sensor on battery and not have these issues?

From what I understand looking at your original picture in the other thread, it shouldn’t work at all. It’s possible some electrical quirk of using a mains power supply is causing it to work when, by all rights, it shouldn’t.

The HC-SR04 datasheet specifies a 10uS TTL pulse. Basically 10uS between +3v and +5v. The outputs on Explorer HAT/pHAT boards don’t supply any voltage at all, they just sink to ground or 0v in this case.

Unless you’re connecting directly to the GPIO header, using the pin that’s set up for output one (which is BCM6 as it happens) then no TTL pulse can occur.

If you remove the Explorer pHAT from the equation and connect directly to the GPIO pin then it should work, because there’s no ULN2003A Darlington Array to invert your signal.

Even digital electronics are analog, and can behave in unexpected and frankly bizarre ways.

Basically my best guess is that your sensor is not wired correctly, and it works because of an electrical fluke. Changing to another controller board wont so much fix your wiring problem as sidestep it altogether.

You can fix it in a couple of ways:

- Use a 10K pullup resistor between the output pin you’re using, and 5V (or you could solder the pullup between Trig and VCC on the sensor board itself) and invert your signal so that you turn the output “off” for 10uS instead of on. When your output is “off” then it will not sink to 0V, so the pullup resistor will pull it up to 5V until you turn it on again. Note: you might need a lower resistance value.

- Solder/wire directly to the Pi’s BCM6 GPIO pin, or another spare GPIO in, to avoid the inverting buffer altogether.

P.S. I was referencing the HC-SR04 datasheet here: [http://www.micropik.com/PDF/HCSR04.pdf](http://www.micropik.com/PDF/HCSR04.pdf)

---

<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 27, 2017, 2:17pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/30 "2017-01-27T14:17:05Z")

</div>

I think i have to agree.  
It is purely some quirk that is allowing it to work - but funny how it does. And this quirk does not under battery!  
I wish i had the equipment to explore more!!! but i am just a below amateur tinkerer!

As said above - use the pullup resistors.  
Then you can keep using motor outputs.

Or use a different board (like referenced above) which just works differently.

Which approach would probably relate to intended use - i.e. more than once sensor, whether motors need higher voltage, etc.

---

<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 27, 2017, 2:40pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/31 "2017-01-27T14:40:47Z")

</div>

Well trust me to stumble across some kind of quirk. :)

By the way I am a Software Engineer and have not really done any electronics since school so I am a bit of a beginner. But with all these interesting gadgets coming out and getting bored of making .Net applications all day I wanted to get back into electronics as well.

I have made numerous R/C cars and started looking for projects that would combine electronics and coding.

I made the Picade which is amazing and since then I have been hooked. (I have 5 PI’s and a Microbit so far).

Anyway now that I understand that it is a quirk and should not actually work on the explorerPhat I think I am going to go to solder it directly to some free GPIO pins.

Cheers for all the help and information guys I feel I have learned a lot already.

---

<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: [January 27, 2017, 3:03pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/32 "2017-01-27T15:03:06Z")

</div>

if you have an Explorer HAT Pro you can just use a pin from the breakout. 3.3v only, but that’s no different than hooking things up direct to the Pi GPIO…

---

<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: [January 27, 2017, 3:05pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/33 "2017-01-27T15:05:19Z")

</div>

… if that helps, you can find the breakout bcm correspondence at the very end of:

> <https://github.com/pimoroni/explorer-hat/blob/master/documentation/Technical-reference.md>

---

<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: [January 27, 2017, 3:10pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/34 "2017-01-27T15:10:17Z")

</div>

err… goes without saying, but always better to say it nonetheless… bcm2-3 would be the least suitable since the I2C interface is needed for the Explorer. The SPI pins are probably your best bet, as long as you don’t need them for a display or some other add-ons.

---

<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 27, 2017, 3:18pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/35 "2017-01-27T15:18:27Z")

</div>

I am using the explorerPhat so if I am right according to the pinout BCM 17 and 27 are completely unused.

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

Also I need to remember to add a voltage divider to the input pin as it will not handle the 5V.

(From this guide) [https://www.modmypi.com/blog/hc-sr04-ultrasonic-range-sensor-on-the-raspberry-pi](https://www.modmypi.com/blog/hc-sr04-ultrasonic-range-sensor-on-the-raspberry-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: [January 27, 2017, 3:21pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/36 "2017-01-27T15:21:50Z")

</div>

I was going to ask which is the most suitable. In theory any are usable right - just not if they clash with something else in use!

As a guess i fired it up on BCM8, and again worked fine. (over USB).  
However, the echo never receives in battery mode. So the trigger is either not sent, or the echo is not received. The Echo from the HC-SR04 is 5v, so this should be fine straight into the input on explorer hat pro.

Could it be that the battery is not providing enough voltage, and as such the echo is not being sent correctly, or perhaps not detected by the Explorer Hat Pro?

### SCRATCH THAT IDEA

Measured the 5V on the explorer hat pro on both USB and Battery - i get 5.16V and 5.08V…  
So it might be the trigger is not sent at 3.3v ???

### Follow Up

The script is working and running on battery power, and i can get stray detections occasionally.  
I guess its a question of on battery power is the input not sensitive enough, or output strong enough?

---

<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: [January 27, 2017, 3:59pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/37 "2017-01-27T15:59:11Z")

</div>

[quote=“kenjara, post:35, topic:641”]  
I am using the explorerPhat so if I am right according to the pinout BCM 17 and 27 are completely unused.[/quote]  
sure, you can use those… my point was that the easiest - if using an Explorer HAT Pro - is to use some readily accessible pins on the breakout, they are as good as any (notwithstanding I2C and specific project needs), no need to clear access to GPIO necessarily.

---

<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: [January 27, 2017, 4:09pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/38 "2017-01-27T16:09:06Z")

</div>

[quote=“nevsie, post:36, topic:641”]  
Measured the 5V on the explorer hat pro on both USB and Battery - i get 5.16V and 5.08V…So it might be the trigger is not sent at 3.3v ???[/quote]  
it’s clear to me that if you use bcm31 in your code, the trigger is simply never sent out. If you use bcm6 then, unless you follow @gadgetoid’s wiring suggestion then that is true too.

… hence the suggestion to bypass the Explorer Darlington array and use a pin straight from the Pi/breakout (the later being exactly the same, electrically speaking).

regarding the echo, right now there is nothing that rules the Explorer input out as a valid way to read the sensor’s data. So yes, you could use that, if that works that saves you from setting up a voltage divider.

ultimately, this does not answer the question why things go North when using batteries… I don’t know what’s going on (and sadly have no HC-SR04 to test).

---

<div class="post-metadata">

### Author: ![major\_tomm](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/major_tomm/32/9044_2.png) [@major\_tomm](https://forums.pimoroni.com/u/major_tomm)
#### Post date: [January 27, 2017, 4:32pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/39 "2017-01-27T16:32:29Z")

</div>

I’m fairly certain you can trigger these with 3V3. In the past I’ve wired up 5V, GND and TRIG to the appropriate Pi pins and then used a voltage divider (couple of resistors) to reduce the ECHO to 3V3 (from 5V).

Here’s the board I made for that purpose:

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

HTH

---

<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: [January 27, 2017, 4:40pm UTC](https://forums.pimoroni.com/t/unreliable-hc-sr04-with-explorer-hat-pro/641/40 "2017-01-27T16:40:18Z")

</div>

that’s correct, according to the datasheet at least, a 10 uS pulse between +3v and +5v should be fine to trig.

… I’m just wondering if the issue has to do with the buffer on the Explorer inputs, but beats me why it would work with certain power sources and not others.

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

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