简体   繁体   中英

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. I'm using RPi.GPIO library to access gpio pins. 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. When i read 16th pin it always shows low. I even tried with many other pins like 3,7,16 etc...

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

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. However, when there is no input , you haven't defined any state at all.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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