Our Motive to test ultra sonic waves, when object detect blink a led to show. Print the distance of object. I have added a LED to show up the power is ON, one LED is check for pulse checking, and last LED will blink when object will detect. Object detection distance is set to 75 cm (.75m).
So lets getting started with circuit :
So lets getting started with circuit :
import RPi.GPIO as GPIO
import time
trigger_pin = 20 # pin 38
echo_pin = 21 # pin 40
alert_pin = 18 # pin 12
GPIO.setmode(GPIO.BCM)
GPIO.setup(trigger_pin, GPIO.OUT)
GPIO.setup(echo_pin, GPIO.IN)
GPIO.setup(alert_pin,GPIO.OUT)
def send_trigger_pulse():
GPIO.output(trigger_pin, True)
time.sleep(0.0001)
GPIO.output(trigger_pin, False)
def wait_for_echo(value, timeout):
count = timeout
while GPIO.input(echo_pin) != value and count > 0:
count = count - 1
def get_distance():
send_trigger_pulse()
wait_for_echo(True, 10000)
start = time.time()
wait_for_echo(False, 10000)
finish = time.time()
pulse_len = finish - start
distance_cm = pulse_len / 0.000058
#distance_in = distance_cm / 2.5
#distance_mt = distance_cm / 100
return distance_cm
while True:
distance = get_distance()
print("cm=%f" % distance)
if distance > 0 and distance < 75:
GPIO.output(alert_pin,True)
else:
GPIO.output(alert_pin,False)
time.sleep(0.5)

No comments:
Post a Comment