简体   繁体   English

Raspberry Pi B + GPIO输入值无需连接即可更改

[英]Raspberry Pi B+ GPIO input value is changing without connecting

I am new to Raspberry Pi programming. 我是Raspberry Pi编程的新手。 Using Raspberry Pi Model B+ I was trying to read input value through GPIO pins. 使用Raspberry Pi Model B +我试图通过GPIO引脚读取输入值。 But it end up with GPIO pin value is going HIGH and LOW alternatively (without any uniform pattern). 但最终GPIO引脚值变为HIGH and LOW (没有任何统一的模式)。

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 . 我期待着直到通过setup GPIO.INGPIO引脚setup GPIO.IN它的值将为0并且只有当电源连接时它才会为1

Here is the sample python code I have written for to check the status of the PIN: 以下是我为检查PIN状态而编写的示例python代码:

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? 那么,如何通过这些PIN读取输入信号? Is it any problem on my Raspberry Pi Board? 我的Raspberry Pi Board上有什么问题吗?

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 . 为什么不连接时输入为LOW ,为什么不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. 如果没有任何值将HIGHLOW拉高,噪声(在空中?)可以将输入移动到任何电压,因此不会定义输入。

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? 那么,如何通过这些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? 我的Raspberry Pi Board上有什么问题吗?

This is not enough to know if it has. 这还不足以知道它是否有。

That is normal, you didn't connected the GPIO pins to anything, so they're "floating", meaning it will change randomly. 这是正常的,你没有将GPIO引脚连接到任何东西,因此它们“浮动”,这意味着它将随机改变。

Tie a pull-down resistor to the GPIO and it will be stabilized. 将下拉电阻连接到GPIO,它将稳定下来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM