raspberry gpio tutorial 4..distance measuring using ultrasonic
python codings
import RPi.GPIO as GPIOimport time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
trig = 4
echo = 18
GPIO.setup(trig , GPIO.OUT)
GPIO.setup(echo , GPIO.IN)
while 1:
GPIO.output(trig, True)
time.sleep(0.0001)
GPIO.output(trig, False)
while GPIO.input(echo) == False:
srt = time.time()
while GPIO.input(echo) == True:
fsh = time.time()
dif = fsh - srt
#for cm :
dis_cm = dif/0.000058
dis_inch = dif/0.000148
print('distance: {}cm'.format(dis_cm))
time.sleep(.5)
*********************************************************************************
copy that and paste in your python idle
Comments
Post a Comment