-
Notifications
You must be signed in to change notification settings - Fork 305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HC-SR04 Ultrasonic Sensor #114
Comments
My example: import RPi.GPIO as GPIO
from time import time, sleep
#the factor used to times the echo time by to get the distance
DISTANCEFACTOR = 17000
class HCSR04Sensor:
def __init__(self, triggerPin, echoPin):
#persist values
self.triggerPin = triggerPin
self.echoPin = echoPin
#setup pins
GPIO.setup(triggerPin, GPIO.OUT)
GPIO.setup(echoPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#set trigger to off
GPIO.output(triggerPin, False)
def getEcho(self):
#if the echo pin is high wait for it to go low
if GPIO.input(self.echoPin) == 1:
while GPIO.input(self.echoPin) == 1:
pass
#turn trigger on
GPIO.output(self.triggerPin, True)
#wait 10 micro settings
sleep(0.00001)
#turn trigger off
GPIO.output(self.triggerPin, False)
#wait for the echo to go high
while GPIO.input(self.echoPin) == 0:
pass
#start the timer
start = time()
#wait for it to go low
while GPIO.input (self.echoPin) == 1:
pass
#stop the timer
end = time()
#work out the time difference
diff = end - time
return diff
def getDistance(self):
return self.getEcho() * DISTANCEFACTOR
#test
if __name__ == "__main__":
#set the mode
GPIO.setmode(GPIO.BCM)
#pins
TRIGGERPIN = 23
ECHOPIN = 24
try:
#create the sensor
sensor = HCSR04Sensor(TRIGGERPIN, ECHOPIN)
while True:
#read the distance
print sensor.getDistance()
sleep(0.01)
finally:
#cleanup the GPIO
GPIO.cleanup() |
I have an HCSR04 sensor module I put together and use for a sump pit monitoring system. The module allows extra functionality
https://github.com/alaudet/hcsr04sensor/blob/master/hcsr04sensor/sensor.py Sample usage scripts |
@alaudet - Very interesting stuff - I'll certainly dig into that when I get around to the ultra-sonic sensor, thanks! |
Got an ultra-sonic in the CamJam 3 kit now so I can work on this for 1.2. |
Yup was going to say let's make sure we get this in. @MarcScott was asking me about it today - told him to use the base classes for now. |
Implements support for the HR-SC04 ultrasonic sensor as an input device class named DistanceSensor
Okay, there's a rough initial commit for anyone that wants to play. I'll make this into a proper PR when I get some time but it's not ready for merge yet. |
Implements support for the HR-SC04 ultrasonic sensor as an input device class named DistanceSensor
Cool thanks I'll check it out. @MarcScott get in here! |
If people really want to play with right now, just clone my repo (https://github.com/waveform80/gpio-zero) and checkout the ultrasonics branch. But as mentioned, this isn't ready for a PR yet (still need to add some configuration for speed of sound conversion and experiment a bit with the smoothing; at the moment I think it's set to take the median of the last 30 samples that the background thread has read - in experiments it reads about 160samples/s on an idle Pi2 so that's effective latency of 3/16ths of a second for any given reading). Still pretty jittery in certain circumstances though, but I suspect that's just the hardware for you... |
Implements support for the HR-SC04 ultrasonic sensor as an input device class named DistanceSensor
Implements support for the HR-SC04 ultrasonic sensor as an input device class named DistanceSensor
Implements support for the HR-SC04 ultrasonic sensor as an input device class named DistanceSensor
Implements support for the HC-SR04 ultrasonic sensor as an input device class named DistanceSensor
Implements support for the HC-SR04 ultrasonic sensor as an input device class named DistanceSensor
Okay, that's the PR done - give it a whirl and let me know if anything needs changing. I'm not sure about some of the naming (far/near? Hmmm... couldn't think of anything better off the top of my head, suggestions welcome). The class has a few oddities; while it's got the usual 0-1 scaled For now I've left out any overt mention of the ability to re-configure the class for different speeds of sound (compensating for different mediums / temperatures / whatever) because I figure that's probably "too much information". However, if you want to play with it, just modify the I've also modified the smoothing thread to use median averaging by default. The ultrasonic sensor seems to be considerably more jittery than most and median was necessary to eliminate some of its wilder swings. As a result, LDR might be a bit "smoother" too but I figure that's no bad thing. In case we desperately need mean in future, I've made the averaging algorithm used by GPIOQueue configurable and back-ported the median and mean implementations from Python 3.4 for 3.2 (wheezy) or 2.7 users. Anyway, let me know what you think! |
Oh, and there's a fun new recipe thrown in too for a nervous robot (haven't tested it yet though :) |
Thank you. :D :D Will give this a whirl tomorrow on my robot project. It uses two of these sensors as an avoider. I'm new to coding but have already created a working code in the old GPIO format as it where. |
I am anxious to have a look. Just for the record though, it's a HC-SR04 and not HR-SC04 |
Fixed :) I actually realized I'd got that wrong in nearly every place the other day before committing, but thankfully it's a unique enough string so a quick |
oh ya, I wanted to comment on the speed of sound stuff. Given that this sensor doesn't work very well beyond a few metres its probably not even necessary to add anything in regards temperature. I added it in hcsr04sensor module but its kind of useless and you can never get highly accurate readings anyway. In an educational setting I have to think most people are sitting at about room temperature. Unless people plan to use it at -25 or something, it just won't make much difference in readings. |
Yeah, I'll leave the speed of sound as a "hidden" bit of configuration for anyone that wants to play with it, but I don't think there's much point complicating the docs with it. |
Implements support for the HC-SR04 ultrasonic sensor as an input device class named DistanceSensor
Implements support for the HC-SR04 ultrasonic sensor as an input device class named DistanceSensor
Anybody got any better ideas for event names than near/far? If not, I'm tempted to just merge this so it's easier for people to have a play with it - we're not doing a release yet so no big deal if we change the names later (and now we've got a stable default docs version we won't confuse any newbies looking at them). |
Yeah, I can see that with 'near' and 'far' there might be an expectation that there's a third state between the two (rather than it being a 'boolean' state like a button-press), but I can't think of any better suggestions... |
A thought occurs (as so rarely happens!): how about doing something similar to MotionSensor: Still, I think you've hit the nail on the head: it does feel strange that something is "near", then a millimetre further back and suddenly it's "far" (initially I was thinking "when_close" but especially for non-native English speakers the conflation of "close" for distance with "close" to closing an object would just be too confusing). Even googling "synonym near" and "synonym far" I don't get that many inspirations for alternatives (everything seems to be a variation on near, close, nearby, far, afar, etc.) |
I haven't played with it yet, but looked at the recipe. Near/Far seems ok. The only other thing I can think of would be in_range/out_of_range |
Hmm, I'm liking Okay, if there's no objections (or better ideas!) by this evening I'll do a little renaming, then merge this. |
Sounds good! |
Another one from camjam kit 3, see the worksheet
Example code:
The text was updated successfully, but these errors were encountered: