Created
August 13, 2017 20:03
-
-
Save rachelwritingcode/c235b29ecfd3cf3c7d00c65741d4d349 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import GPIO Library | |
import time | |
import RPi.GPIO as GPIO | |
GPIO.setmode(GPIO.BCM) | |
# Define GPIO pins being used | |
GPIO_TRIGGER = 18 | |
GPIO_ECHO = 25 | |
#Trig will send out a short burst of sound waves | |
GPIO.setup(GPIO_TRIGGER,GPIO.OUT) | |
#Echo will listen for waves that bounce back | |
GPIO.setup(GPIO_ECHO,GPIO.IN) | |
GPIO.output(GPIO_TRIGGER, False) | |
# Wait for sensor to settle | |
time.sleep(1) | |
# Send out pulse | |
GPIO.output(GPIO_TRIGGER, True) | |
#Wait 10 microseconds so the pulse | |
#can be read by the sensor (the sensor expects 10Us) | |
time.sleep(0.00001) | |
#Stop the pulse | |
GPIO.output(GPIO_TRIGGER, False) | |
pulse_start = time.time() | |
#Determine what values echo will measure | |
#Listen to echo, 0 means no pulse has been sent back | |
while GPIO.input(GPIO_ECHO) == 0: | |
#Records the time | |
pulse_start = time.time() | |
#Listen to echo, 1 means a pulse has been sent back to echo | |
while GPIO.input(GPIO_ECHO) == 1: | |
#Records the time | |
pulse_stop = time.time() | |
# Calculate pulse duration by subtracting the two | |
#different measurements from echo | |
pulse_duration = pulse_stop-pulse_start | |
#Convert distance into centimetres | |
distance = pulse_duration * 17000 | |
#Round the distance up to nearest 2 decimals | |
distance = round(distance, 2) | |
#Print out a message for each of the different distance values | |
if distance < 10: | |
print "The distance is less than 10cms" | |
elif distance < 20: | |
print "The distance is less than 20cms" | |
elif distance < 30: | |
print "The distance is less than 20cms" | |
else: | |
print "The distance is over 30cms" | |
#exit the program | |
GPIO.cleanup() | |
""" | |
Credits to ModMyPi.com for base code | |
https://www.modmypi.com/blog/hc-sr04-ultrasonic-range-sensor-on-the-raspberry-pi | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment