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 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!
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.
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.
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ā¦
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 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).
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.