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