V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
owenliang
V2EX  ›  Raspberry Pi

不懂电路,谁能讲一下树莓派 GPIO 输入模式。

  •  
  •   owenliang · 2018-11-02 11:01:17 +08:00 · 6863 次点击
    这是一个创建于 1973 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我是电路小白,想接个按钮上去检测一下。

    看到什么上拉下拉电阻,实在是不知道干啥的,谁能通俗的透彻的说一下理解。

    下面是官方的说明,貌似是说可以编程控制 GPIO 默认输入高电平还是低电平的意思,设置默认值的感觉?

    If you do not have the input pin connected to anything, it will 'float'. In other words, the value that is read in is undefined because it is not connected to anything until you press a button or switch. It will probably change value a lot as a result of receiving mains interference.

    To get round this, we use a pull up or a pull down resistor. In this way, the default value of the input can be set. It is possible to have pull up/down resistors in hardware and using software. In hardware, a 10K resistor between the input channel and 3.3V (pull-up) or 0V (pull-down) is commonly used. The RPi.GPIO module allows you to configure the Broadcom SOC to do this in software:

    GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_UP)

    or

    GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

    6 条回复    2018-11-02 14:50:38 +08:00
    sennes
        1
    sennes  
       2018-11-02 11:17:28 +08:00
    建议用上拉电阻的形式。
    1. 按键按下时输入低电平(IO 接地)
    2. 按键恢复后上拉电阻会把输入拉高。
    你可以理解成默认值
    不接上下拉,平时就是浮空状态,逻辑不好判断。
    jatsz
        2
    jatsz  
       2018-11-02 11:54:10 +08:00   ❤️ 2
    楼上说的很简洁,确实是这样的。
    我以前在这里也困惑过,后来还写了篇博客: https://www.imzjy.com/blog/2016-06-26-pullup-resistor
    owenliang
        3
    owenliang  
    OP
       2018-11-02 12:35:48 +08:00
    @jatsz 看了一下,写的不错,3Q
    owenliang
        4
    owenliang  
    OP
       2018-11-02 14:24:07 +08:00
    理解下来,接 GND 的是可以稳定低电平的,啥都没接的会漂来飘去,可以这么理解吧。
    xmoiduts
        5
    xmoiduts  
       2018-11-02 14:48:17 +08:00 via Android
    数电里会提到,io 引脚在高低电平两种状态以外会有个高阻态,此时读取是楼上所说的“浮空”,输入值是不确定的。
    xmoiduts
        6
    xmoiduts  
       2018-11-02 14:50:38 +08:00 via Android
    有些单片机会有内置的上 /下拉电阻,可以编程启用。比如正文最后那两行,Arduino 同理。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3328 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 13:45 · PVG 21:45 · LAX 06:45 · JFK 09:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.