简体   繁体   English

Raspberry pi GPIO 输出工作但不输入

[英]Raspberry pi GPIO output working but not input

I am facing a strange problem.我正面临一个奇怪的问题。 My raspberry 3B+ working all fine but only facing issue in GPIO.我的 raspberry 3B+ 工作正常,但仅在 GPIO 中遇到问题。 I'm using RPi.GPIO library to access gpio pins.我正在使用 RPi.GPIO 库来访问 gpio 引脚。 The strange thing is I'm not able to read any of the input logic, but all output can be set and all outputs are working fine.奇怪的是我无法读取任何输入逻辑,但是可以设置所有输出并且所有输出都可以正常工作。

In GPIO.BOARD configuration i have connected 17th pin (ie 3.3V) to 16th pin.在 GPIO.BOARD 配置中,我已将第 17 个引脚(即 3.3V)连接到第 16 个引脚。 When i read 16th pin it always shows low.当我读到第 16 个引脚时,它总是显示低电平。 I even tried with many other pins like 3,7,16 etc...我什至尝试了许多其他引脚,如 3、7、16 等...

my code as below我的代码如下

import RPi.GPIO as a
a.setmode(a.BOARD)
a.setwarnings(False)
a.setup(16,a.IN)
while (True):
    if a.input(16):
        print('high')
    else:
        print('low')

verified with DMM that about pins are getting 3.3V经 DMM 验证,关于引脚的电压为 3.3V

This answer is just an attempt to make you realize the good practices.这个答案只是试图让您意识到良好的做法。

It is always a good practice to use built in pull up or pull down resistors of the raspberry pi when reading the state of a digital pin.在读取数字引脚的状态时,使用树莓派的内置上拉或下拉电阻始终是一个好习惯。

When you are providing the 3.3v direct input from 17th pin , the state of the pin 16 is defined clearly.当您从 17th pin 提供 3.3v 直接输入时,pin 16 的状态被明确定义。 However, when there is no input , you haven't defined any state at all.但是,当没有 input 时,您根本没有定义任何状态。

Code snippet for it is as shown below.它的代码片段如下所示。

a.setup(16,a.IN,pull_up_down=a.PUD_DOWN) #for pull down resistor 

a.setup(16,a.IN,pull_up_down=a.PUD_UP) #for pull up resistor

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

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