-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmesafe_sensoru.py
More file actions
49 lines (38 loc) · 1.21 KB
/
mesafe_sensoru.py
File metadata and controls
49 lines (38 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# ------------------------------------------
# PIN NUMARALARI
# ------------------------------------------
TRIG = 23
ECHO = 24
print("HC-SR04 mesafe sensoru")
# ------------------------------------------
# GİRİŞ VE ÇIKIŞLARIN BELİRTİLMESİ
# ------------------------------------------
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
# ------------------------------------------
# MESAFE ÖLÇÜMÜ
# ------------------------------------------
while True:
# ölçme aralığı ve veri iletim hızı
GPIO.output(TRIG, False)
print("Olculuyor...")
time.sleep(0.5)
GPIO.output(TRIG, True)
time.sleep(0.0001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO) == 0:
pulse_start = time.time()
while GPIO.input(ECHO) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
# 2 ve 400 aralığındaki ölçümler yapılır onun dışında menzil aşıldı bilgisi verilir
if distance > 2 and distance < 400:
print("Mesafe:", distance - 0.5, "cm")
else:
print("Menzil asildi")