#!/usr/bin/python import pigpio import time,sys _TRIG=5 # hc-sr04 tigger pin _ECHO=6 # hc-rc04 echo pin # this one must be brought down to 3.3V # from the hc-sr04 5V level through a level converter pi = pigpio.pi() # access the local Pi's gpio if pi.connected == False: print "connection error" sys.exit(-1) else: print "successfully connected" pi.set_mode (_TRIG, pigpio.OUTPUT) pi.set_mode (_ECHO, pigpio.INPUT) # pi.write(_ECHO, pigpio.LOW) print "hardware revision " + str(pi.get_hardware_revision()) count = 0 while True: print "triggering" pi.gpio_trigger(_TRIG,10,1) # pi.write(_TRIG,pigpio.HIGH) # time.sleep(0.000002) # wait 10 us # pi.write(_TRIG,pigpio.LOW) # while pi.read(_ECHO) == pigpio.LOW: # count += 1 # print count timeout = time.time() while pi.read(_ECHO) == 0: start = time.time() if start - timeout > 5: print "timeout: signal not coming high" continue while pi.read(_ECHO) == 1: stop = time.time() if stop - start > 5: print "timeout: signal not going low" continue # print count duration = stop - start distance = duration * 17150 print "distance: " + str(distance) + "cm" time.sleep(1)