erinaco/test/ultraschall.py

26 lines
559 B
Python
Raw Normal View History

2017-04-15 02:42:23 +02:00
#!/usr/bin/python3
# Datei ultraschall.py
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
trig = 5 # GPIO-Pin-Nummern
echo = 17
GPIO.setup(echo, GPIO.IN)
GPIO.setup(trig, GPIO.OUT)
while True:
GPIO.output(trig, True)
time.sleep(0.00001) # 10 Mikrosekunden
GPIO.output(trig, False)
while GPIO.input(echo) == 0:
pass
start = time.time()
while GPIO.input(echo) == 1:
pass
ende = time.time()
entfernung = ((ende - start) * 34300) / 2
print("Entfernung:", entfernung, "cm")
time.sleep(0.5)