Error Message
After having written a function that sets output and values to specific GPIO pins, I'm running into the following error:
ValueError: GP0 in use.
Code
The function is:
def detect_connection_between_two_pins_circuitpython(left, right):
# Import for circuitpython.
import board
import digitalio
left_pin=get_circuitpython_gpio_pin(board,left)
right_pin=get_circuitpython_gpio_pin(board,right)
# Set the output pin to GPIO pin nr 0.
output_line = digitalio.DigitalInOut(left_pin)
output_line.direction=digitalio.Direction.OUTPUT
# Set the input pin to GPIO pin nr 1.
input_line = digitalio.DigitalInOut(right_pin)
input_line.direction=digitalio.Direction.INPUT
# Put voltage/value of 1 [-] on GPIO pin 0.
output_line.value = 1
if input_line.value == 1:
#output_line.direction=None
output_line=None
#input_line.direction=None
input_line=None
return True
# print(f"{left},{right}")
#output_line.direction=None
output_line=None
#input_line.direction=None
input_line=None
return False
And for completeness, it uses the following mapping:
def get_circuitpython_gpio_pin(board,gpio_pin_nr):
circuitpython_gpio_pins= [
board.GP0,
board.GP1,
board.GP2,
board.GP3,
board.GP4,
board.GP5,
board.GP6,
board.GP7,
board.GP8,
board.GP9,
board.GP10,
board.GP11,
board.GP12,
board.GP13,
board.GP14,
board.GP15,
board.GP16,
board.GP17,
board.GP18,
board.GP19,
board.GP20,
board.GP21,
board.GP22,
board.GP23,
board.GP24,
board.GP25,
board.GP26,
board.GP27,
board.GP28,
]
return circuitpython_gpio_pins[gpio_pin_nr]
Bug Interpretation
The error occurs the second time the function is called, at:
output_line = digitalio.DigitalInOut(left_pin)
Where it tries to set the value of GPIO pin 0
/board.GP0
again. It seems to me it still has some value left from the previous time the function was called. So I tried resetting the GPIO pin 0
with:
#output_line.direction=None
output_line=None
#input_line.direction=None
input_line=None
However, None
is not a valid direction, and setting the object to None
does not alleviate the error.
Question
How can I reset the value of the GPIO pin at the end of the function such that I can keep on calling the function (on the same pins)?