-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhcsr04.py
62 lines (53 loc) · 1.82 KB
/
hcsr04.py
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
50
51
52
53
54
55
56
57
58
59
60
61
#======================================================================
#
# Python Module to handle an HC-SR04 Ultrasonic Module on a single Pin
# Aimed at use on Picon Zero
#
# Created by Gareth Davies, Mar 2016
# Copyright 4tronix
#
# This code is in the public domain and may be freely copied and used
# No warranty is provided or implied
#
#======================================================================
import RPi.GPIO as GPIO, sys, threading, time, os, subprocess
# Define Sonar Pin (Uses same pin for both Ping and Echo)
sonar = 38
#======================================================================
# General Functions
#
def init():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
def cleanup():
GPIO.cleanup()
#======================================================================
#======================================================================
# UltraSonic Functions
#
# getDistance(). Returns the distance in cm to the nearest reflecting object. 0 == no object
def getDistance():
GPIO.setup(sonar, GPIO.OUT)
# Send 10us pulse to trigger
GPIO.output(sonar, True)
time.sleep(0.00001)
GPIO.output(sonar, False)
start = time.time()
count=time.time()
GPIO.setup(sonar,GPIO.IN)
while GPIO.input(sonar)==0 and time.time()-count<0.1:
start = time.time()
count=time.time()
stop=count
while GPIO.input(sonar)==1 and time.time()-count<0.1:
stop = time.time()
# Calculate pulse length
elapsed = stop-start
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance = elapsed * 34000
# That was the distance there and back so halve the value
distance = distance / 2
return distance
# End of UltraSonic Functions
#======================================================================