Tinker Board Forum

Full Version: Ultrasonic sensor using Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.


Hi all.

Taking cue from material available at this link, I successfully performed python based range measurement.
Initial hiccups regarding "no module named gpio" was removed by placing GPIO.so in correct folder. (see link).
Sensor pin                                                          TB board pin no.
Vcc                                                                      2 (5 V)
TRIG                                                                    16
ECHO                                                                   18
GND                                                                     any ground on TB

Watch the attached video. Code below.


Code:
import ASUS.GPIO as GPIO  
import time
from time import sleep       
  
GPIO.setmode(GPIO.BOARD)       
GPIO.setwarnings(False)
TRIG = 16

ECHO = 18
print ("Distance Measurement In Progress")

GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)

GPIO.output(TRIG, False)
print ("Waiting For Sensor To Settle")
time.sleep(2)

GPIO.output(TRIG, True)
time.sleep(0.00001)
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)
print ("Distance:",distance,"cm")
GPIO.cleanup()