1
votes

I have a strange problem with raspberry (3B) ang GPIO as Input. I'm actually just experimenting and trying to get a pushbutton press without side effects.

setup

Hardware

  • PIN 16 (GPIO 26) as IN
  • PIN 6 as GND
  • 2 open Jumper wires on the pins for better debugging. Later there will be pushbutton behind

Software

  • Raspbian 9.4 stretch
  • Kernel 4.9.80-v7+ (newest over normal apt-get)
  • RPi.GPIO version 0.63

code

#!/usr/bin/python
import RPi.GPIO as GPIO
from time import sleep

red_channel = 23

GPIO.setmode(GPIO.BCM)
GPIO.setup(red_channel, GPIO.IN, pull_up_down=GPIO.PUD_UP) # pin is up to 3.3V

try:
    while True:
        state = GPIO.input(red_channel)
        if state == 0: # when pin pulled down
            print 'red pressed', state
        sleep(0.3)
except KeyboardInterrupt:
    GPIO.cleanup()

Problem

When I run the code and only come with my hand in the near of the jumper wires or even move them, button pressure will be detected. And at this point I don't get why! Did I get something wrong?

Solutions already tried

  • new wires
  • other ports
  • other raspberry (same version)!
  • OS new installed and GPIO Libs fresh installed
  • Breadboard between (same effect)

Thanks for your help!

1
Your if state == 0: print 'red pressed' is going to print it is pressed when the button is not pressed since GPIO.input(red_channel) will return False only then, was this a typo in the code?nj2237
With this code, you will be getting output red pressed every 0.3 seconds without pressing the buttonnj2237
Because the pin is set up to be up when the button will be pressed it will be pulled down (to GND). In this case GPIO.input(red_channel) will offer a 1 as long pin is up. When button pressed, pin will get pulled down and 0 will be returned. an example: razzpisampler.oreilly.com/ch07.htmlMarios
oh yeah, you are right. my bad! then, this is a very weird behavior indeed. is there any pattern in the output? in the frequency of printing state True or False?nj2237
The script will return 0 as long I move the wire. Fun fact: I inverted the wiring so that the pin is down and will be shorten to 3.3V when button pressed. Same issue. StrangeMarios

1 Answers

0
votes

I can't help thinking that you've got a short or dodgy link in this somewhere.

I'd take off the PI, and test the board with a multi-meter in continuity mode. Check what happens when you wiggle the cables etc.

Any short or wrong link should then show up as long as you test logically