0

I wrote a code that I run in Thonny which detects if a connection is made between the 3.3V output and a GPIO pin on the Raspberry Pico:

print("Hello World!")
import time
import digitalio
import board
print(board.board_id)

btn15_pin = board.GP15


btn15 = digitalio.DigitalInOut(btn15_pin)
btn15.direction = digitalio.Direction.INPUT
btn15.pull = digitalio.Pull.DOWN


while True:
    if btn15.value:
        print("button 15 pressed")
    time.sleep(0.1)

So I thought I could use this to identify which keyboard keys are pressed through by connecting the 20 pins on a FPC connector to the various GPIO inputs on a Raspberry Pico (whilst connecting the ground to the backplate of the keyboard). However, that assumption is wrong, and I have determined that some keyboard matrix is used which creates a connection between various pin pairs within the FPC connections. For example, pressing the left shift button creates a connection between pin 4 and 6. Therefore, I was wondering:

(How) is it possible to detect connections between various GPIO pins (e.g. a connection between GPIO pin 15 and GPIO pin 17) on the Raspberry Pico?

Answer Raspberry PI

I am not able to post an answer to this question. However, this link explains how one can identify connections that are being made between two GPIO connections on a Raspberry (Pi):

Internal wires connect the individual rows and columns of switches so that when a user presses one of the switches, that switch electrically connects a single row to a column.

In the described wiring sceme, these rows and columns are connected to different GPIO pins, resulting in an electrical connection between GPIO pins if a row and a column wire are connected. In particular:

In the connection scheme they describe, they have 4 wires on GPIO pins 1,7,8,25, and another four wires on GPIO pins: 12,16,20,21. Next, the following script practically registers if an electrical connection is made between a GPIO pin of the first group and a GPIO pin of the second group:

# import required libraries
import RPi.GPIO as GPIO
import time

# these GPIO pins are connected to the keypad
# change these according to your connections!
L1 = 25
L2 = 8
L3 = 7
L4 = 1

C1 = 12
C2 = 16
C3 = 20
C4 = 21

# Initialize the GPIO pins

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

GPIO.setup(L1, GPIO.OUT)
GPIO.setup(L2, GPIO.OUT)
GPIO.setup(L3, GPIO.OUT)
GPIO.setup(L4, GPIO.OUT)

# Make sure to configure the input pins to use the internal pull-down resistors

GPIO.setup(C1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# The readLine function implements the procedure discussed in the article
# It sends out a single pulse to one of the rows of the keypad
# and then checks each column for changes
# If it detects a change, the user pressed the button that connects the given line
# to the detected column

def readLine(line, characters):
    GPIO.output(line, GPIO.HIGH)
    if(GPIO.input(C1) == 1):
        print(characters[0])
    if(GPIO.input(C2) == 1):
        print(characters[1])
    if(GPIO.input(C3) == 1):
        print(characters[2])
    if(GPIO.input(C4) == 1):
        print(characters[3])
    GPIO.output(line, GPIO.LOW)

try:
    while True:
        # call the readLine function for each row of the keypad
        readLine(L1, ["1","2","3","A"])
        readLine(L2, ["4","5","6","B"])
        readLine(L3, ["7","8","9","C"])
        readLine(L4, ["*","0","#","D"])
        time.sleep(0.1)
except KeyboardInterrupt:
    print("\nApplication stopped!")

Answer Raspberry Pico

The following code detects a connection between GPIO pin 0 and 1 on a Raspberry Pico:

from machine import Pin
import time

# Do disco if connection is detected
led = Pin(15, Pin.OUT)
# Set the output pin to GPIO pin nr 0.
output_line= Pin(0, Pin.OUT)
# Set the input pin to GPIO pin nr 1.
input_line = Pin(1, Pin.IN, Pin.PULL_DOWN)


while True:
    time.sleep(2)
    
    # Check if the input pin nr 1 has an incoming value.
    if(input_line.value()):
        
        # Connection is found, start disco!
        led.toggle()
        # Not quite time yet, let's hang back.
        time.sleep(0.5)
        # Celebrate victory.
        print("connected")
    
    # Put voltage/value of 1 [-] on GPIO pin 0.
    output_line.value(1)
    print("Started sending output signal on output pin nr 0.")

Note it is advisable to not set one pin to HIGH and the other to LOW whilst connecting them, as that might fry the board.

3
  • The reason that this question was closed as not Pi specific is because "the connection scheme they describe" is a actually a description of how a matrix keyboard works in general; it would be exactly the same if you were using an Arduino etc.
    – goldilocks
    Jan 13, 2022 at 16:21
  • Thank you for the elaboration. I agree with your elaboration. However, respectfully, I think this question is not about the wiring sceme, it was how a Raspberry could be used/coded to detect connections between GPIO pins, or as the answer below puts it identical logical levels amongst GPIO pins. I think that is done significantly different on an Arduino than on a Raspberry. E.g. on a Raspberry that can be done using some code and Thonny, and on an Arduino, I don't know, it seems using the Arduino IDE.
    – a.t.
    Jan 13, 2022 at 16:46
  • 1
    It is in the re-open queue, so if enough users agree with you it will be re-opened. However, that it is "not about the wiring scheme" is a bit of a stretch: You did not know about it when you asked your question, and it is integral to how you solved your problem. You could not write the answer you have without referring to that wiring scheme unless you changed the question substantially, and realistically the code is completely trivial, it is just an implementation of the algorithm that makes the wiring work (and that algorithm would be the same on an Arduino).
    – goldilocks
    Jan 13, 2022 at 17:12

1 Answer 1

1

Even though I think the question is off-topic I'll give an answer.

You can't 100% tell in software if two or more GPIO are connected. All you can do is notice that one or more GPIO always seem to have the same level.

1
  • This answer assumes no knowledge exists beyond the Pico. However, if one can include knowledge on the wiring sceme outside the Pico and take that into account in to the software, one can practically detect a connection between two pins. The examples included in the question show how. So the answer is technically correct, but makes an assumption that implies the answers scope is limited such that the answer does not add significant value to the question in my experience.
    – a.t.
    Jan 13, 2022 at 11:17

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