This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ultrasonic sensor using Python
#1


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()
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)