Search code examples
pythonpython-2.7inputraspberry-piraspberry-pi2

Raspberry Pi B+ GPIO input value is changing without connecting


I am new to Raspberry Pi programming. Using Raspberry Pi Model B+ I was trying to read input value through GPIO pins. But it end up with GPIO pin value is going HIGH and LOW alternatively (without any uniform pattern).

Why its like that? Is it supposed to be like that? I was expecting until a power is supplied to the GPIO pin with setup GPIO.IN its value will be 0 and only when power is conncted it will be 1.

Here is the sample python code I have written for to check the status of the PIN:

import RPi.GPIO as GPIO
import time

PINS = [11,13,15,16,18,22,36,37]
GPIO.setmode(GPIO.BOARD)

for pin in PINS:
    GPIO.setup(pin,GPIO.IN)

while True:
    try:
        for pin in PINS:
            print  pin, “ input value is  : “, GPIO.input(pin)
        time.sleep(2)
        print “checking pin status "
    except (KeyboardInterrupt, SystemExt)
        GPIO.cleanup()

And output is as follows:

checking pin status 
11 input value is  : 1
13 input value is  : 1
15 input value is  : 1 
16 input value is  : 1
18 input value is  : 1
22 input value is  : 1
36 input value is  : 1
37 input value is  : 1
checking pin status 
11 input value is  : 0
13 input value is  : 0
15 input value is  : 0 
16 input value is  : 0
18 input value is  : 0
22 input value is  : 0
36 input value is  : 0
37 input value is  : 1
checking pin status 
11 input value is  : 1
13 input value is  : 1
15 input value is  : 1 
16 input value is  : 1
18 input value is  : 1
22 input value is  : 0
36 input value is  : 0
37 input value is  : 0
checking pin status 
11 input value is  : 1
13 input value is  : 1
15 input value is  : 1 
16 input value is  : 1
18 input value is  : 1
22 input value is  : 1
36 input value is  : 1
37 input value is  : 1

So, how can I read a input signal through these PIN? Is it any problem on my Raspberry Pi Board?


Solution

  • This has nothing to do with software, it's hardware.

    Why do you expect the input to be LOW when not connected, why not HIGH. If there's nothing pulling the value HIGH or LOW, the noise (in the air?) can move the input to any voltage, so the input will not be defined.

    Now, if you configure the input with a pull-up/pull-down resistor, then it will have a stable value even when nothing is connected to it.

    So, regarding your question:

    So, how can I read a input signal through these PIN?

    To read an input signal you first have to have an input signal, so connect one to your input pins.

    Is it any problem on my Raspberry Pi Board?

    This is not enough to know if it has.