Robodanjal
Posts: 17
Joined: Tue Aug 11, 2015 8:18 pm

pull_up_down=GPIO.PUD_UP issue

Mon May 02, 2016 6:28 pm

I have been working on a script which will run a photobooth that I have built for my wedding.

After testing it for 30 minutes or so today it started to trigger the photo preview sequence even though the button had not been pressed. To test if this was being caused by the button I simply removed it; it still triggered on it's own.

I removed everything from the Pi and now have it connected to the monitor, keyboard, and mouse. I have run the script below to see if I can rule out any mistake made in my photobooth script.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

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

while True:
    input_state = GPIO.input(23)
    if input_state == False:
        print('Button Pressed')
        time.sleep(0.2)
With this code I am getting "Button Pressed" over and over again without the button being pressed.

Does anyone perhaps know what could be causing this?
Last edited by Robodanjal on Mon May 02, 2016 8:57 pm, edited 1 time in total.

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: pull_up_down=GPIO.PUD_UP issue

Mon May 02, 2016 6:57 pm

What have you got connected to GPIO 18?

Robodanjal
Posts: 17
Joined: Tue Aug 11, 2015 8:18 pm

Re: pull_up_down=GPIO.PUD_UP issue

Mon May 02, 2016 7:01 pm

joan wrote:What have you got connected to GPIO 18?
My momentary switch.

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: pull_up_down=GPIO.PUD_UP issue

Mon May 02, 2016 7:05 pm

A photo may help.

The internal pull-up is about 50k ohms. That's normally enough to prevent random changes at the GPIO. However an attached wire will be affected by electrical noise and radio waves. The longer the wire the more susceptible. You could try something like an external 5k pull-up.

Robodanjal
Posts: 17
Joined: Tue Aug 11, 2015 8:18 pm

Re: pull_up_down=GPIO.PUD_UP issue

Mon May 02, 2016 7:11 pm

joan wrote:A photo may help.

The internal pull-up is about 50k ohms. That's normally enough to prevent random changes at the GPIO. However an attached wire will be affected by electrical noise and radio waves. The longer the wire the more susceptible. You could try something like an external 5k pull-up.

It' is quite simply GPIO 18 to switch and switch to ground.

My issue is that this has just started to occur, it was working correctly earlier.

Oddly enough if I do not connect anything to the GPIO pin I still get the output "button pressed".

Quick video to show what is going on. At no point is a button pressed.

https://www.youtube.com/watch?v=7rWMD4l78-0

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: pull_up_down=GPIO.PUD_UP issue

Mon May 02, 2016 7:20 pm

The video appears to show the switch is connected to 5V. If that's the case you may have destroyed the GPIO.

Robodanjal
Posts: 17
Joined: Tue Aug 11, 2015 8:18 pm

Re: pull_up_down=GPIO.PUD_UP issue

Mon May 02, 2016 7:31 pm

joan wrote:The video appears to show the switch is connected to 5V. If that's the case you may have destroyed the GPIO.
You're mistaken, it is connected to ground.

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: pull_up_down=GPIO.PUD_UP issue

Mon May 02, 2016 8:38 pm

The code in the video is set-up for GPIO 23 pin#16, the code you've posted in this thread is GPIO 18 pin#12.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Robodanjal
Posts: 17
Joined: Tue Aug 11, 2015 8:18 pm

Re: pull_up_down=GPIO.PUD_UP issue

Mon May 02, 2016 8:53 pm

DougieLawson wrote:The code in the video is set-up for GPIO 23 pin#16, the code you've posted in this thread is GPIO 18 pin#12.
Granted, but I am using GPIO 23 on the Pi and have changed the code to match that. This doesn't explain the issue I am having.

I shouldn't be getting the "button pressed" output when nothing is being pressed. The issue I am raising is reproduce-able regardless of which GPIO I am using, or if there is anything connected to the GPIO at all.

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: pull_up_down=GPIO.PUD_UP issue

Mon May 02, 2016 9:21 pm

If the pin is floating you can get anything (it will pick up all sorts of stray RF noise). Add an external pull-up or pull-down 10K resistor (depending whether you're detecting a low or high in your code) so that you know what value is expected when the switch is open and when it's closed.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Robodanjal
Posts: 17
Joined: Tue Aug 11, 2015 8:18 pm

Re: pull_up_down=GPIO.PUD_UP issue

Mon May 02, 2016 9:26 pm

DougieLawson wrote:If the pin is floating you can get anything (it will pick up all sorts of stray RF noise). Add an external pull-up or pull-down 10K resistor (depending whether you're detecting a low or high in your code) so that you know what value is expected when the switch is open and when it's closed.
I still don't understand how this was a non-issue for the last 8 months and all of a sudden it is having this issue.

Also, should it be picking up interference when no cabling is attached to the GPIO at all?

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: pull_up_down=GPIO.PUD_UP issue

Mon May 02, 2016 9:29 pm

Because you've got some long unshielded wires attached to your GPIO. Those things are acting as an antenna.

What's the difficulty with adding a 10K resistor?
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Robodanjal
Posts: 17
Joined: Tue Aug 11, 2015 8:18 pm

Re: pull_up_down=GPIO.PUD_UP issue

Mon May 02, 2016 9:37 pm

DougieLawson wrote:Because you've got some long unshielded wires attached to your GPIO. Those things are acting as an antenna.

What's the difficulty with adding a 10K resistor?
No difficulty, I'm just not convinced that this is the cause of the issue I'm having. Also, should I still be having this issue when I have no cables connected at all? As in I run the script with nothing attached to the GPIO and I still get the button pressed output.

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: pull_up_down=GPIO.PUD_UP issue

Mon May 02, 2016 9:49 pm

Just a bare pin acts as an antenna if there's enough RF noise nearby.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

boyoh
Posts: 1474
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: pull_up_down=GPIO.PUD_UP issue

Tue May 03, 2016 3:37 pm

Some times under standing why you use pull up and pull down resisters can be hard to understand.
The GPIO in it's normal state as a very high impedance( resistance) some times called TriStat
It will neither sink or source a signal( Floating) To give it a working impedance level you use
Resisters to set the level, I think 10k is a good level,

If your in/put signal is a active 1( high) you connect the 10k to the 0v- Gnd Rail.

If your in/put signal is a Active 0 ( low ) you connect the 10k to the 3.3v+rail
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

Robodanjal
Posts: 17
Joined: Tue Aug 11, 2015 8:18 pm

Re: pull_up_down=GPIO.PUD_UP issue

Tue May 03, 2016 5:09 pm

boyoh wrote:Some times under standing why you use pull up and pull down resisters can be hard to understand.
The GPIO in it's normal state as a very high impedance( resistance) some times called TriStat
It will neither sink or source a signal( Floating) To give it a working impedance level you use
Resisters to set the level, I think 10k is a good level,

If your in/put signal is a active 1( high) you connect the 10k to the 0v- Gnd Rail.

If your in/put signal is a Active 0 ( low ) you connect the 10k to the 3.3v+rail
Thanks for your reply Boyoh, I appreciate you taking the time to reply.

Unfortunately I have tested my Pi against a borrowed one and it seems that the GPIO on mine is faulty. Same code, same connected hardware, and the borrowed one works correctly.

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: pull_up_down=GPIO.PUD_UP issue

Tue May 03, 2016 5:36 pm

Robodanjal wrote:Unfortunately I have tested my Pi against a borrowed one and it seems that the GPIO on mine is faulty. Same code, same connected hardware, and the borrowed one works correctly.
Did you also swap the SD card?

Return to “Troubleshooting”