I am having trouble recording a signal from my ultrasonic distance detector. This is connected to a bread board which I have in turn connected to the 4 pins on my 4tronix microcon motor controller board.
According to the 4tronix site, the 4 pins are all connected to one GPIO pin on the Pi so what is confusing me is how can I have a GPIO pin be both an Input and an Output?
In my code what I have done is first have the Pin 8 be an Output and then run a GPIO.cleanup() and then run the function where it is an Input to read back the distance.
I do NOT get back any errors when running the script but nothing happens - eventually I ctrl + C it and get the "100" value I have in my "except" part.
Code: Select all
#Testing the sensor import RPi.GPIO as GPIO import time def out_sig(): #Set type of pin numbering GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) #Set the pin to output GPIO.setup(8, GPIO.OUT) GPIO.cleanup() def input_sig(measure="cm"): try: #Set type of pin numbering GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) #Set pin to input for sensor GPIO.setup(8, GPIO.IN) while GPIO.input(8) == 0: nosig = time.time() while GPIO.input(8) == 1: sig = time.time() timeline = sig - nosig if measure == "cm": distance = timeline / 0.000058 else: print "The wrong measurement has been used - use only cm." distance = None GPIO.cleanup() return distance except: distance = 100 GPIO.cleanup() return distance out_sig() print input_sig("cm")