I am writing a code for Ultrasonic distance sensor. I have wired all the things perfectly and written the code for it but somehow it gives a runtime error of the channel already being in use. Below is my code:
import RPi.GPIO as gpio import time def distance(measure = 'cm'): gpio.setmode(gpio.BOARD) gpio.setup(12, gpio.OUT) #Here it gives error of channel in use gpio.setup(16, gpio.IN) gpio.output(12, False) #it doesn't show me any value of distance while(gpio.input(16) == 0): nosig = time.time() while(gpio.input(16) == 1): sig = time.time() t = sig - nosig if(measure == 'cm'): distance = t / 0.000058 elif(measure == 'in'): distance = t / 0.000148 else: print('improper measurement: inches or centimeters') distance = None gpio.cleanup() return distance() print(distance('cm'))