Search code examples
raspberry-pigpio

RaspberryPi - check state of disconnected pins


I want write script for detecting which pin is connected with which one. For example (BCM mode) I connect GPIO2 with GPIO3 and GPIO4 with GPIO5. My idea was to check each with each if there are connected.

for i in range(2,6):
    for j in range(2,6):
        check(i,j)

enter image description here

If all pins are connected it works, but if same of them are disconnected code print same crazy for me output. I think all input should return 0, but as you see same of them are in HIGHT state.

output:

1 2 3
0 2 4
0 2 5
1 3 2
0 3 4
0 3 5
1 4 2
1 4 3
0 4 5
1 5 2
1 5 3
0 5 4

code:

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
for i in range(2, 6):
    for j in range(2, 6):
        if i == j:
            continue
        GPIO.setup([2, 3, 4, 5], GPIO.OUT, initial=GPIO.LOW)
        GPIO.setup(i, GPIO.OUT, initial=GPIO.HIGH)
        GPIO.setup(j, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
        sleep(0.1)
        if GPIO.input(j):
            l.add(tuple(sorted((i, j))))
print(l)

Solution

  • I think the problem You have is related to pull-up and pull-down resistors. They are used to ensure the "known" level of digital value (1 or 0).

    http://www.resistorguide.com/pull-up-resistor_pull-down-resistor/

    There are many solutions to this "problem" -here is one for Arduino but actually every digital IC has these "problem" and datasheet should say how to deal with unconnected pins and what to expect in case of leaving them dangling.

    https://electronics.stackexchange.com/questions/35704/arduino-has-an-input-value-in-disconnected-pin