1

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)?

1 Answer 1

1

The question seems to be asking for the Raspberry Pico equivalent of the cleanup() function that is discussed here for the Raspberry Pi.

After looking at the objects that were used such as digitalio and board.GP0 I found the deinit() function. This allows one to re-use the GPIO pins in the same script.

Code

A working function was hence found with:

def detect_connection_between_two_pins_circuitpythonV5(left, right,circuitpython_pins):
    left_boardpin=pin_to_board_pin(left)
    right_boardpin=pin_to_board_pin(right)
    out = digitalio.DigitalInOut(left_boardpin)
    out.direction = digitalio.Direction.OUTPUT
    out.value=1
    
    input = digitalio.DigitalInOut(right_boardpin)
    input.direction = digitalio.Direction.INPUT
    input.pull = digitalio.Pull.DOWN

    # Check if the input has an incoming value.
    if input.value:
        out.deinit()
        input.deinit()
        return True
    #row.value = 0
    out.deinit()
    input.deinit()
    return False

and for completeness:

def pin_to_board_pin(pin_nr):
    if pin_nr==0:
        return board.GP0
    elif pin_nr==1:
        return board.GP1
    elif pin_nr==2:
        return board.GP2
    elif pin_nr==3:
        return board.GP3
    elif pin_nr==4:
        return board.GP4
    elif pin_nr==5:
        return board.GP5
    elif pin_nr==6:
        return board.GP6
    elif pin_nr==7:
        return board.GP7
    elif pin_nr==8:
        return board.GP8
    elif pin_nr==9:
        return board.GP9
    elif pin_nr==10:
        return board.GP10
    elif pin_nr==11:
        return board.GP11
    elif pin_nr==12:
        return board.GP12
    elif pin_nr==13:
        return board.GP13
    elif pin_nr==14:
        return board.GP14
    elif pin_nr==15:
        return board.GP15
    elif pin_nr==16:
        return board.GP16
    elif pin_nr==17:
        return board.GP17
    elif pin_nr==18:
        return board.GP18
    elif pin_nr==19:
        return board.GP19
    elif pin_nr==20:
        return board.GP20
    elif pin_nr==21:
        return board.GP21
    elif pin_nr==22:
        return board.GP22
    elif pin_nr==23:
        return board.GP23
    elif pin_nr==24:
        return board.GP24
    elif pin_nr==25:
        return board.GP25
    elif pin_nr==26:
        return board.GP26
    elif pin_nr==27:
        return board.GP27
    elif pin_nr==28:
        return board.GP28
    else:
        raise Exception(f"No  pin found for: pin_nr={pin_nr}.")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.