|
- '''
- Pin#7:GPIO(56, "in/out")
- Pin#16:GPIO(101,"in/out")
- Pin#18:GPIO(121,"in/out")
- Pin#19:GPIO(4, "in/out")
- Pin#21:GPIO(3, "in/out")
- Pin#23:GPIO(2, "in/out")
- Pin#24:GPIO(5,"in/out")
- Pin#26:GPIO(6,"in/out")
- Pin#27:GPIO(1, "in/out")
- Pin#28:GPIO(0,"in/out")
- Pin#29:GPIO(122, "in/out")
- Pin#31:GPIO(123, "in/out")
- Pin#33:GPIO(124, "in/out")
- Pin#35:GPIO(125, "in/out")
- Pin#37:GPIO(126, "in/out")
- '''
- from periphery import GPIO
- import time
- import datetime
- def getCM():
- #Send trig pulse
- gpio_trig.write(True)
- time.sleep(20/1000000.0)
- gpio_trig.write(False)
- # time.sleep(10/1000000.0)
- #Wait for echo start
- while(gpio_echo.read() == 0):
- pass
- #Wait for echo end
- now = datetime.datetime.now()
- startTime = now.microsecond
- while(gpio_echo.read() == 1):
- pass
- now = datetime.datetime.now()
- travelTime = now.microsecond- startTime;
- distance = travelTime / 58;
- return distance
- gpio_echo = GPIO(125, "in")
- gpio_trig = GPIO(126, "out")
- gpio_trig.write(False)
- time.sleep(0.03)
- while(1):
- dist = getCM()
- print dist
- time.sleep(1.0)
Copy code
orangepi@OrangePi:~/test$ sudo python ./hc-sr04.py
12
14
16
18
19
21
21
23
23
23
|
|