Unreliable HC-SR04 with Explorer HAT Pro

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

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


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:

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

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.

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

Or perhaps it is using the one of the alternative functions:
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

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.

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.

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.

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?

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

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.

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.

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ā€¦

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

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.

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

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

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?

[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.

[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).

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:

HTH

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.